ObjectPools.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Events;
  4. namespace UnityEngine.Rendering
  5. {
  6. /// <summary>
  7. /// Generic object pool.
  8. /// </summary>
  9. /// <typeparam name="T">Type of the object pool.</typeparam>
  10. public class ObjectPool<T> where T : new()
  11. {
  12. readonly Stack<T> m_Stack = new Stack<T>();
  13. readonly UnityAction<T> m_ActionOnGet;
  14. readonly UnityAction<T> m_ActionOnRelease;
  15. readonly bool m_CollectionCheck = true;
  16. /// <summary>
  17. /// Number of objects in the pool.
  18. /// </summary>
  19. public int countAll { get; private set; }
  20. /// <summary>
  21. /// Number of active objects in the pool.
  22. /// </summary>
  23. public int countActive { get { return countAll - countInactive; } }
  24. /// <summary>
  25. /// Number of inactive objects in the pool.
  26. /// </summary>
  27. public int countInactive { get { return m_Stack.Count; } }
  28. /// <summary>
  29. /// Constructor.
  30. /// </summary>
  31. /// <param name="actionOnGet">Action on get.</param>
  32. /// <param name="actionOnRelease">Action on release.</param>
  33. /// <param name="collectionCheck">True if collection integrity should be checked.</param>
  34. public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease, bool collectionCheck = true)
  35. {
  36. m_ActionOnGet = actionOnGet;
  37. m_ActionOnRelease = actionOnRelease;
  38. m_CollectionCheck = collectionCheck;
  39. }
  40. /// <summary>
  41. /// Get an object from the pool.
  42. /// </summary>
  43. /// <returns>A new object from the pool.</returns>
  44. public T Get()
  45. {
  46. T element;
  47. if (m_Stack.Count == 0)
  48. {
  49. element = new T();
  50. countAll++;
  51. }
  52. else
  53. {
  54. element = m_Stack.Pop();
  55. }
  56. if (m_ActionOnGet != null)
  57. m_ActionOnGet(element);
  58. return element;
  59. }
  60. /// <summary>
  61. /// Pooled object.
  62. /// </summary>
  63. public struct PooledObject : IDisposable
  64. {
  65. readonly T m_ToReturn;
  66. readonly ObjectPool<T> m_Pool;
  67. internal PooledObject(T value, ObjectPool<T> pool)
  68. {
  69. m_ToReturn = value;
  70. m_Pool = pool;
  71. }
  72. /// <summary>
  73. /// Disposable pattern implementation.
  74. /// </summary>
  75. void IDisposable.Dispose() => m_Pool.Release(m_ToReturn);
  76. }
  77. /// <summary>
  78. /// Get et new PooledObject.
  79. /// </summary>
  80. /// <param name="v">Output new typed object.</param>
  81. /// <returns>New PooledObject</returns>
  82. public PooledObject Get(out T v) => new PooledObject(v = Get(), this);
  83. /// <summary>
  84. /// Release an object to the pool.
  85. /// </summary>
  86. /// <param name="element">Object to release.</param>
  87. public void Release(T element)
  88. {
  89. #if UNITY_EDITOR // keep heavy checks in editor
  90. if (m_CollectionCheck && m_Stack.Count > 0)
  91. {
  92. if (m_Stack.Contains(element))
  93. Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
  94. }
  95. #endif
  96. if (m_ActionOnRelease != null)
  97. m_ActionOnRelease(element);
  98. m_Stack.Push(element);
  99. }
  100. }
  101. /// <summary>
  102. /// Generic pool.
  103. /// </summary>
  104. /// <typeparam name="T">Type of the objects in the pull.</typeparam>
  105. public static class GenericPool<T>
  106. where T : new()
  107. {
  108. // Object pool to avoid allocations.
  109. static readonly ObjectPool<T> s_Pool = new ObjectPool<T>(null, null);
  110. /// <summary>
  111. /// Get a new object.
  112. /// </summary>
  113. /// <returns>A new object from the pool.</returns>
  114. public static T Get() => s_Pool.Get();
  115. /// <summary>
  116. /// Get a new PooledObject
  117. /// </summary>
  118. /// <param name="value">Output typed object.</param>
  119. /// <returns>A new PooledObject.</returns>
  120. public static ObjectPool<T>.PooledObject Get(out T value) => s_Pool.Get(out value);
  121. /// <summary>
  122. /// Release an object to the pool.
  123. /// </summary>
  124. /// <param name="toRelease">Object to release.</param>
  125. public static void Release(T toRelease) => s_Pool.Release(toRelease);
  126. }
  127. /// <summary>
  128. /// Generic pool without collection checks.
  129. /// This class is an alternative for the GenericPool for object that allocate memory when they are being compared.
  130. /// It is the case for the CullingResult class from Unity, and because of this in HDRP HDCullingResults generates garbage whenever we use ==, .Equals or ReferenceEquals.
  131. /// This pool doesn't do any of these comparison because we don't check if the stack already contains the element before releasing it.
  132. /// </summary>
  133. /// <typeparam name="T">Type of the objects in the pull.</typeparam>
  134. public static class UnsafeGenericPool<T>
  135. where T : new()
  136. {
  137. // Object pool to avoid allocations.
  138. static readonly ObjectPool<T> s_Pool = new ObjectPool<T>(null, null, false);
  139. /// <summary>
  140. /// Get a new object.
  141. /// </summary>
  142. /// <returns>A new object from the pool.</returns>
  143. public static T Get() => s_Pool.Get();
  144. /// <summary>
  145. /// Get a new PooledObject
  146. /// </summary>
  147. /// <param name="value">Output typed object.</param>
  148. /// <returns>A new PooledObject.</returns>
  149. public static ObjectPool<T>.PooledObject Get(out T value) => s_Pool.Get(out value);
  150. /// <summary>
  151. /// Release an object to the pool.
  152. /// </summary>
  153. /// <param name="toRelease">Object to release.</param>
  154. public static void Release(T toRelease) => s_Pool.Release(toRelease);
  155. }
  156. /// <summary>
  157. /// List Pool.
  158. /// </summary>
  159. /// <typeparam name="T">Type of the objects in the pooled lists.</typeparam>
  160. public static class ListPool<T>
  161. {
  162. // Object pool to avoid allocations.
  163. static readonly ObjectPool<List<T>> s_Pool = new ObjectPool<List<T>>(null, l => l.Clear());
  164. /// <summary>
  165. /// Get a new List
  166. /// </summary>
  167. /// <returns>A new List</returns>
  168. public static List<T> Get() => s_Pool.Get();
  169. /// <summary>
  170. /// Get a new list PooledObject.
  171. /// </summary>
  172. /// <param name="value">Output typed List.</param>
  173. /// <returns>A new List PooledObject.</returns>
  174. public static ObjectPool<List<T>>.PooledObject Get(out List<T> value) => s_Pool.Get(out value);
  175. /// <summary>
  176. /// Release an object to the pool.
  177. /// </summary>
  178. /// <param name="toRelease">List to release.</param>
  179. public static void Release(List<T> toRelease) => s_Pool.Release(toRelease);
  180. }
  181. /// <summary>
  182. /// HashSet Pool.
  183. /// </summary>
  184. /// <typeparam name="T">Type of the objects in the pooled hashsets.</typeparam>
  185. public static class HashSetPool<T>
  186. {
  187. // Object pool to avoid allocations.
  188. static readonly ObjectPool<HashSet<T>> s_Pool = new ObjectPool<HashSet<T>>(null, l => l.Clear());
  189. /// <summary>
  190. /// Get a new HashSet
  191. /// </summary>
  192. /// <returns>A new HashSet</returns>
  193. public static HashSet<T> Get() => s_Pool.Get();
  194. /// <summary>
  195. /// Get a new list PooledObject.
  196. /// </summary>
  197. /// <param name="value">Output typed HashSet.</param>
  198. /// <returns>A new HashSet PooledObject.</returns>
  199. public static ObjectPool<HashSet<T>>.PooledObject Get(out HashSet<T> value) => s_Pool.Get(out value);
  200. /// <summary>
  201. /// Release an object to the pool.
  202. /// </summary>
  203. /// <param name="toRelease">hashSet to release.</param>
  204. public static void Release(HashSet<T> toRelease) => s_Pool.Release(toRelease);
  205. }
  206. /// <summary>
  207. /// Dictionary Pool.
  208. /// </summary>
  209. /// <typeparam name="TKey">Key type.</typeparam>
  210. /// <typeparam name="TValue">Value type.</typeparam>
  211. public static class DictionaryPool<TKey, TValue>
  212. {
  213. // Object pool to avoid allocations.
  214. static readonly ObjectPool<Dictionary<TKey, TValue>> s_Pool
  215. = new ObjectPool<Dictionary<TKey, TValue>>(null, l => l.Clear());
  216. /// <summary>
  217. /// Get a new Dictionary
  218. /// </summary>
  219. /// <returns>A new Dictionary</returns>
  220. public static Dictionary<TKey, TValue> Get() => s_Pool.Get();
  221. /// <summary>
  222. /// Get a new dictionary PooledObject.
  223. /// </summary>
  224. /// <param name="value">Output typed Dictionary.</param>
  225. /// <returns>A new Dictionary PooledObject.</returns>
  226. public static ObjectPool<Dictionary<TKey, TValue>>.PooledObject Get(out Dictionary<TKey, TValue> value)
  227. => s_Pool.Get(out value);
  228. /// <summary>
  229. /// Release an object to the pool.
  230. /// </summary>
  231. /// <param name="toRelease">Dictionary to release.</param>
  232. public static void Release(Dictionary<TKey, TValue> toRelease) => s_Pool.Release(toRelease);
  233. }
  234. }