< 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: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 130
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
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%
.ctor()100%66100%
get_CollectionPool()100%11100%
Rent()100%11100%
Get(...)100%11100%
Release(...)100%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 SwiftLazyDisposable<SwiftObjectPool<SwiftSparseMap<T>>> _lazyCollectionPool =
 532        new(() =>
 433        {
 434            return new SwiftObjectPool<SwiftSparseMap<T>>(
 535                createFunc: () => new SwiftSparseMap<T>(),
 436                actionOnRelease: map => map.Clear()
 437            );
 938        });
 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()
 763    {
 764        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 65
 666        return CollectionPool.Rent();
 667    }
 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)
 175    {
 176        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 77
 178        return CollectionPool.Rent(out value);
 179    }
 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)
 386    {
 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()
 698    {
 699        if (_disposed || !_lazyCollectionPool.IsValueCreated)
 1100            return;
 101
 5102        _lazyCollectionPool.Value.Clear();
 6103    }
 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()
 5113    {
 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);
 5125    }
 126
 3127    ~SwiftSparseMapPool() => Dispose();
 128
 129    #endregion
 130}