| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections.Pool; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A specialized pool for managing <see cref="SwiftDictionary{TKey, TValue}"/> instances, |
| | | 9 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam> |
| | | 12 | | /// <typeparam name="TValue">The type of values in the dictionary.</typeparam> |
| | | 13 | | public sealed class SwiftDictionaryPool<TKey, TValue> : |
| | | 14 | | SwiftCollectionPool<SwiftDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>>, IDisposable |
| | | 15 | | { |
| | | 16 | | #region Singleton Instance |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Shared instance of the dictionary pool, providing a globally accessible pool. |
| | | 20 | | /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal. |
| | | 21 | | /// </summary> |
| | 1 | 22 | | private readonly static SwiftLazyDisposable<SwiftDictionaryPool<TKey, TValue>> _lazyInstance = |
| | 1 | 23 | | new SwiftLazyDisposable<SwiftDictionaryPool<TKey, TValue>>( |
| | 1 | 24 | | () => new SwiftDictionaryPool<TKey, TValue>(), LazyThreadSafetyMode.ExecutionAndPublication |
| | 1 | 25 | | ); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the shared instance of the pool. |
| | | 29 | | /// </summary> |
| | 2 | 30 | | public static SwiftDictionaryPool<TKey, TValue> Shared => _lazyInstance.Value; |
| | | 31 | | |
| | | 32 | | #endregion |
| | | 33 | | |
| | | 34 | | #region Fields |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Tracks whether the pool has been disposed. |
| | | 38 | | /// </summary> |
| | | 39 | | private volatile bool _disposed; |
| | | 40 | | |
| | | 41 | | #endregion |
| | | 42 | | |
| | | 43 | | #region Methods |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Rents a dictionary instance from the pool. If the pool is empty, a new dictionary is created. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>A <see cref="SwiftDictionary{TKey, TValue}"/> instance.</returns> |
| | | 49 | | public override SwiftDictionary<TKey, TValue> Rent() |
| | 7 | 50 | | { |
| | 7 | 51 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>)); |
| | | 52 | | |
| | 6 | 53 | | return base.Rent(); |
| | 6 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Releases a dictionary instance back to the pool for reuse. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="dictionary">The dictionary to release.</param> |
| | | 60 | | /// <remarks> |
| | | 61 | | /// The dictionary will be cleared before being returned to the pool to remove any existing data. |
| | | 62 | | /// </remarks> |
| | | 63 | | public override void Release(SwiftDictionary<TKey, TValue> dictionary) |
| | 3 | 64 | | { |
| | 3 | 65 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>)); |
| | | 66 | | |
| | 3 | 67 | | if (dictionary == null) return; |
| | | 68 | | |
| | 3 | 69 | | base.Release(dictionary); |
| | 3 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Clears all pooled dictionaries from the pool. |
| | | 74 | | /// </summary> |
| | | 75 | | public override void Clear() |
| | 5 | 76 | | { |
| | 5 | 77 | | if (_disposed) return; |
| | | 78 | | |
| | 5 | 79 | | base.Clear(); |
| | 5 | 80 | | } |
| | | 81 | | |
| | | 82 | | #endregion |
| | | 83 | | |
| | | 84 | | #region IDisposable Implementation |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Releases all resources used by the SwiftDictionaryPool. |
| | | 88 | | /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks. |
| | | 89 | | /// </summary> |
| | | 90 | | public void Dispose() |
| | 5 | 91 | | { |
| | 5 | 92 | | if (_disposed) |
| | 1 | 93 | | return; |
| | | 94 | | |
| | 4 | 95 | | Clear(); |
| | 4 | 96 | | base.Flush(); |
| | | 97 | | |
| | 4 | 98 | | _disposed = true; |
| | | 99 | | |
| | | 100 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 4 | 101 | | GC.SuppressFinalize(this); |
| | 5 | 102 | | } |
| | | 103 | | |
| | 3 | 104 | | ~SwiftDictionaryPool() => Dispose(); |
| | | 105 | | |
| | | 106 | | #endregion |
| | | 107 | | } |