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