< Summary

Information
Class: SwiftCollections.Pool.SwiftListPool<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftListPool.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 120
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/SwiftListPool.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftListPool.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="SwiftList{T}"/> instances,
 17/// providing efficient reuse to minimize memory allocations and improve performance.
 18/// </summary>
 19/// <typeparam name="T">The type of elements in the list.</typeparam>
 20public sealed class SwiftListPool<T> : SwiftCollectionPool<SwiftList<T>, T>, IDisposable
 21{
 22    #region Singleton Instance
 23
 24    /// <summary>
 25    /// Shared instance of the hash set 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<SwiftListPool<T>> _lazyInstance =
 129        new(() => new SwiftListPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 30
 31    /// <summary>
 32    /// Gets the shared instance of the pool.
 33    /// </summary>
 234    public static SwiftListPool<T> Shared => _lazyInstance.Value;
 35
 36    #endregion
 37
 38    #region Fields
 39
 40    /// <summary>
 41    /// Tracks whether the pool has been disposed.
 42    /// </summary>
 43    private volatile bool _disposed;
 44
 45    #endregion
 46
 47    #region Methods
 48
 49    /// <summary>
 50    /// Rents a list instance from the pool. If the pool is empty, a new list is created.
 51    /// </summary>
 52    /// <returns>A <see cref="SwiftList{T}"/> instance.</returns>
 53    public override SwiftList<T> Rent()
 54    {
 755        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftList<T>));
 56
 657        return base.Rent();
 58    }
 59
 60    /// <summary>
 61    /// Releases a list instance back to the pool for reuse.
 62    /// </summary>
 63    /// <param name="list">The list to release.</param>
 64    /// <remarks>
 65    /// The list will be cleared before being returned to the pool to ensure it contains no stale data.
 66    /// </remarks>
 67    public override void Release(SwiftList<T> list)
 68    {
 569        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftList<T>));
 70
 571        if (list == null) return;
 72
 73        // Ensure the list is clear before returning it to the pool
 374        list.Clear();
 375        base.Release(list);
 376    }
 77
 78    /// <summary>
 79    /// Clears all pooled lists from the pool.
 80    /// </summary>
 81    public override void Clear()
 82    {
 1083        if (_disposed) return;
 84
 885        base.Clear();
 886    }
 87
 88    #endregion
 89
 90    #region IDisposable Implementation
 91
 92    /// <summary>
 93    /// Releases all resources used by the SwiftListPool.
 94    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 95    /// </summary>
 96    public void Dispose()
 97    {
 798        if (_disposed)
 199            return;
 100
 6101        Clear();
 6102        base.Flush();
 103
 6104        _disposed = true;
 105
 106        // Suppress finalization to prevent unnecessary GC overhead
 6107        GC.SuppressFinalize(this);
 6108    }
 109
 110    /// <summary>
 111    /// Releases the resources used by the SwiftListPool instance.
 112    /// </summary>
 113    /// <remarks>
 114    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 115    /// It is recommended to call Dispose to release resources deterministically.
 116    /// </remarks>
 4117    ~SwiftListPool() => Dispose();
 118
 119    #endregion
 120}