| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using GDX.Collections.Generic; |
| | | 7 | | using UnityEngine; |
| | | 8 | | |
| | | 9 | | namespace GDX.Collections.Pooling |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// A <see cref="object" /> <see cref="SimpleList{T}" /> backed pool implementation. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class SimpleListManagedPool : IManagedPool |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The <see cref="Flags" /> index used to determine if the <see cref="SimpleListManagedPool" /> is able to |
| | | 18 | | /// more items as necessary. |
| | | 19 | | /// </summary> |
| | | 20 | | const int k_AllowCreateMoreFlag = 0; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The <see cref="Flags" /> index used to if <see cref="TearDown" /> can be called by a manager. |
| | | 24 | | /// </summary> |
| | | 25 | | const int k_AllowManagedTeardownFlag = 1; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The <see cref="Flags" /> index used to determine if items should be reused when the pool is starved. |
| | | 29 | | /// </summary> |
| | | 30 | | const int k_AllowReuseFlag = 2; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The <see cref="Flags" /> index used to determine if the pool should create items during its constructor. |
| | | 34 | | /// </summary> |
| | | 35 | | const int k_PrewarmPoolFlag = 3; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The object which the pool is based off of, used as a model when creating new items. |
| | | 39 | | /// </summary> |
| | | 40 | | public readonly object BaseObject; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The object which serves as a container for all objects of the pool. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks>Used more by implementations of pools, then this base class.</remarks> |
| | | 46 | | public readonly object ContainerObject; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// A defined function to create items for the pool. |
| | | 50 | | /// </summary> |
| | | 51 | | readonly Func<SimpleListManagedPool, object> m_CreateItemFunc; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The absolutely unique identifier for this pool. |
| | | 55 | | /// </summary> |
| | | 56 | | readonly uint m_Key; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The Maximum number of objects to be managed by the pool. |
| | | 60 | | /// </summary> |
| | | 61 | | readonly int m_MaximumObjects; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The minimum number of objects to be managed by the pool. |
| | | 65 | | /// </summary> |
| | | 66 | | readonly int m_MinimumObjects; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// An <c>event</c> invoked when an item is destroyed by the <see cref="SimpleListManagedPool" />. |
| | | 70 | | /// </summary> |
| | | 71 | | public Action<object> destroyedItem; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// A <see cref="BitArray8" /> used to store pool based flags, as well as provide additional spots for imple |
| | | 75 | | /// </summary> |
| | | 76 | | /// <remarks> |
| | | 77 | | /// Index 0-3 (<see cref="k_AllowCreateMoreFlag" />, <see cref="k_AllowManagedTeardownFlag" />, |
| | | 78 | | /// <see cref="k_AllowReuseFlag" />, and <see cref="k_PrewarmPoolFlag" />) are used by the |
| | | 79 | | /// <see cref="SimpleListManagedPool" /> itself, leaving 4-7 for additional use. |
| | | 80 | | /// </remarks> |
| | | 81 | | public BitArray8 Flags; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// A cached count of the number of items contained in <see cref="InItems" />. |
| | | 85 | | /// </summary> |
| | | 86 | | public int InCachedCount; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// A collection of items that are currently contained in the pool for use when spawning items upon request. |
| | | 90 | | /// </summary> |
| | | 91 | | public SimpleList<object> InItems; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// A collection of items that are currently considered out of the pool, that have been spawned. |
| | | 95 | | /// </summary> |
| | | 96 | | SimpleList<object> m_OutItems; |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// A cached count of the number of items contained in <see cref="m_OutItems" />. |
| | | 100 | | /// </summary> |
| | | 101 | | public int OutCachedCount; |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// An <c>event</c> invoked when an item is returned to the <see cref="SimpleListManagedPool" />. |
| | | 105 | | /// </summary> |
| | | 106 | | public Action<SimpleListManagedPool, object> returnedItem; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// An <c>event</c> invoked when an item is spawned from the <see cref="SimpleListManagedPool" />. |
| | | 110 | | /// </summary> |
| | | 111 | | public Action<SimpleListManagedPool, object> spawnedItem; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// An <c>event</c> invoked when a pool is tearing down, before the items are pooled. |
| | | 115 | | /// </summary> |
| | | 116 | | public Action<SimpleListManagedPool> tearingDown; |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Create a <see cref="SimpleListManagedPool" />. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="baseObject">The object which going to be cloned.</param> |
| | | 122 | | /// <param name="createItemFunc">The function used to create new items for the pool.</param> |
| | | 123 | | /// <param name="minimumObjects">The minimum number of objects to be managed by the pool.</param> |
| | | 124 | | /// <param name="maximumObjects">The maximum number of objects to be managed by the pool.</param> |
| | | 125 | | /// <param name="containerObject">A reference to an object which should be used as the container for created ite |
| | | 126 | | /// <param name="prewarmPool">Should this pool create its items during the constructor?</param> |
| | | 127 | | /// <param name="allowCreateMore">Can more items be created as needed when starved for items?</param> |
| | | 128 | | /// <param name="allowReuseWhenCapped">Should we reuse oldest items when starving for items?</param> |
| | | 129 | | /// <param name="allowManagedTearDown">Does the pool allow a managed tear down event call?</param> |
| | 14 | 130 | | public SimpleListManagedPool( |
| | | 131 | | object baseObject, |
| | | 132 | | Func<SimpleListManagedPool, object> createItemFunc, |
| | | 133 | | int minimumObjects = 10, |
| | | 134 | | int maximumObjects = 50, |
| | | 135 | | object containerObject = null, |
| | | 136 | | bool prewarmPool = true, |
| | | 137 | | bool allowCreateMore = true, |
| | | 138 | | bool allowReuseWhenCapped = false, |
| | | 139 | | bool allowManagedTearDown = false) |
| | 14 | 140 | | { |
| | | 141 | | // Get pool ID ticket |
| | 14 | 142 | | m_Key = ManagedPools.GetNextPoolKey(); |
| | | 143 | | |
| | 14 | 144 | | BaseObject = baseObject; |
| | 14 | 145 | | m_CreateItemFunc = createItemFunc; |
| | 14 | 146 | | m_MinimumObjects = minimumObjects; |
| | 14 | 147 | | m_MaximumObjects = maximumObjects; |
| | | 148 | | |
| | 14 | 149 | | Flags[k_AllowCreateMoreFlag] = allowCreateMore; |
| | 14 | 150 | | Flags[k_AllowManagedTeardownFlag] = allowManagedTearDown; |
| | 14 | 151 | | Flags[k_AllowReuseFlag] = allowReuseWhenCapped; |
| | 14 | 152 | | Flags[k_PrewarmPoolFlag] = prewarmPool; |
| | | 153 | | |
| | 14 | 154 | | ContainerObject = containerObject; |
| | | 155 | | |
| | 14 | 156 | | ManagedPools.Register(this); |
| | | 157 | | |
| | 14 | 158 | | InItems = new SimpleList<object>(maximumObjects); |
| | 14 | 159 | | m_OutItems = new SimpleList<object>(maximumObjects); |
| | | 160 | | |
| | 14 | 161 | | if (!prewarmPool) |
| | 14 | 162 | | { |
| | 14 | 163 | | return; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | for (int i = 0; i < minimumObjects; i++) |
| | 0 | 167 | | { |
| | 0 | 168 | | CreateItem(); |
| | 0 | 169 | | } |
| | 14 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc /> |
| | | 173 | | public void CreateItem() |
| | 12 | 174 | | { |
| | | 175 | | // ReSharper disable once Unity.PerformanceCriticalCodeInvocation, Unity.ExpensiveCode |
| | 12 | 176 | | m_CreateItemFunc(this); |
| | 12 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <inheritdoc /> |
| | | 180 | | public void ForceRemove(object item) |
| | 0 | 181 | | { |
| | 0 | 182 | | int outCount = m_OutItems.Count; |
| | 0 | 183 | | for (int i = 0; i < outCount; i++) |
| | 0 | 184 | | { |
| | 0 | 185 | | if (m_OutItems.Array[i] != item) |
| | 0 | 186 | | { |
| | 0 | 187 | | continue; |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | m_OutItems.RemoveAtSwapBack(i); |
| | 0 | 191 | | OutCachedCount--; |
| | 0 | 192 | | break; |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | int inCount = InItems.Count; |
| | 0 | 196 | | for (int i = 0; i < inCount; i++) |
| | 0 | 197 | | { |
| | 0 | 198 | | if (InItems.Array[i] != item) |
| | 0 | 199 | | { |
| | 0 | 200 | | continue; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | InItems.RemoveAtSwapBack(i); |
| | 0 | 204 | | InCachedCount--; |
| | 0 | 205 | | break; |
| | | 206 | | } |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <inheritdoc /> |
| | | 210 | | public object Get(bool triggerOnSpawnedFromPool = true) |
| | 12 | 211 | | { |
| | | 212 | | // Are we empty, but have refills? |
| | 12 | 213 | | if ((InCachedCount == 0 && OutCachedCount < m_MaximumObjects) || |
| | | 214 | | (InCachedCount == 0 && Flags[k_AllowCreateMoreFlag])) |
| | 12 | 215 | | { |
| | 12 | 216 | | CreateItem(); |
| | 12 | 217 | | } |
| | | 218 | | |
| | 12 | 219 | | if (InCachedCount > 0) |
| | 12 | 220 | | { |
| | 12 | 221 | | int targetIndex = InCachedCount - 1; |
| | | 222 | | |
| | | 223 | | // Make sure we don't pull badness |
| | 12 | 224 | | object returnItem = InItems.Array[targetIndex]; |
| | 12 | 225 | | if (returnItem == null) |
| | 0 | 226 | | { |
| | 0 | 227 | | Debug.LogWarning( |
| | | 228 | | $"[ListObjectPool->Get] A null object was pulled from a pool ({m_Key.ToString()})."); |
| | 0 | 229 | | InCachedCount--; |
| | 0 | 230 | | return null; |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | // Handle counters |
| | 12 | 234 | | m_OutItems.AddUnchecked(returnItem); |
| | 12 | 235 | | OutCachedCount++; |
| | 12 | 236 | | InItems.RemoveAt(targetIndex); |
| | 12 | 237 | | InCachedCount--; |
| | | 238 | | |
| | 12 | 239 | | if (triggerOnSpawnedFromPool) |
| | 0 | 240 | | { |
| | 0 | 241 | | spawnedItem?.Invoke(this, returnItem); |
| | 0 | 242 | | } |
| | | 243 | | |
| | 12 | 244 | | return returnItem; |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | if (Flags[k_AllowReuseFlag]) |
| | 0 | 248 | | { |
| | 0 | 249 | | object returnItem = m_OutItems.Array[0]; |
| | 0 | 250 | | if (returnItem == null) |
| | 0 | 251 | | { |
| | 0 | 252 | | Debug.LogWarning( |
| | | 253 | | $"[ListObjectPool->Get] A null object was returned to the object pool ({m_Key.ToString()})."); |
| | 0 | 254 | | return null; |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | returnedItem?.Invoke(this, returnItem); |
| | 0 | 258 | | if (triggerOnSpawnedFromPool) |
| | 0 | 259 | | { |
| | 0 | 260 | | spawnedItem?.Invoke(this, returnItem); |
| | 0 | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | return returnItem; |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | Debug.LogWarning( |
| | | 267 | | $"[ListObjectPool->Get] Hit maximum object cap of {m_MaximumObjects.ToString()} for object pool ({m_Key. |
| | 0 | 268 | | return null; |
| | 12 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <inheritdoc /> |
| | | 272 | | public object GetBaseObject() |
| | 0 | 273 | | { |
| | 0 | 274 | | return BaseObject; |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <inheritdoc /> |
| | | 278 | | public uint GetKey() |
| | 29 | 279 | | { |
| | 29 | 280 | | return m_Key; |
| | 29 | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <inheritdoc /> |
| | | 284 | | public bool HasMinimumPooledItems() |
| | 0 | 285 | | { |
| | 0 | 286 | | return InCachedCount >= m_MinimumObjects; |
| | 0 | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <inheritdoc /> |
| | | 290 | | public bool IsAllowedManagedTearDown() |
| | 0 | 291 | | { |
| | 0 | 292 | | return Flags[k_AllowManagedTeardownFlag]; |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <inheritdoc /> |
| | | 296 | | public bool IsManaged(object item) |
| | 0 | 297 | | { |
| | 0 | 298 | | int outCount = m_OutItems.Count; |
| | 0 | 299 | | for (int i = 0; i < outCount; i++) |
| | 0 | 300 | | { |
| | 0 | 301 | | if (m_OutItems.Array[i] == item) |
| | 0 | 302 | | { |
| | 0 | 303 | | return true; |
| | | 304 | | } |
| | 0 | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | int inCount = InItems.Count; |
| | 0 | 308 | | for (int i = 0; i < inCount; i++) |
| | 0 | 309 | | { |
| | 0 | 310 | | if (InItems.Array[i] == item) |
| | 0 | 311 | | { |
| | 0 | 312 | | return true; |
| | | 313 | | } |
| | 0 | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return false; |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <inheritdoc /> |
| | | 320 | | public bool IsPooled(object item) |
| | 0 | 321 | | { |
| | 0 | 322 | | int inCount = InItems.Count; |
| | 0 | 323 | | for (int i = 0; i < inCount; i++) |
| | 0 | 324 | | { |
| | 0 | 325 | | if (InItems.Array[i] == item) |
| | 0 | 326 | | { |
| | 0 | 327 | | return true; |
| | | 328 | | } |
| | 0 | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | return false; |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <inheritdoc /> |
| | | 335 | | public void Return(object item) |
| | 12 | 336 | | { |
| | | 337 | | // Do we have the interface call? |
| | 12 | 338 | | returnedItem?.Invoke(this, item); |
| | | 339 | | |
| | 12 | 340 | | int outCount = m_OutItems.Count; |
| | 24 | 341 | | for (int i = 0; i < outCount; i++) |
| | 12 | 342 | | { |
| | 12 | 343 | | if (m_OutItems.Array[i] != item) |
| | 0 | 344 | | { |
| | 0 | 345 | | continue; |
| | | 346 | | } |
| | | 347 | | |
| | 12 | 348 | | m_OutItems.RemoveAtSwapBack(i); |
| | 12 | 349 | | OutCachedCount--; |
| | 12 | 350 | | break; |
| | | 351 | | } |
| | | 352 | | |
| | 12 | 353 | | int inCount = InItems.Count; |
| | 24 | 354 | | for (int i = 0; i < inCount; i++) |
| | 0 | 355 | | { |
| | 0 | 356 | | if (InItems.Array[i] == item) |
| | 0 | 357 | | { |
| | 0 | 358 | | return; |
| | | 359 | | } |
| | 0 | 360 | | } |
| | | 361 | | |
| | 12 | 362 | | InItems.AddUnchecked(item); |
| | 12 | 363 | | InCachedCount++; |
| | 12 | 364 | | } |
| | | 365 | | |
| | | 366 | | /// <inheritdoc /> |
| | | 367 | | public void ReturnAll(bool shouldShrink = true) |
| | 0 | 368 | | { |
| | 0 | 369 | | for (int i = OutCachedCount - 1; i >= 0; i--) |
| | 0 | 370 | | { |
| | 0 | 371 | | Return(m_OutItems.Array[i]); |
| | 0 | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | if (shouldShrink && InCachedCount <= m_MaximumObjects) |
| | 0 | 375 | | { |
| | 0 | 376 | | return; |
| | | 377 | | } |
| | | 378 | | |
| | 0 | 379 | | int removeCount = InCachedCount - m_MaximumObjects; |
| | 0 | 380 | | for (int i = 0; i < removeCount; i++) |
| | 0 | 381 | | { |
| | | 382 | | // Trigger specific logic, like Object.Destroy |
| | 0 | 383 | | destroyedItem?.Invoke(InItems.Array[i]); |
| | | 384 | | |
| | | 385 | | // Dereferencing |
| | 0 | 386 | | InItems.RemoveAt(i); |
| | 0 | 387 | | InCachedCount--; |
| | 0 | 388 | | } |
| | 0 | 389 | | } |
| | | 390 | | |
| | | 391 | | /// <inheritdoc /> |
| | | 392 | | public void TearDown() |
| | 14 | 393 | | { |
| | 14 | 394 | | tearingDown?.Invoke(this); |
| | | 395 | | |
| | | 396 | | // Return all items to the pool |
| | 52 | 397 | | for (int i = OutCachedCount - 1; i >= 0; i--) |
| | 12 | 398 | | { |
| | 12 | 399 | | if (m_OutItems.Array[i] != null) |
| | 12 | 400 | | { |
| | 12 | 401 | | Return(m_OutItems.Array[i]); |
| | 12 | 402 | | } |
| | 12 | 403 | | } |
| | | 404 | | |
| | 14 | 405 | | m_OutItems.Clear(); |
| | 14 | 406 | | OutCachedCount = 0; |
| | | 407 | | |
| | | 408 | | // Wipe internals |
| | 52 | 409 | | for (int i = InCachedCount - 1; i >= 0; i--) |
| | 12 | 410 | | { |
| | 12 | 411 | | if (InItems.Array[i] != null) |
| | 12 | 412 | | { |
| | 12 | 413 | | destroyedItem?.Invoke(InItems.Array[i]); |
| | 12 | 414 | | } |
| | 12 | 415 | | } |
| | | 416 | | |
| | 14 | 417 | | InItems.Clear(); |
| | 14 | 418 | | InCachedCount = 0; |
| | | 419 | | |
| | | 420 | | // Unregister |
| | 14 | 421 | | ManagedPools.Unregister(this); |
| | 14 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// The <see cref="SimpleListManagedPool" /> destructor which unregisters itself from <see cref="ManagedPool |
| | | 426 | | /// </summary> |
| | | 427 | | ~SimpleListManagedPool() |
| | 0 | 428 | | { |
| | | 429 | | // Unregister |
| | 0 | 430 | | ManagedPools.Unregister(this); |
| | 0 | 431 | | } |
| | | 432 | | } |
| | | 433 | | } |