PathPool.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //#define ASTAR_NO_POOLING // Disable pooling for some reason. Maybe for debugging or just for measuring the difference.
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace PF {
  6. /** Pools path objects to reduce load on the garbage collector */
  7. public static class PathPool {
  8. static readonly Dictionary<Type, Stack<Path> > pool = new Dictionary<Type, Stack<Path> >();
  9. static readonly Dictionary<Type, int> totalCreated = new Dictionary<Type, int>();
  10. /** Adds a path to the pool.
  11. * This function should not be used directly. Instead use the Path.Claim and Path.Release functions.
  12. */
  13. public static void Pool (Path path) {
  14. #if !ASTAR_NO_POOLING
  15. lock (pool) {
  16. if (((IPathInternals)path).Pooled) {
  17. throw new System.ArgumentException("The path is already pooled.");
  18. }
  19. Stack<Path> poolStack;
  20. if (!pool.TryGetValue(path.GetType(), out poolStack)) {
  21. poolStack = new Stack<Path>();
  22. pool[path.GetType()] = poolStack;
  23. }
  24. ((IPathInternals)path).Pooled = true;
  25. ((IPathInternals)path).OnEnterPool();
  26. poolStack.Push(path);
  27. }
  28. #endif
  29. }
  30. /** Total created instances of paths of the specified type */
  31. public static int GetTotalCreated (Type type) {
  32. int created;
  33. if (totalCreated.TryGetValue(type, out created)) {
  34. return created;
  35. } else {
  36. return 0;
  37. }
  38. }
  39. /** Number of pooled instances of a path of the specified type */
  40. public static int GetSize (Type type) {
  41. Stack<Path> poolStack;
  42. if (pool.TryGetValue(type, out poolStack)) {
  43. return poolStack.Count;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. /** Get a path from the pool or create a new one if the pool is empty */
  49. public static T GetPath<T>() where T : Path, new() {
  50. #if ASTAR_NO_POOLING
  51. T result = new T();
  52. ((IPathInternals)result).Reset();
  53. return result;
  54. #else
  55. lock (pool) {
  56. T result;
  57. Stack<Path> poolStack;
  58. if (pool.TryGetValue(typeof(T), out poolStack) && poolStack.Count > 0) {
  59. // Guaranteed to have the correct type
  60. result = poolStack.Pop() as T;
  61. } else {
  62. result = new T();
  63. // Make sure an entry for the path type exists
  64. if (!totalCreated.ContainsKey(typeof(T))) {
  65. totalCreated[typeof(T)] = 0;
  66. }
  67. totalCreated[typeof(T)]++;
  68. }
  69. ((IPathInternals)result).Pooled = false;
  70. ((IPathInternals)result).Reset();
  71. return result;
  72. }
  73. #endif
  74. }
  75. }
  76. /** Pools path objects to reduce load on the garbage collector.
  77. * \deprecated Generic version is now obsolete to trade an extremely tiny performance decrease for a large decrease in boilerplate for Path classes
  78. */
  79. [System.Obsolete("Generic version is now obsolete to trade an extremely tiny performance decrease for a large decrease in boilerplate for Path classes")]
  80. public static class PathPool<T> where T : Path, new() {
  81. /** Recycles a path and puts in the pool.
  82. * This function should not be used directly. Instead use the Path.Claim and Path.Release functions.
  83. */
  84. public static void Recycle (T path) {
  85. PathPool.Pool(path);
  86. }
  87. /** Warms up path, node list and vector list pools.
  88. * Makes sure there is at least \a count paths, each with a minimum capacity for paths with length \a length in the pool.
  89. * The capacity means that paths shorter or equal to the capacity can be calculated without any large allocations taking place.
  90. */
  91. public static void Warmup (int count, int length) {
  92. ListPool<GraphNode>.Warmup(count, length);
  93. ListPool<Vector3>.Warmup(count, length);
  94. var tmp = new Path[count];
  95. for (int i = 0; i < count; i++) { tmp[i] = GetPath(); tmp[i].Claim(tmp); }
  96. for (int i = 0; i < count; i++) tmp[i].Release(tmp);
  97. }
  98. public static int GetTotalCreated () {
  99. return PathPool.GetTotalCreated(typeof(T));
  100. }
  101. public static int GetSize () {
  102. return PathPool.GetSize(typeof(T));
  103. }
  104. [System.Obsolete("Use PathPool.GetPath<T> instead of PathPool<T>.GetPath")]
  105. public static T GetPath () {
  106. return PathPool.GetPath<T>();
  107. }
  108. }
  109. }