< 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: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 123
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
 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
 8using SwiftCollections.Diagnostics;
 9using SwiftCollections.Lazy;
 10using System;
 11using System.Collections.Generic;
 12using System.Threading;
 13
 14namespace 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>
 22public 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>
 131    private readonly static SwiftLazyDisposable<SwiftDictionaryPool<TKey, TValue>> _lazyInstance =
 132        new(
 133                () => new SwiftDictionaryPool<TKey, TValue>(), LazyThreadSafetyMode.ExecutionAndPublication
 134            );
 35
 36    /// <summary>
 37    /// Gets the shared instance of the pool.
 38    /// </summary>
 239    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    {
 760        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>));
 61
 662        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    {
 574        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftDictionaryPool<TKey, TValue>));
 75
 576        if (dictionary == null) return;
 77
 378        base.Release(dictionary);
 379    }
 80
 81    /// <summary>
 82    /// Clears all pooled dictionaries from the pool.
 83    /// </summary>
 84    public override void Clear()
 85    {
 886        if (_disposed) return;
 87
 688        base.Clear();
 689    }
 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    {
 6101        if (_disposed)
 1102            return;
 103
 5104        Clear();
 5105        base.Flush();
 106
 5107        _disposed = true;
 108
 109        // Suppress finalization to prevent unnecessary GC overhead
 5110        GC.SuppressFinalize(this);
 5111    }
 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>
 3120    ~SwiftDictionaryPool() => Dispose();
 121
 122    #endregion
 123}