< 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: 117
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
 1//=======================================================================
 2// SwiftQueuePool.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="SwiftQueue{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 queue.</typeparam>
 20public sealed class SwiftQueuePool<T> : SwiftCollectionPool<SwiftQueue<T>, T>, IDisposable
 21{
 22    #region Singleton Instance
 23
 24    /// <summary>
 25    /// Shared instance of the queue 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<SwiftQueuePool<T>> _lazyInstance =
 129        new(() => new SwiftQueuePool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 30
 31    /// <summary>
 32    /// Gets the shared instance of the pool.
 33    /// </summary>
 234    public static SwiftQueuePool<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 queue instance from the pool. If the pool is empty, a new queue is created.
 51    /// </summary>
 52    /// <returns>A <see cref="SwiftQueue{T}"/> instance.</returns>
 53    public override SwiftQueue<T> Rent()
 54    {
 455        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftQueuePool<T>));
 56
 357        return base.Rent();
 58    }
 59
 60    /// <summary>
 61    /// Releases a queue instance back to the pool for reuse.
 62    /// </summary>
 63    /// <param name="queue">The queue to release.</param>
 64    /// <remarks>
 65    /// The queue will be cleared before being returned to the pool to ensure it contains no stale data.
 66    /// </remarks>
 67    public override void Release(SwiftQueue<T> queue)
 68    {
 469        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftQueuePool<T>));
 70
 471        if (queue == null) return;
 72
 73        // SwiftCollectionPool clears pool before releasing
 274        base.Release(queue);
 275    }
 76
 77    /// <summary>
 78    /// Clears all pooled queues from the pool.
 79    /// </summary>
 80    public override void Clear()
 81    {
 582        if (_disposed) return;
 383        base.Clear();
 384    }
 85
 86    #endregion
 87
 88    #region IDisposable Implementation
 89
 90    /// <summary>
 91    /// Releases all resources used by the SwiftQueuePool.
 92    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 93    /// </summary>
 94    public void Dispose()
 95    {
 596        if (_disposed) return;
 97
 398        Clear();
 399        base.Flush();
 100
 3101        _disposed = true;
 102
 103        // Suppress finalization to prevent unnecessary GC overhead
 3104        GC.SuppressFinalize(this);
 3105    }
 106
 107    /// <summary>
 108    /// Releases the resources used by the SwiftQueuePool instance.
 109    /// </summary>
 110    /// <remarks>
 111    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 112    /// It is recommended to call Dispose to release resources deterministically.
 113    /// </remarks>
 2114    ~SwiftQueuePool() => Dispose();
 115
 116    #endregion
 117}