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