< Summary

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

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace SwiftCollections.Pool;
 5
 6/// <summary>
 7/// A specialized pool for managing <see cref="SwiftQueue{T}"/> instances,
 8/// providing efficient reuse to minimize memory allocations and improve performance.
 9/// </summary>
 10/// <typeparam name="T">The type of elements in the queue.</typeparam>
 11public sealed class SwiftQueuePool<T> : SwiftCollectionPool<SwiftQueue<T>, T>, IDisposable
 12{
 13    #region Singleton Instance
 14
 15    /// <summary>
 16    /// Shared instance of the queue 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<SwiftQueuePool<T>> _lazyInstance =
 220        new(() => new SwiftQueuePool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 21
 22    /// <summary>
 23    /// Gets the shared instance of the pool.
 24    /// </summary>
 225    public static SwiftQueuePool<T> Shared => _lazyInstance.Value;
 26
 27    #endregion
 28
 29    #region Fields
 30
 31    /// <summary>
 32    /// Tracks whether the pool has been disposed.
 33    /// </summary>
 34    private volatile bool _disposed;
 35
 36    #endregion
 37
 38    #region Methods
 39
 40    /// <summary>
 41    /// Rents a queue instance from the pool. If the pool is empty, a new queue is created.
 42    /// </summary>
 43    /// <returns>A <see cref="SwiftQueue{T}"/> instance.</returns>
 44    public override SwiftQueue<T> Rent()
 45    {
 446        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftQueuePool<T>));
 47
 348        return base.Rent();
 49    }
 50
 51    /// <summary>
 52    /// Releases a queue instance back to the pool for reuse.
 53    /// </summary>
 54    /// <param name="queue">The queue to release.</param>
 55    /// <remarks>
 56    /// The queue will be cleared before being returned to the pool to ensure it contains no stale data.
 57    /// </remarks>
 58    public override void Release(SwiftQueue<T> queue)
 59    {
 460        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftQueuePool<T>));
 61
 462        if (queue == null) return;
 63
 64        // SwiftCollectionPool clears pool before releasing
 265        base.Release(queue);
 266    }
 67
 68    /// <summary>
 69    /// Clears all pooled queues from the pool.
 70    /// </summary>
 71    public override void Clear()
 72    {
 573        if (_disposed) return;
 374        base.Clear();
 375    }
 76
 77    #endregion
 78
 79    #region IDisposable Implementation
 80
 81    /// <summary>
 82    /// Releases all resources used by the SwiftQueuePool.
 83    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 84    /// </summary>
 85    public void Dispose()
 86    {
 587        if (_disposed) return;
 88
 389        Clear();
 390        base.Flush();
 91
 392        _disposed = true;
 93
 94        // Suppress finalization to prevent unnecessary GC overhead
 395        GC.SuppressFinalize(this);
 396    }
 97
 98    /// <summary>
 99    /// Releases the resources used by the SwiftQueuePool instance.
 100    /// </summary>
 101    /// <remarks>
 102    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 103    /// It is recommended to call Dispose to release resources deterministically.
 104    /// </remarks>
 2105    ~SwiftQueuePool() => Dispose();
 106
 107    #endregion
 108}