| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A specialized pool for managing <see cref="SwiftList{T}"/> instances, |
| | | 8 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 11 | | public sealed class SwiftListPool<T> : SwiftCollectionPool<SwiftList<T>, T>, IDisposable |
| | | 12 | | { |
| | | 13 | | #region Singleton Instance |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared instance of the hash set pool, providing a globally accessible pool. |
| | | 17 | | /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal. |
| | | 18 | | /// </summary> |
| | 1 | 19 | | private readonly static SwiftLazyDisposable<SwiftListPool<T>> _lazyInstance = |
| | 2 | 20 | | new SwiftLazyDisposable<SwiftListPool<T>>(() => new SwiftListPool<T>(), LazyThreadSafetyMode.ExecutionAndPublica |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the shared instance of the pool. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static SwiftListPool<T> Shared => _lazyInstance.Value; |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Fields |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Tracks whether the pool has been disposed. |
| | | 33 | | /// </summary> |
| | | 34 | | private volatile bool _disposed; |
| | | 35 | | |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | #region Methods |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Rents a list instance from the pool. If the pool is empty, a new list is created. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <returns>A <see cref="SwiftList{T}"/> instance.</returns> |
| | | 44 | | public override SwiftList<T> Rent() |
| | 7 | 45 | | { |
| | 7 | 46 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftList<T>)); |
| | | 47 | | |
| | 6 | 48 | | return base.Rent(); |
| | 6 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Releases a list instance back to the pool for reuse. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="list">The list to release.</param> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// The list will be cleared before being returned to the pool to ensure it contains no stale data. |
| | | 57 | | /// </remarks> |
| | | 58 | | public override void Release(SwiftList<T> list) |
| | 3 | 59 | | { |
| | 3 | 60 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftList<T>)); |
| | | 61 | | |
| | 3 | 62 | | if (list == null) return; |
| | | 63 | | |
| | | 64 | | // Ensure the list is clear before returning it to the pool |
| | 3 | 65 | | list.Clear(); |
| | 3 | 66 | | base.Release(list); |
| | 3 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Clears all pooled lists from the pool. |
| | | 71 | | /// </summary> |
| | | 72 | | public override void Clear() |
| | 7 | 73 | | { |
| | 7 | 74 | | if (_disposed) return; |
| | | 75 | | |
| | 7 | 76 | | base.Clear(); |
| | 7 | 77 | | } |
| | | 78 | | |
| | | 79 | | #endregion |
| | | 80 | | |
| | | 81 | | #region IDisposable Implementation |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Releases all resources used by the SwiftListPool. |
| | | 85 | | /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks. |
| | | 86 | | /// </summary> |
| | | 87 | | public void Dispose() |
| | 6 | 88 | | { |
| | 6 | 89 | | if (_disposed) |
| | 1 | 90 | | return; |
| | | 91 | | |
| | 5 | 92 | | Clear(); |
| | 5 | 93 | | base.Flush(); |
| | | 94 | | |
| | 5 | 95 | | _disposed = true; |
| | | 96 | | |
| | | 97 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 5 | 98 | | GC.SuppressFinalize(this); |
| | 6 | 99 | | } |
| | | 100 | | |
| | 4 | 101 | | ~SwiftListPool() => Dispose(); |
| | | 102 | | |
| | | 103 | | #endregion |
| | | 104 | | } |