| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections.Pool; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A generic object pooling class designed to efficiently reuse objects, reducing memory allocation overhead |
| | | 10 | | /// and improving performance. Provides thread-safe operations for creating, renting, and releasing objects. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="T">The type of object to pool. Must be a reference type.</typeparam> |
| | | 13 | | public sealed class SwiftObjectPool<T> : IDisposable, ISwiftObjectPool<T> where T : class |
| | | 14 | | { |
| | | 15 | | #region Fields |
| | | 16 | | |
| | | 17 | | private readonly ConcurrentStack<T> _pool; |
| | | 18 | | private readonly Func<T> _createFunc; |
| | | 19 | | private readonly Action<T> _actionOnGet; |
| | | 20 | | private readonly Action<T> _actionOnRelease; |
| | | 21 | | private readonly Action<T> _actionOnDestroy; |
| | | 22 | | private readonly int _maxSize; |
| | | 23 | | |
| | | 24 | | private volatile bool _disposed; |
| | | 25 | | |
| | | 26 | | #endregion |
| | | 27 | | |
| | | 28 | | #region Constructor |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="SwiftObjectPool{T}"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="createFunc">A function used to create new instances of the object type.</param> |
| | | 34 | | /// <param name="actionOnGet">An optional action to perform when an object is rented from the pool.</param> |
| | | 35 | | /// <param name="actionOnRelease">An optional action to perform when an object is returned to the pool.</param> |
| | | 36 | | /// <param name="actionOnDestroy">An optional action to perform when an object is destroyed due to pool size constra |
| | | 37 | | /// <param name="maxSize">The maximum number of objects the pool can hold.</param> |
| | | 38 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="createFunc"/> is null.</exception> |
| | | 39 | | /// <exception cref="ArgumentException">Thrown if <paramref name="maxSize"/> is less than or equal to 0.</exception> |
| | 46 | 40 | | public SwiftObjectPool( |
| | 46 | 41 | | Func<T> createFunc, |
| | 46 | 42 | | Action<T> actionOnGet = null, |
| | 46 | 43 | | Action<T> actionOnRelease = null, |
| | 46 | 44 | | Action<T> actionOnDestroy = null, |
| | 46 | 45 | | int maxSize = 100) |
| | 46 | 46 | | { |
| | 46 | 47 | | SwiftThrowHelper.ThrowIfNull(createFunc, nameof(createFunc)); |
| | 46 | 48 | | SwiftThrowHelper.ThrowIfNegativeOrZero(maxSize, nameof(maxSize)); |
| | | 49 | | |
| | 46 | 50 | | _pool = new ConcurrentStack<T>(); |
| | 46 | 51 | | _createFunc = createFunc; |
| | 46 | 52 | | _actionOnGet = actionOnGet; |
| | 46 | 53 | | _actionOnRelease = actionOnRelease; |
| | 46 | 54 | | _actionOnDestroy = actionOnDestroy; |
| | 46 | 55 | | _maxSize = maxSize; |
| | 46 | 56 | | } |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Properties |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the total number of objects created by the pool, including both active and inactive objects. |
| | | 64 | | /// </summary> |
| | 139 | 65 | | public int CountAll { get; private set; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the number of objects currently in use (rented from the pool). |
| | | 69 | | /// </summary> |
| | 4 | 70 | | public int CountActive => CountAll - CountInactive; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the number of objects currently available in the pool for rent. |
| | | 74 | | /// </summary> |
| | 7 | 75 | | public int CountInactive => _pool.Count; |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | #region Collection Manipulation |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Rents an object from the pool. If the pool is empty, a new object is created using the factory function. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns>An instance of <typeparamref name="T"/>.</returns> |
| | | 85 | | /// <exception cref="InvalidOperationException">Thrown if object creation fails.</exception> |
| | | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 87 | | public T Rent() |
| | 66 | 88 | | { |
| | 66 | 89 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftObjectPool<T>)); |
| | | 90 | | |
| | 65 | 91 | | if (_pool.TryPop(out var obj)) |
| | 12 | 92 | | { |
| | 12 | 93 | | _actionOnGet?.Invoke(obj); |
| | 12 | 94 | | return obj; |
| | | 95 | | } |
| | | 96 | | |
| | 53 | 97 | | var newObj = _createFunc(); |
| | 54 | 98 | | if (newObj == null) throw new InvalidOperationException("Failed to create a new object."); |
| | 52 | 99 | | CountAll++; |
| | 52 | 100 | | _actionOnGet?.Invoke(newObj); |
| | 52 | 101 | | return newObj; |
| | 64 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Rents an object from the pool and wraps it in a <see cref="SwiftPooledObject{T}"/> for automatic release. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="value">The rented object.</param> |
| | | 108 | | /// <returns>A <see cref="SwiftPooledObject{T}"/> instance wrapping the rented object.</returns> |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 110 | | public SwiftPooledObject<T> Rent(out T value) => new SwiftPooledObject<T>(value = Rent(), this); |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Releases an object back to the pool for reuse. If the pool has reached its maximum size, the object |
| | | 114 | | /// is destroyed using the configured destroy action. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="element">The object to release.</param> |
| | | 117 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="element"/> is null.</exception> |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 119 | | public void Release(T element) |
| | 37 | 120 | | { |
| | 37 | 121 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftObjectPool<T>)); |
| | 37 | 122 | | SwiftThrowHelper.ThrowIfNull(element, nameof(element)); |
| | | 123 | | |
| | 36 | 124 | | _actionOnRelease?.Invoke(element); |
| | | 125 | | |
| | 36 | 126 | | if (_pool.Count < _maxSize) |
| | 35 | 127 | | _pool.Push(element); |
| | | 128 | | else |
| | 1 | 129 | | { |
| | 1 | 130 | | _actionOnDestroy?.Invoke(element); |
| | 1 | 131 | | CountAll--; |
| | 1 | 132 | | } |
| | 36 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Releases all objects back to the pool for reuse. If the pool has reached its maximum size, objects |
| | | 137 | | /// are destroyed using the configured destroy action. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="elements"></param> |
| | | 140 | | /// <exception cref="ArgumentNullException">Thrown if any object in <paramref name="elements"/> is null.</exception> |
| | | 141 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 142 | | public void Release(IEnumerable<T> elements) |
| | 1 | 143 | | { |
| | 1 | 144 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftObjectPool<T>)); |
| | | 145 | | |
| | 7 | 146 | | foreach (T item in elements) |
| | 2 | 147 | | Release(item); |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Clears all objects from the pool, destroying any active objects if a destroy action is configured. |
| | | 152 | | /// </summary> |
| | | 153 | | public void Clear() |
| | 36 | 154 | | { |
| | 47 | 155 | | if (_disposed) return; |
| | | 156 | | |
| | 42 | 157 | | while (_pool.TryPop(out var obj)) |
| | 17 | 158 | | _actionOnDestroy?.Invoke(obj); |
| | | 159 | | |
| | 25 | 160 | | CountAll = 0; |
| | 36 | 161 | | } |
| | | 162 | | |
| | | 163 | | #endregion |
| | | 164 | | |
| | | 165 | | #region IDisposable Implementation |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Releases all resources used by the SwiftObjectPool. |
| | | 169 | | /// It is important to call Dispose() to release pooled arrays, preventing potential memory leaks. |
| | | 170 | | /// </summary> |
| | | 171 | | public void Dispose() |
| | 91 | 172 | | { |
| | 91 | 173 | | if (_disposed) |
| | 45 | 174 | | return; |
| | | 175 | | |
| | 46 | 176 | | _pool.Clear(); |
| | | 177 | | |
| | 46 | 178 | | _disposed = true; |
| | | 179 | | |
| | | 180 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 46 | 181 | | GC.SuppressFinalize(this); |
| | 91 | 182 | | } |
| | | 183 | | |
| | 26 | 184 | | ~SwiftObjectPool() => Dispose(); |
| | | 185 | | |
| | | 186 | | #endregion |
| | | 187 | | } |