BucketedObjectPool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DunGen.Pooling
  5. {
  6. /// <summary>
  7. /// A simple object pool that groups objects into buckets based on a key
  8. /// </summary>
  9. /// <typeparam name="TKey">The key type that determines how objects are bucketed</typeparam>
  10. /// <typeparam name="TObject">The type of pooled object</typeparam>
  11. public sealed class BucketedObjectPool<TKey, TObject> where TObject : class
  12. {
  13. private readonly Dictionary<TKey, List<TObject>> buckets = new Dictionary<TKey, List<TObject>>();
  14. private readonly Func<TKey, TObject> objectFactory;
  15. private readonly Action<TObject> takeAction;
  16. private readonly Action<TObject> returnAction;
  17. private readonly int initialCapacity;
  18. private readonly Dictionary<TObject, TKey> owningBuckets = new Dictionary<TObject, TKey>();
  19. public BucketedObjectPool(Func<TKey, TObject> objectFactory,
  20. Action<TObject> takeAction = null,
  21. Action<TObject> returnAction = null,
  22. int initialCapacity = 0)
  23. {
  24. this.objectFactory = objectFactory;
  25. this.takeAction = takeAction;
  26. this.returnAction = returnAction;
  27. this.initialCapacity = initialCapacity;
  28. }
  29. /// <summary>
  30. /// Clears out all the existing objects in the pool
  31. /// </summary>
  32. public void Clear()
  33. {
  34. buckets.Clear();
  35. owningBuckets.Clear();
  36. }
  37. /// <summary>
  38. /// Takes an object from the pool, creating a new one if the pool is empty
  39. /// </summary>
  40. /// <param name="key">The bucket to take from</param>
  41. /// <returns>The object returned from the pool</returns>
  42. public TObject TakeObject(TKey key)
  43. {
  44. TryTakeObject(key, out var obj);
  45. return obj;
  46. }
  47. /// <summary>
  48. /// Takes an object from the pool, creating a new one if the pool is empty
  49. /// </summary>
  50. /// <param name="key">The bucket to take from</param>
  51. /// <param name="obj">Object returned from the pool</param>
  52. /// <returns>Did we take an existing object from the pool? False if a new instance had to be created</returns>
  53. public bool TryTakeObject(TKey key, out TObject obj)
  54. {
  55. if(key == null)
  56. throw new ArgumentNullException(nameof(key));
  57. // Create the bucket if it doesn't exist yet
  58. if (!buckets.TryGetValue(key, out var bucket))
  59. bucket = InitialiseBucket(key);
  60. // Take an object from the bucket if there is one..
  61. if (bucket.Count > 0)
  62. {
  63. obj = bucket[bucket.Count - 1];
  64. bucket.RemoveAt(bucket.Count - 1);
  65. takeAction?.Invoke(obj);
  66. return true;
  67. }
  68. // ..otherwise create a new object
  69. else
  70. {
  71. var newObject = objectFactory(key);
  72. owningBuckets[newObject] = key;
  73. obj = newObject;
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// Returns an object to the pool
  79. /// </summary>
  80. /// <param name="obj">The object to return</param>
  81. /// <returns>True if successful</returns>
  82. public bool ReturnObject(TObject obj)
  83. {
  84. if (obj == null)
  85. return false;
  86. // Find which bucket owns this object
  87. if (!owningBuckets.TryGetValue(obj, out var key))
  88. return false;
  89. returnAction?.Invoke(obj);
  90. buckets[key].Add(obj);
  91. return true;
  92. }
  93. /// <summary>
  94. /// Inserts an object into the pool. This is useful for pre-warming the pool with existing objects
  95. /// </summary>
  96. /// <param name="key">The bucket to insert into</param>
  97. /// <param name="obj">The object to insert</param>
  98. /// <returns>True if successful</returns>
  99. public bool InsertObject(TKey key, TObject obj)
  100. {
  101. if(key == null || obj == null)
  102. return false;
  103. if (owningBuckets.TryGetValue(obj, out var existingKey))
  104. {
  105. Debug.LogError("Tried to 'Insert' an object into the pool that already belongs to it, use ReturnObject() instead");
  106. return false;
  107. }
  108. // Create the bucket if it doesn't exist yet
  109. if (!buckets.TryGetValue(key, out var bucket))
  110. bucket = InitialiseBucket(key);
  111. returnAction?.Invoke(obj);
  112. buckets[key].Add(obj);
  113. owningBuckets[obj] = key;
  114. return true;
  115. }
  116. private List<TObject> InitialiseBucket(TKey key)
  117. {
  118. var bucket = new List<TObject>(initialCapacity);
  119. buckets[key] = bucket;
  120. for (int i = 0; i < initialCapacity; i++)
  121. {
  122. var newObject = objectFactory(key);
  123. owningBuckets[newObject] = key;
  124. }
  125. return bucket;
  126. }
  127. public void DumpPoolInfo(Func<TKey, string> getBucketName = null)
  128. {
  129. foreach (var pair in buckets)
  130. {
  131. string bucketName = getBucketName?.Invoke(pair.Key) ?? pair.Key.ToString();
  132. Debug.Log($"Bucket: {bucketName}, Count: {pair.Value.Count}");
  133. }
  134. }
  135. }
  136. }