< Summary

Information
Class: SwiftCollections.Pool.SwiftCollectionPool<T1, T2>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftCollectionPool.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 109
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%22100%
get_CollectionPool()100%11100%
Rent()100%11100%
Get(...)100%11100%
Release(...)100%11100%
Clear()100%22100%
Flush()100%44100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftCollectionPool.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftCollectionPool.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.Lazy;
 9using System;
 10using System.Collections.Generic;
 11
 12namespace SwiftCollections.Pool;
 13
 14/// <summary>
 15/// A generic pool for managing collections such as <see cref="List{T}"/>, <see cref="HashSet{T}"/>,
 16/// <see cref="Dictionary{TKey, TValue}"/>, and other types implementing <see cref="ICollection{T}"/>.
 17/// Provides efficient reuse of collection instances to reduce memory allocations.
 18/// </summary>
 19/// <typeparam name="TCollection">The type of the collection being pooled. Must implement <see cref="ICollection{TItem}"
 20/// <typeparam name="TItem">The type of items contained in the collection.</typeparam>
 21public abstract class SwiftCollectionPool<TCollection, TItem> where TCollection : class, ICollection<TItem>, new()
 22{
 23    #region Singleton Instances
 24
 25    /// <summary>
 26    /// Internal object pool for managing the lifecycle of pooled collections.
 27    /// Uses <see cref="Lazy{T}"/> to ensure lazy initialization.
 28    /// </summary>
 3629    private SwiftLazyDisposable<SwiftObjectPool<TCollection>> _lazyCollectionPool =
 3630        new(() =>
 3631        {
 3632            return new SwiftObjectPool<TCollection>(
 3633                    createFunc: () => new TCollection(),
 3634                    actionOnRelease: collection => collection.Clear()
 3635                );
 3636        });
 37
 38    /// <summary>
 39    /// Gets the shared instance of the pool.
 40    /// </summary>
 5641    public SwiftObjectPool<TCollection> CollectionPool => _lazyCollectionPool.Value;
 42
 43    #endregion
 44
 45    #region Methods
 46
 47    /// <summary>
 48    /// Rents a collection from the pool. If the pool is empty, a new collection is created.
 49    /// </summary>
 50    /// <returns>A collection instance of type <typeparamref name="TCollection"/>.</returns>
 51    public virtual TCollection Rent()
 52    {
 3653        return CollectionPool.Rent();
 54    }
 55
 56    /// <summary>
 57    /// Rents a collection from the pool and wraps it in a <see cref="SwiftPooledObject{TCollection}"/> for automatic re
 58    /// </summary>
 59    /// <param name="value">The rented collection.</param>
 60    /// <returns>A <see cref="SwiftPooledObject{TCollection}"/> instance wrapping the rented collection.</returns>
 61    public virtual SwiftPooledObject<TCollection> Get(out TCollection value)
 62    {
 163        return CollectionPool.Rent(out value);
 64    }
 65
 66    /// <summary>
 67    /// Releases a collection back to the pool for reuse.
 68    /// </summary>
 69    /// <param name="toRelease">The collection to release.</param>
 70    public virtual void Release(TCollection toRelease)
 71    {
 1972        CollectionPool.Release(toRelease);
 1973    }
 74
 75    /// <summary>
 76    /// Clears all collections from the pool.
 77    /// </summary>
 78    public virtual void Clear()
 79    {
 3580        if (_lazyCollectionPool.IsValueCreated)
 2781            _lazyCollectionPool.Value.Clear();
 3582    }
 83
 84    #endregion
 85
 86    #region IDisposable Implementation
 87
 88    /// <summary>
 89    /// Releases all resources used by the SwiftCollectionPool.
 90    /// </summary>
 91    public virtual void Flush()
 92    {
 3293        if (_lazyCollectionPool.IsValueCreated)
 94        {
 2495            _lazyCollectionPool.Value.Dispose();
 96
 2497            _lazyCollectionPool = new SwiftLazyDisposable<SwiftObjectPool<TCollection>>(() =>
 2498            {
 2499                return new SwiftObjectPool<TCollection>(
 24100                        createFunc: () => new TCollection(),
 24101                        actionOnRelease: collection => collection.Clear()
 24102                    );
 24103            });
 104        }
 105
 32106    }
 107
 108    #endregion
 109}