summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Core/Pooling/PathPool.cs
blob: 9fa75d143d8b723455b9ab6afd92ee6842d029db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//#define ASTAR_NO_POOLING // Disable pooling for some reason. Maybe for debugging or just for measuring the difference.
using System;
using System.Collections.Generic;

namespace Pathfinding {
	/// <summary>Pools path objects to reduce load on the garbage collector</summary>
	public static class PathPool {
		static readonly Dictionary<Type, Stack<Path> > pool = new Dictionary<Type, Stack<Path> >();
		static readonly Dictionary<Type, int> totalCreated = new Dictionary<Type, int>();

		/// <summary>
		/// Adds a path to the pool.
		/// This function should not be used directly. Instead use the Path.Claim and Path.Release functions.
		/// </summary>
		public static void Pool (Path path) {
#if !ASTAR_NO_POOLING
			lock (pool) {
				if (((IPathInternals)path).Pooled) {
					throw new System.ArgumentException("The path is already pooled.");
				}

				Stack<Path> poolStack;
				if (!pool.TryGetValue(path.GetType(), out poolStack)) {
					poolStack = new Stack<Path>();
					pool[path.GetType()] = poolStack;
				}

				((IPathInternals)path).Pooled = true;
				((IPathInternals)path).OnEnterPool();
				poolStack.Push(path);
			}
#endif
		}

		/// <summary>Total created instances of paths of the specified type</summary>
		public static int GetTotalCreated (Type type) {
			int created;

			if (totalCreated.TryGetValue(type, out created)) {
				return created;
			} else {
				return 0;
			}
		}

		/// <summary>Number of pooled instances of a path of the specified type</summary>
		public static int GetSize (Type type) {
			Stack<Path> poolStack;

			if (pool.TryGetValue(type, out poolStack)) {
				return poolStack.Count;
			} else {
				return 0;
			}
		}

		/// <summary>Get a path from the pool or create a new one if the pool is empty</summary>
		public static T GetPath<T>() where T : Path, new() {
#if ASTAR_NO_POOLING
			T result = new T();
			((IPathInternals)result).Reset();
			return result;
#else
			lock (pool) {
				T result;
				Stack<Path> poolStack;
				if (pool.TryGetValue(typeof(T), out poolStack) && poolStack.Count > 0) {
					// Guaranteed to have the correct type
					result = poolStack.Pop() as T;
				} else {
					result = new T();

					// Make sure an entry for the path type exists
					if (!totalCreated.ContainsKey(typeof(T))) {
						totalCreated[typeof(T)] = 0;
					}

					totalCreated[typeof(T)]++;
				}

				((IPathInternals)result).Pooled = false;
				((IPathInternals)result).Reset();
				return result;
			}
#endif
		}
	}
}