< Summary

Information
Class: SwiftCollections.Pool.SwiftSparseMapPool<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftSparseMapPool.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 137
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
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%
.ctor()100%66100%
get_CollectionPool()100%11100%
Rent()100%11100%
Get(...)100%11100%
Release(...)50%22100%
Clear()100%44100%
Dispose()100%44100%
Finalize()100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace SwiftCollections.Pool;
 5
 6/// <summary>
 7/// A specialized pool for managing <see cref="SwiftSparseMap{T}"/> instances,
 8/// providing efficient reuse to minimize memory allocations and improve performance.
 9/// </summary>
 10/// <typeparam name="T">The type of values stored in the sparse map.</typeparam>
 11public sealed class SwiftSparseMapPool<T> : IDisposable
 12{
 13    #region Singleton Instance
 14
 15    /// <summary>
 16    /// Shared instance of the sparse map pool, providing a globally accessible pool.
 17    /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal.
 18    /// </summary>
 119    private readonly static SwiftLazyDisposable<SwiftSparseMapPool<T>> _lazyInstance =
 220        new(() => new SwiftSparseMapPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 21
 22    /// <summary>
 23    /// Gets the shared instance of the pool.
 24    /// </summary>
 225    public static SwiftSparseMapPool<T> Shared => _lazyInstance.Value;
 26
 27    #endregion
 28
 29    #region Fields
 30
 531    private readonly SwiftLazyDisposable<SwiftObjectPool<SwiftSparseMap<T>>> _lazyCollectionPool =
 532        new(() =>
 533        {
 434            return new SwiftObjectPool<SwiftSparseMap<T>>(
 535                createFunc: () => new SwiftSparseMap<T>(),
 436                actionOnRelease: map => map.Clear()
 437            );
 538        });
 39
 40    /// <summary>
 41    /// Tracks whether the pool has been disposed.
 42    /// </summary>
 43    private volatile bool _disposed;
 44
 45    #endregion
 46
 47    #region Properties
 48
 49    /// <summary>
 50    /// Gets the underlying object pool used to manage sparse map instances.
 51    /// </summary>
 1052    public SwiftObjectPool<SwiftSparseMap<T>> CollectionPool => _lazyCollectionPool.Value;
 53
 54    #endregion
 55
 56    #region Methods
 57
 58    /// <summary>
 59    /// Rents a sparse map instance from the pool. If the pool is empty, a new sparse map is created.
 60    /// </summary>
 61    /// <returns>A <see cref="SwiftSparseMap{T}"/> instance.</returns>
 62    public SwiftSparseMap<T> Rent()
 63    {
 764        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 65
 666        return CollectionPool.Rent();
 67    }
 68
 69    /// <summary>
 70    /// Rents a sparse map from the pool and wraps it in a <see cref="SwiftPooledObject{T}"/> for automatic release.
 71    /// </summary>
 72    /// <param name="value">The rented sparse map.</param>
 73    /// <returns>A <see cref="SwiftPooledObject{T}"/> instance wrapping the rented sparse map.</returns>
 74    public SwiftPooledObject<SwiftSparseMap<T>> Get(out SwiftSparseMap<T> value)
 75    {
 176        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 77
 178        return CollectionPool.Rent(out value);
 79    }
 80
 81    /// <summary>
 82    /// Releases a sparse map instance back to the pool for reuse.
 83    /// </summary>
 84    /// <param name="map">The sparse map to release.</param>
 85    public void Release(SwiftSparseMap<T> map)
 86    {
 387        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 88
 389        if (map == null) return;
 90
 391        CollectionPool.Release(map);
 392    }
 93
 94    /// <summary>
 95    /// Clears all pooled sparse maps from the pool.
 96    /// </summary>
 97    public void Clear()
 98    {
 699        if (_disposed || !_lazyCollectionPool.IsValueCreated)
 1100            return;
 101
 5102        _lazyCollectionPool.Value.Clear();
 5103    }
 104
 105    #endregion
 106
 107    #region IDisposable Implementation
 108
 109    /// <summary>
 110    /// Releases all resources used by the SwiftSparseMapPool.
 111    /// </summary>
 112    public void Dispose()
 113    {
 5114        if (_disposed)
 1115            return;
 116
 4117        Clear();
 118
 4119        if (_lazyCollectionPool.IsValueCreated)
 4120            _lazyCollectionPool.Value.Dispose();
 121
 4122        _disposed = true;
 123
 4124        GC.SuppressFinalize(this);
 4125    }
 126
 127    /// <summary>
 128    /// Releases the resources used by the SwiftSparseMapPool instance.
 129    /// </summary>
 130    /// <remarks>
 131    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 132    /// It is recommended to call Dispose to release resources deterministically.
 133    /// </remarks>
 3134    ~SwiftSparseMapPool() => Dispose();
 135
 136    #endregion
 137}