< Summary

Information
Class: SwiftCollections.Pool.SwiftDictionaryPool<T1, T2>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftDictionaryPool.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 107
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Shared()100%11100%
Rent()100%11100%
Release(...)100%22100%
Clear()100%22100%
Dispose()100%22100%
Finalize()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftDictionaryPool.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4
 5namespace 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>
 13public 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>
 122    private readonly static SwiftLazyDisposable<SwiftDictionaryPool<TKey, TValue>> _lazyInstance =
 123        new SwiftLazyDisposable<SwiftDictionaryPool<TKey, TValue>>(
 124                () => new SwiftDictionaryPool<TKey, TValue>(), LazyThreadSafetyMode.ExecutionAndPublication
 125            );
 26
 27    /// <summary>
 28    /// Gets the shared instance of the pool.
 29    /// </summary>
 230    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()
 750    {
 751        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>));
 52
 653        return base.Rent();
 654    }
 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)
 364    {
 365        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>));
 66
 367        if (dictionary == null) return;
 68
 369        base.Release(dictionary);
 370    }
 71
 72    /// <summary>
 73    /// Clears all pooled dictionaries from the pool.
 74    /// </summary>
 75    public override void Clear()
 576    {
 577        if (_disposed) return;
 78
 579        base.Clear();
 580    }
 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()
 591    {
 592        if (_disposed)
 193            return;
 94
 495        Clear();
 496        base.Flush();
 97
 498        _disposed = true;
 99
 100        // Suppress finalization to prevent unnecessary GC overhead
 4101        GC.SuppressFinalize(this);
 5102    }
 103
 3104    ~SwiftDictionaryPool() => Dispose();
 105
 106    #endregion
 107}