< 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: 146
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%22100%
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
 1//=======================================================================
 2// SwiftSparseMapPool.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.Threading;
 12
 13namespace SwiftCollections.Pool;
 14
 15/// <summary>
 16/// A specialized pool for managing <see cref="SwiftSparseMap{T}"/> instances,
 17/// providing efficient reuse to minimize memory allocations and improve performance.
 18/// </summary>
 19/// <typeparam name="T">The type of values stored in the sparse map.</typeparam>
 20public sealed class SwiftSparseMapPool<T> : IDisposable
 21{
 22    #region Singleton Instance
 23
 24    /// <summary>
 25    /// Shared instance of the sparse map pool, providing a globally accessible pool.
 26    /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal.
 27    /// </summary>
 128    private readonly static SwiftLazyDisposable<SwiftSparseMapPool<T>> _lazyInstance =
 129        new(() => new SwiftSparseMapPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 30
 31    /// <summary>
 32    /// Gets the shared instance of the pool.
 33    /// </summary>
 234    public static SwiftSparseMapPool<T> Shared => _lazyInstance.Value;
 35
 36    #endregion
 37
 38    #region Fields
 39
 640    private readonly SwiftLazyDisposable<SwiftObjectPool<SwiftSparseMap<T>>> _lazyCollectionPool =
 641        new(() =>
 642        {
 643            return new SwiftObjectPool<SwiftSparseMap<T>>(
 644                createFunc: () => new SwiftSparseMap<T>(),
 645                actionOnRelease: map => map.Clear()
 646            );
 647        });
 48
 49    /// <summary>
 50    /// Tracks whether the pool has been disposed.
 51    /// </summary>
 52    private volatile bool _disposed;
 53
 54    #endregion
 55
 56    #region Properties
 57
 58    /// <summary>
 59    /// Gets the underlying object pool used to manage sparse map instances.
 60    /// </summary>
 1061    public SwiftObjectPool<SwiftSparseMap<T>> CollectionPool => _lazyCollectionPool.Value;
 62
 63    #endregion
 64
 65    #region Methods
 66
 67    /// <summary>
 68    /// Rents a sparse map instance from the pool. If the pool is empty, a new sparse map is created.
 69    /// </summary>
 70    /// <returns>A <see cref="SwiftSparseMap{T}"/> instance.</returns>
 71    public SwiftSparseMap<T> Rent()
 72    {
 773        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 74
 675        return CollectionPool.Rent();
 76    }
 77
 78    /// <summary>
 79    /// Rents a sparse map from the pool and wraps it in a <see cref="SwiftPooledObject{T}"/> for automatic release.
 80    /// </summary>
 81    /// <param name="value">The rented sparse map.</param>
 82    /// <returns>A <see cref="SwiftPooledObject{T}"/> instance wrapping the rented sparse map.</returns>
 83    public SwiftPooledObject<SwiftSparseMap<T>> Get(out SwiftSparseMap<T> value)
 84    {
 185        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 86
 187        return CollectionPool.Rent(out value);
 88    }
 89
 90    /// <summary>
 91    /// Releases a sparse map instance back to the pool for reuse.
 92    /// </summary>
 93    /// <param name="map">The sparse map to release.</param>
 94    public void Release(SwiftSparseMap<T> map)
 95    {
 596        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftSparseMapPool<T>));
 97
 598        if (map == null) return;
 99
 3100        CollectionPool.Release(map);
 3101    }
 102
 103    /// <summary>
 104    /// Clears all pooled sparse maps from the pool.
 105    /// </summary>
 106    public void Clear()
 107    {
 8108        if (_disposed || !_lazyCollectionPool.IsValueCreated)
 3109            return;
 110
 5111        _lazyCollectionPool.Value.Clear();
 5112    }
 113
 114    #endregion
 115
 116    #region IDisposable Implementation
 117
 118    /// <summary>
 119    /// Releases all resources used by the SwiftSparseMapPool.
 120    /// </summary>
 121    public void Dispose()
 122    {
 6123        if (_disposed)
 1124            return;
 125
 5126        Clear();
 127
 5128        if (_lazyCollectionPool.IsValueCreated)
 4129            _lazyCollectionPool.Value.Dispose();
 130
 5131        _disposed = true;
 132
 5133        GC.SuppressFinalize(this);
 5134    }
 135
 136    /// <summary>
 137    /// Releases the resources used by the SwiftSparseMapPool instance.
 138    /// </summary>
 139    /// <remarks>
 140    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 141    /// It is recommended to call Dispose to release resources deterministically.
 142    /// </remarks>
 3143    ~SwiftSparseMapPool() => Dispose();
 144
 145    #endregion
 146}