ObjectPool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Pathfinding.Util {
  8. public interface IAstarPooledObject {
  9. void OnEnterPool();
  10. }
  11. /// <summary>
  12. /// Lightweight object Pool for IAstarPooledObject.
  13. /// Handy class for pooling objects of type T which implements the IAstarPooledObject interface.
  14. ///
  15. /// Usage:
  16. /// - Claim a new object using <code> SomeClass foo = ObjectPool<SomeClass>.Claim (); </code>
  17. /// - Use it and do stuff with it
  18. /// - Release it with <code> ObjectPool<SomeClass>.Release (foo); </code>
  19. ///
  20. /// After you have released a object, you should never use it again.
  21. ///
  22. /// Since: Version 3.2
  23. /// Version: Since 3.7.6 this class is thread safe
  24. /// See: Pathfinding.Util.ListPool
  25. /// See: ObjectPoolSimple
  26. /// </summary>
  27. public static class ObjectPool<T> where T : class, IAstarPooledObject, new(){
  28. public static T Claim () {
  29. return ObjectPoolSimple<T>.Claim();
  30. }
  31. public static void Release (ref T obj) {
  32. obj.OnEnterPool();
  33. ObjectPoolSimple<T>.Release(ref obj);
  34. }
  35. }
  36. /// <summary>
  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 (); </code>
  42. /// - Use it and do stuff with it
  43. /// - Release it with <code> ObjectPool<SomeClass>.Release (foo); </code>
  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. /// </summary>
  52. public static class ObjectPoolSimple<T> where T : class, new(){
  53. /// <summary>Internal pool</summary>
  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. /// <summary>
  59. /// Claim a object.
  60. /// Returns a pooled object if any are in the pool.
  61. /// Otherwise it creates a new one.
  62. /// After usage, this object should be released using the Release function (though not strictly necessary).
  63. /// </summary>
  64. public static T Claim () {
  65. #if ASTAR_NO_POOLING
  66. return new T();
  67. #else
  68. lock (pool) {
  69. if (pool.Count > 0) {
  70. T ls = pool[pool.Count-1];
  71. pool.RemoveAt(pool.Count-1);
  72. inPool.Remove(ls);
  73. return ls;
  74. } else {
  75. return new T();
  76. }
  77. }
  78. #endif
  79. }
  80. /// <summary>
  81. /// Releases an object.
  82. /// After the object has been released it should not be used anymore.
  83. /// The variable will be set to null to prevent silly mistakes.
  84. ///
  85. /// Throws: System.InvalidOperationException
  86. /// Releasing an object when it has already been released will cause an exception to be thrown.
  87. /// However enabling ASTAR_OPTIMIZE_POOLING will prevent this check.
  88. ///
  89. /// See: Claim
  90. /// </summary>
  91. public static void Release (ref T obj) {
  92. #if !ASTAR_NO_POOLING
  93. lock (pool) {
  94. #if !ASTAR_OPTIMIZE_POOLING
  95. if (!inPool.Add(obj)) {
  96. throw new InvalidOperationException("You are trying to pool an object twice. Please make sure that you only pool it once.");
  97. }
  98. #endif
  99. pool.Add(obj);
  100. }
  101. #endif
  102. obj = null;
  103. }
  104. /// <summary>
  105. /// Clears the pool for objects of this type.
  106. /// This is an O(n) operation, where n is the number of pooled objects.
  107. /// </summary>
  108. public static void Clear () {
  109. lock (pool) {
  110. #if !ASTAR_OPTIMIZE_POOLING && !ASTAR_NO_POOLING
  111. inPool.Clear();
  112. #endif
  113. pool.Clear();
  114. }
  115. }
  116. /// <summary>Number of objects of this type in the pool</summary>
  117. public static int GetSize () {
  118. return pool.Count;
  119. }
  120. }
  121. }