ObjectPool.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if !UNITY_EDITOR
  2. // Extra optimizations when not running in the editor, but less error checking
  3. #define ASTAR_OPTIMIZE_POOLING
  4. #endif
  5. using System;
  6. using System.Collections.Generic;
  7. namespace PF {
  8. public interface IAstarPooledObject {
  9. void OnEnterPool ();
  10. }
  11. /** Lightweight object Pool for IAstarPooledObject.
  12. * Handy class for pooling objects of type T which implements the IAstarPooledObject interface.
  13. *
  14. * Usage:
  15. * - Claim a new object using \code SomeClass foo = ObjectPool<SomeClass>.Claim (); \endcode
  16. * - Use it and do stuff with it
  17. * - Release it with \code ObjectPool<SomeClass>.Release (foo); \endcode
  18. *
  19. * After you have released a object, you should never use it again.
  20. *
  21. * \since Version 3.2
  22. * \version Since 3.7.6 this class is thread safe
  23. * \see Pathfinding.Util.ListPool
  24. * \see ObjectPoolSimple
  25. */
  26. public static class ObjectPool<T> where T : class, IAstarPooledObject, new(){
  27. public static T Claim () {
  28. return ObjectPoolSimple<T>.Claim();
  29. }
  30. public static void Release (ref T obj) {
  31. // obj will be set to null so we need to copy the reference
  32. var tmp = obj;
  33. ObjectPoolSimple<T>.Release(ref obj);
  34. tmp.OnEnterPool();
  35. }
  36. }
  37. /** Lightweight object Pool.
  38. * Handy class for pooling objects of type T.
  39. *
  40. * Usage:
  41. * - Claim a new object using \code SomeClass foo = ObjectPool<SomeClass>.Claim (); \endcode
  42. * - Use it and do stuff with it
  43. * - Release it with \code ObjectPool<SomeClass>.Release (foo); \endcode
  44. *
  45. * After you have released a object, you should never use it again.
  46. *
  47. * \since Version 3.2
  48. * \version Since 3.7.6 this class is thread safe
  49. * \see Pathfinding.Util.ListPool
  50. * \see ObjectPool
  51. */
  52. public static class ObjectPoolSimple<T> where T : class, new(){
  53. /** Internal pool */
  54. static List<T> pool = new List<T>();
  55. #if !ASTAR_NO_POOLING
  56. static readonly HashSet<T> inPool = new HashSet<T>();
  57. #endif
  58. /** Claim a object.
  59. * Returns a pooled object if any are in the pool.
  60. * Otherwise it creates a new one.
  61. * After usage, this object should be released using the Release function (though not strictly necessary).
  62. */
  63. public static T Claim () {
  64. #if ASTAR_NO_POOLING
  65. return new T();
  66. #else
  67. lock (pool) {
  68. if (pool.Count > 0) {
  69. T ls = pool[pool.Count-1];
  70. pool.RemoveAt(pool.Count-1);
  71. inPool.Remove(ls);
  72. return ls;
  73. } else {
  74. return new T();
  75. }
  76. }
  77. #endif
  78. }
  79. /** Releases an object.
  80. * After the object has been released it should not be used anymore.
  81. * The variable will be set to null to prevent silly mistakes.
  82. *
  83. * \throws System.InvalidOperationException
  84. * Releasing an object when it has already been released will cause an exception to be thrown.
  85. * However enabling ASTAR_OPTIMIZE_POOLING will prevent this check.
  86. *
  87. * \see Claim
  88. */
  89. public static void Release (ref T obj) {
  90. #if !ASTAR_NO_POOLING
  91. lock (pool) {
  92. #if !ASTAR_OPTIMIZE_POOLING
  93. if (!inPool.Add(obj)) {
  94. throw new InvalidOperationException("You are trying to pool an object twice. Please make sure that you only pool it once.");
  95. }
  96. #endif
  97. pool.Add(obj);
  98. }
  99. #endif
  100. obj = null;
  101. }
  102. /** Clears the pool for objects of this type.
  103. * This is an O(n) operation, where n is the number of pooled objects.
  104. */
  105. public static void Clear () {
  106. lock (pool) {
  107. #if !ASTAR_OPTIMIZE_POOLING && !ASTAR_NO_POOLING
  108. inPool.Clear();
  109. #endif
  110. pool.Clear();
  111. }
  112. }
  113. /** Number of objects of this type in the pool */
  114. public static int GetSize () {
  115. return pool.Count;
  116. }
  117. }
  118. }