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