| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections.Pool; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A thread-safe pool designed to manage arrays of a specific size. |
| | | 10 | | /// The pool optimizes memory usage by reusing arrays, reducing allocations and improving performance. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="T">The type of elements in the arrays being pooled. Must have a parameterless constructor.</typepar |
| | | 13 | | public sealed class SwiftArrayPool<T> : IDisposable where T : new() |
| | | 14 | | { |
| | | 15 | | #region Singleton Instance |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A lazily initialized singleton instance of the array pool. |
| | | 19 | | /// </summary> |
| | 1 | 20 | | private static readonly SwiftLazyDisposable<SwiftArrayPool<T>> _instance = |
| | 2 | 21 | | new SwiftLazyDisposable<SwiftArrayPool<T>>(() => new SwiftArrayPool<T>(), LazyThreadSafetyMode.ExecutionAndPubli |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the shared instance of the pool. |
| | | 25 | | /// </summary> |
| | 2 | 26 | | public static SwiftArrayPool<T> Shared => _instance.Value; |
| | | 27 | | |
| | | 28 | | #endregion |
| | | 29 | | |
| | | 30 | | #region Fields |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// A collection of object pools, keyed by the size of the arrays they manage. |
| | | 34 | | /// </summary> |
| | | 35 | | private readonly ConcurrentDictionary<int, SwiftObjectPool<T[]>> _sizePools; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Tracks whether the pool has been disposed. |
| | | 39 | | /// </summary> |
| | | 40 | | private volatile bool _disposed; |
| | | 41 | | |
| | | 42 | | #endregion |
| | | 43 | | |
| | | 44 | | #region Constructor |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a new instance of the <see cref="SwiftArrayPool{T}"/> class with customizable behavior. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="createFunc">A function used to create new arrays (default: creates arrays of the specified size).</ |
| | | 50 | | /// <param name="actionOnRelease">An action performed when an array is released back to the pool (default: clears th |
| | | 51 | | /// <param name="actionOnDestroy">An action performed when an array is removed from the pool (default: no action).</ |
| | | 52 | | /// <param name="poolMaxCapacity">The maximum number of arrays each pool can hold for a specific size (default: 100) |
| | 13 | 53 | | public SwiftArrayPool( |
| | 13 | 54 | | Func<int, T[]> createFunc = null, |
| | 13 | 55 | | Action<T[]> actionOnRelease = null, |
| | 13 | 56 | | Action<T[]> actionOnDestroy = null, |
| | 13 | 57 | | int poolMaxCapacity = 100) |
| | 13 | 58 | | { |
| | 13 | 59 | | SwiftThrowHelper.ThrowIfNegativeOrZero(poolMaxCapacity, nameof(poolMaxCapacity)); |
| | | 60 | | |
| | 13 | 61 | | _sizePools = new ConcurrentDictionary<int, SwiftObjectPool<T[]>>(); |
| | 13 | 62 | | PoolMaxCapacity = poolMaxCapacity; |
| | | 63 | | |
| | 24 | 64 | | CreateFunc = createFunc ?? (size => new T[size]); |
| | 13 | 65 | | ActionOnRelease = actionOnRelease ?? (array => |
| | 19 | 66 | | Array.Clear(array, 0, array.Length)); |
| | 13 | 67 | | ActionOnDestroy = actionOnDestroy; |
| | 13 | 68 | | } |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Properties |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the function used to create new arrays. |
| | | 76 | | /// </summary> |
| | 12 | 77 | | public Func<int, T[]> CreateFunc { get; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the action performed when an array is released back to the pool. |
| | | 81 | | /// </summary> |
| | 12 | 82 | | public Action<T[]> ActionOnRelease { get; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets the action performed when an array is removed from the pool. |
| | | 86 | | /// </summary> |
| | 12 | 87 | | public Action<T[]> ActionOnDestroy { get; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the maximum number of arrays each pool can hold for a specific size. |
| | | 91 | | /// </summary> |
| | 12 | 92 | | public int PoolMaxCapacity { get; } |
| | | 93 | | |
| | | 94 | | #endregion |
| | | 95 | | |
| | | 96 | | #region Collection Manipulation |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Rents an array of the specified size from the pool. If no pool exists for the size, a new one is created. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="size">The desired size of the array.</param> |
| | | 102 | | /// <returns>An array of the specified size, either newly created or retrieved from the pool.</returns> |
| | | 103 | | /// <exception cref="ArgumentException">Thrown if the specified size is less than or equal to 0.</exception> |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public T[] Rent(int size) |
| | 18 | 106 | | { |
| | 18 | 107 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftArrayPool<T>)); |
| | 16 | 108 | | SwiftThrowHelper.ThrowIfNegativeOrZero(size, nameof(size)); |
| | | 109 | | |
| | 26 | 110 | | return _sizePools.GetOrAdd(size, key => CreatePoolForSize(key)).Rent(); |
| | 14 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Releases an array back to the pool for reuse. If no pool exists for the array's size, it is cleared and discarde |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="array">The array to release back to the pool.</param> |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 118 | | public void Release(T[] array) |
| | 12 | 119 | | { |
| | 12 | 120 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftArrayPool<T>)); |
| | | 121 | | |
| | 12 | 122 | | if (array == null || array.Length == 0) return; |
| | | 123 | | |
| | 8 | 124 | | if (_sizePools.TryGetValue(array.Length, out var pool)) |
| | 7 | 125 | | pool.Release(array); |
| | | 126 | | else |
| | 1 | 127 | | Array.Clear(array, 0, array.Length); // Handle large or unusual sizes by discarding |
| | 10 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Clears all object pools, releasing any pooled arrays and resetting the state. |
| | | 132 | | /// </summary> |
| | | 133 | | public void Clear() |
| | 2 | 134 | | { |
| | 2 | 135 | | if (_disposed) return; |
| | | 136 | | |
| | 12 | 137 | | foreach (SwiftObjectPool<T[]> pool in _sizePools.Values) |
| | 3 | 138 | | pool.Clear(); |
| | | 139 | | |
| | 2 | 140 | | _sizePools.Clear(); |
| | 2 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Creates a new object pool for managing arrays of the specified size. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="size">The size of arrays to be managed by the pool.</param> |
| | | 147 | | /// <returns>A new instance of <see cref="SwiftObjectPool{T}"/> for managing arrays of the specified size.</returns> |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 149 | | private SwiftObjectPool<T[]> CreatePoolForSize(int size) |
| | 12 | 150 | | { |
| | 12 | 151 | | return new SwiftObjectPool<T[]>( |
| | 12 | 152 | | createFunc: () => CreateFunc(size), |
| | 12 | 153 | | actionOnRelease: ActionOnRelease, |
| | 12 | 154 | | actionOnDestroy: ActionOnDestroy, |
| | 12 | 155 | | maxSize: PoolMaxCapacity |
| | 12 | 156 | | ); |
| | 12 | 157 | | } |
| | | 158 | | |
| | | 159 | | #endregion |
| | | 160 | | |
| | | 161 | | #region IDisposable Implementation |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Releases all resources used by the SwiftArrayPool. |
| | | 165 | | /// It is important to call Dispose() to release pooled arrays, preventing potential memory leaks. |
| | | 166 | | /// </summary> |
| | | 167 | | public void Dispose() |
| | 12 | 168 | | { |
| | 12 | 169 | | if (_disposed) return; |
| | | 170 | | |
| | 12 | 171 | | _disposed = true; |
| | 54 | 172 | | foreach (SwiftObjectPool<T[]> pool in _sizePools.Values) |
| | 9 | 173 | | pool.Dispose(); |
| | 12 | 174 | | _sizePools.Clear(); |
| | | 175 | | |
| | | 176 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 12 | 177 | | GC.SuppressFinalize(this); |
| | 12 | 178 | | } |
| | | 179 | | |
| | 10 | 180 | | ~SwiftArrayPool() => Dispose(); |
| | | 181 | | |
| | | 182 | | #endregion |
| | | 183 | | } |