< 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: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 105
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
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%22100%
Release(...)100%44100%
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()
 445    {
 446        if (_disposed)
 147            throw new ObjectDisposedException(nameof(SwiftQueuePool<T>));
 48
 349        return base.Rent();
 350    }
 51
 52    /// <summary>
 53    /// Releases a queue instance back to the pool for reuse.
 54    /// </summary>
 55    /// <param name="queue">The queue to release.</param>
 56    /// <remarks>
 57    /// The queue will be cleared before being returned to the pool to ensure it contains no stale data.
 58    /// </remarks>
 59    public override void Release(SwiftQueue<T> queue)
 460    {
 461        if (_disposed)
 162            throw new ObjectDisposedException(nameof(SwiftQueuePool<T>));
 63
 464        if (queue == null) return;
 65
 66        // SwiftCollectionPool clears pool before releasing
 267        base.Release(queue);
 368    }
 69
 70    /// <summary>
 71    /// Clears all pooled queues from the pool.
 72    /// </summary>
 73    public override void Clear()
 474    {
 575        if (_disposed) return;
 76
 377        base.Clear();
 478    }
 79
 80    #endregion
 81
 82    #region IDisposable Implementation
 83
 84    /// <summary>
 85    /// Releases all resources used by the SwiftQueuePool.
 86    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 87    /// </summary>
 88    public void Dispose()
 489    {
 490        if (_disposed)
 191            return;
 92
 393        Clear();
 394        base.Flush();
 95
 396        _disposed = true;
 97
 98        // Suppress finalization to prevent unnecessary GC overhead
 399        GC.SuppressFinalize(this);
 4100    }
 101
 2102    ~SwiftQueuePool() => Dispose();
 103
 104    #endregion
 105}