MicroObjectRecycler.inl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/BackendCommon/MicroObjectRecycler.h>
  6. namespace anki {
  7. template<typename T>
  8. inline void MicroObjectRecycler<T>::destroy()
  9. {
  10. LockGuard<Mutex> lock(m_mtx);
  11. for(U32 i = 0; i < m_objectCache.getSize(); ++i)
  12. {
  13. T* mobj = m_objectCache[i];
  14. ANKI_ASSERT(mobj);
  15. deleteInstance(GrMemoryPool::getSingleton(), mobj);
  16. }
  17. m_objectCache.destroy();
  18. ANKI_ASSERT(m_inUseObjects == 0 && "Destroying the recycler while objects have not recycled yet");
  19. }
  20. template<typename T>
  21. inline T* MicroObjectRecycler<T>::findToReuse()
  22. {
  23. T* out = nullptr;
  24. LockGuard<Mutex> lock(m_mtx);
  25. adjustAliveObjectCount();
  26. // Trim the cache but leave at least one object to be recycled
  27. trimCacheInternal(max(m_availableObjectsAfterTrim, 1u));
  28. if(m_objectCache.getSize())
  29. {
  30. out = m_objectCache[m_objectCache.getSize() - 1];
  31. m_objectCache.popBack();
  32. }
  33. ANKI_ASSERT(out == nullptr || out->getRefcount() == 0);
  34. m_cacheMisses += (out == nullptr);
  35. #if ANKI_ASSERTIONS_ENABLED
  36. ++m_inUseObjects;
  37. #endif
  38. return out;
  39. }
  40. template<typename T>
  41. void MicroObjectRecycler<T>::recycle(T* mobj)
  42. {
  43. ANKI_ASSERT(mobj);
  44. ANKI_ASSERT(mobj->getRefcount() == 0);
  45. LockGuard<Mutex> lock(m_mtx);
  46. m_objectCache.emplaceBack(mobj);
  47. trimCacheInternal(m_availableObjectsAfterTrim);
  48. #if ANKI_ASSERTIONS_ENABLED
  49. ANKI_ASSERT(m_inUseObjects > 0);
  50. --m_inUseObjects;
  51. #endif
  52. }
  53. template<typename T>
  54. void MicroObjectRecycler<T>::trimCacheInternal(U32 aliveObjectCountAfterTrim)
  55. {
  56. aliveObjectCountAfterTrim = min(aliveObjectCountAfterTrim, m_objectCache.getSize());
  57. const U32 toBeKilledCount = m_objectCache.getSize() - aliveObjectCountAfterTrim;
  58. if(toBeKilledCount == 0)
  59. {
  60. return;
  61. }
  62. for(U32 i = 0; i < toBeKilledCount; ++i)
  63. {
  64. deleteInstance(GrMemoryPool::getSingleton(), m_objectCache[i]);
  65. m_objectCache[i] = nullptr;
  66. }
  67. m_objectCache.erase(m_objectCache.getBegin(), m_objectCache.getBegin() + toBeKilledCount);
  68. }
  69. template<typename T>
  70. void MicroObjectRecycler<T>::adjustAliveObjectCount()
  71. {
  72. if(m_requests < kMaxRequestsPerAdjustment) [[likely]]
  73. {
  74. // Not enough requests, keep getting stats
  75. ++m_requests;
  76. }
  77. else
  78. {
  79. if(m_cacheMisses)
  80. {
  81. // Need more alive objects
  82. m_availableObjectsAfterTrim += 4;
  83. }
  84. else if(m_availableObjectsAfterTrim > 0)
  85. {
  86. // Have more than enough alive objects per request, decrease alive objects
  87. --m_availableObjectsAfterTrim;
  88. }
  89. // Start new cycle
  90. m_cacheMisses = 0;
  91. m_requests = 0;
  92. }
  93. }
  94. } // end namespace anki