| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A generic pool for managing collections such as <see cref="List{T}"/>, <see cref="HashSet{T}"/>, |
| | | 8 | | /// <see cref="Dictionary{TKey, TValue}"/>, and other types implementing <see cref="ICollection{T}"/>. |
| | | 9 | | /// Provides efficient reuse of collection instances to reduce memory allocations. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="TCollection">The type of the collection being pooled. Must implement <see cref="ICollection{TItem}" |
| | | 12 | | /// <typeparam name="TItem">The type of items contained in the collection.</typeparam> |
| | | 13 | | public abstract class SwiftCollectionPool<TCollection, TItem> where TCollection : class, ICollection<TItem>, new() |
| | | 14 | | { |
| | | 15 | | #region Singleton Instances |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Internal object pool for managing the lifecycle of pooled collections. |
| | | 19 | | /// Uses <see cref="Lazy{T}"/> to ensure lazy initialization. |
| | | 20 | | /// </summary> |
| | 30 | 21 | | private SwiftLazyDisposable<SwiftObjectPool<TCollection>> _lazyCollectionPool = |
| | 30 | 22 | | new SwiftLazyDisposable<SwiftObjectPool<TCollection>>(() => |
| | 22 | 23 | | { |
| | 22 | 24 | | return new SwiftObjectPool<TCollection>( |
| | 27 | 25 | | createFunc: () => new TCollection(), |
| | 18 | 26 | | actionOnRelease: collection => collection.Clear() |
| | 22 | 27 | | ); |
| | 52 | 28 | | }); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the shared instance of the pool. |
| | | 32 | | /// </summary> |
| | 51 | 33 | | public SwiftObjectPool<TCollection> CollectionPool => _lazyCollectionPool.Value; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Methods |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Rents a collection from the pool. If the pool is empty, a new collection is created. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <returns>A collection instance of type <typeparamref name="TCollection"/>.</returns> |
| | | 43 | | public virtual TCollection Rent() |
| | 33 | 44 | | { |
| | 33 | 45 | | return CollectionPool.Rent(); |
| | 33 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Rents a collection from the pool and wraps it in a <see cref="SwiftPooledObject{TCollection}"/> for automatic re |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="value">The rented collection.</param> |
| | | 52 | | /// <returns>A <see cref="SwiftPooledObject{TCollection}"/> instance wrapping the rented collection.</returns> |
| | | 53 | | public virtual SwiftPooledObject<TCollection> Get(out TCollection value) |
| | 1 | 54 | | { |
| | 1 | 55 | | return CollectionPool.Rent(out value); |
| | 1 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Releases a collection back to the pool for reuse. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="toRelease">The collection to release.</param> |
| | | 62 | | public virtual void Release(TCollection toRelease) |
| | 17 | 63 | | { |
| | 17 | 64 | | CollectionPool.Release(toRelease); |
| | 17 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Clears all collections from the pool. |
| | | 69 | | /// </summary> |
| | | 70 | | public virtual void Clear() |
| | 30 | 71 | | { |
| | 30 | 72 | | if (_lazyCollectionPool.IsValueCreated) |
| | 27 | 73 | | _lazyCollectionPool.Value.Clear(); |
| | 30 | 74 | | } |
| | | 75 | | |
| | | 76 | | #endregion |
| | | 77 | | |
| | | 78 | | #region IDisposable Implementation |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Releases all resources used by the SwiftCollectionPool. |
| | | 82 | | /// </summary> |
| | | 83 | | public virtual void Flush() |
| | 26 | 84 | | { |
| | 26 | 85 | | if (_lazyCollectionPool.IsValueCreated) |
| | 23 | 86 | | { |
| | 23 | 87 | | _lazyCollectionPool.Value.Dispose(); |
| | | 88 | | |
| | 23 | 89 | | _lazyCollectionPool = new SwiftLazyDisposable<SwiftObjectPool<TCollection>>(() => |
| | 1 | 90 | | { |
| | 1 | 91 | | return new SwiftObjectPool<TCollection>( |
| | 1 | 92 | | createFunc: () => new TCollection(), |
| | 0 | 93 | | actionOnRelease: collection => collection.Clear() |
| | 1 | 94 | | ); |
| | 24 | 95 | | }); |
| | 23 | 96 | | } |
| | | 97 | | |
| | 26 | 98 | | } |
| | | 99 | | |
| | | 100 | | #endregion |
| | | 101 | | } |