| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A specialized pool for managing <see cref="SwiftSparseMap{T}"/> instances, |
| | | 8 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="T">The type of values stored in the sparse map.</typeparam> |
| | | 11 | | public sealed class SwiftSparseMapPool<T> : IDisposable |
| | | 12 | | { |
| | | 13 | | #region Singleton Instance |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared instance of the sparse map 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<SwiftSparseMapPool<T>> _lazyInstance = |
| | 2 | 20 | | new(() => new SwiftSparseMapPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the shared instance of the pool. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static SwiftSparseMapPool<T> Shared => _lazyInstance.Value; |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Fields |
| | | 30 | | |
| | 5 | 31 | | private SwiftLazyDisposable<SwiftObjectPool<SwiftSparseMap<T>>> _lazyCollectionPool = |
| | 5 | 32 | | new(() => |
| | 4 | 33 | | { |
| | 4 | 34 | | return new SwiftObjectPool<SwiftSparseMap<T>>( |
| | 5 | 35 | | createFunc: () => new SwiftSparseMap<T>(), |
| | 4 | 36 | | actionOnRelease: map => map.Clear() |
| | 4 | 37 | | ); |
| | 9 | 38 | | }); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Tracks whether the pool has been disposed. |
| | | 42 | | /// </summary> |
| | | 43 | | private volatile bool _disposed; |
| | | 44 | | |
| | | 45 | | #endregion |
| | | 46 | | |
| | | 47 | | #region Properties |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the underlying object pool used to manage sparse map instances. |
| | | 51 | | /// </summary> |
| | 10 | 52 | | public SwiftObjectPool<SwiftSparseMap<T>> CollectionPool => _lazyCollectionPool.Value; |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region Methods |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Rents a sparse map instance from the pool. If the pool is empty, a new sparse map is created. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <returns>A <see cref="SwiftSparseMap{T}"/> instance.</returns> |
| | | 62 | | public SwiftSparseMap<T> Rent() |
| | 7 | 63 | | { |
| | 7 | 64 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>)); |
| | | 65 | | |
| | 6 | 66 | | return CollectionPool.Rent(); |
| | 6 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Rents a sparse map from the pool and wraps it in a <see cref="SwiftPooledObject{T}"/> for automatic release. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="value">The rented sparse map.</param> |
| | | 73 | | /// <returns>A <see cref="SwiftPooledObject{T}"/> instance wrapping the rented sparse map.</returns> |
| | | 74 | | public SwiftPooledObject<SwiftSparseMap<T>> Get(out SwiftSparseMap<T> value) |
| | 1 | 75 | | { |
| | 1 | 76 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>)); |
| | | 77 | | |
| | 1 | 78 | | return CollectionPool.Rent(out value); |
| | 1 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Releases a sparse map instance back to the pool for reuse. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="map">The sparse map to release.</param> |
| | | 85 | | public void Release(SwiftSparseMap<T> map) |
| | 3 | 86 | | { |
| | 3 | 87 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>)); |
| | | 88 | | |
| | 3 | 89 | | if (map == null) return; |
| | | 90 | | |
| | 3 | 91 | | CollectionPool.Release(map); |
| | 3 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Clears all pooled sparse maps from the pool. |
| | | 96 | | /// </summary> |
| | | 97 | | public void Clear() |
| | 6 | 98 | | { |
| | 6 | 99 | | if (_disposed || !_lazyCollectionPool.IsValueCreated) |
| | 1 | 100 | | return; |
| | | 101 | | |
| | 5 | 102 | | _lazyCollectionPool.Value.Clear(); |
| | 6 | 103 | | } |
| | | 104 | | |
| | | 105 | | #endregion |
| | | 106 | | |
| | | 107 | | #region IDisposable Implementation |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Releases all resources used by the SwiftSparseMapPool. |
| | | 111 | | /// </summary> |
| | | 112 | | public void Dispose() |
| | 5 | 113 | | { |
| | 5 | 114 | | if (_disposed) |
| | 1 | 115 | | return; |
| | | 116 | | |
| | 4 | 117 | | Clear(); |
| | | 118 | | |
| | 4 | 119 | | if (_lazyCollectionPool.IsValueCreated) |
| | 4 | 120 | | _lazyCollectionPool.Value.Dispose(); |
| | | 121 | | |
| | 4 | 122 | | _disposed = true; |
| | | 123 | | |
| | 4 | 124 | | GC.SuppressFinalize(this); |
| | 5 | 125 | | } |
| | | 126 | | |
| | 3 | 127 | | ~SwiftSparseMapPool() => Dispose(); |
| | | 128 | | |
| | | 129 | | #endregion |
| | | 130 | | } |