< 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
97%
Covered lines: 34
Uncovered lines: 1
Coverable lines: 35
Total lines: 101
Line coverage: 97.1%
Branch coverage
100%
Covered branches: 16
Total branches: 16
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%66100%
get_CollectionPool()100%11100%
Rent()100%11100%
Get(...)100%11100%
Release(...)100%11100%
Clear()100%22100%
Flush()100%8892.3%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace SwiftCollections.Pool;
 5
 6/// <summary>
 7/// A generic pool for managing collections such as <see cref="List{T}"/>, <see cref="HashSet{T}"/>,
 8/// <see cref="Dictionary{TKey, TValue}"/>, and other types implementing <see cref="ICollection{T}"/>.
 9/// Provides efficient reuse of collection instances to reduce memory allocations.
 10/// </summary>
 11/// <typeparam name="TCollection">The type of the collection being pooled. Must implement <see cref="ICollection{TItem}"
 12/// <typeparam name="TItem">The type of items contained in the collection.</typeparam>
 13public abstract class SwiftCollectionPool<TCollection, TItem> where TCollection : class, ICollection<TItem>, new()
 14{
 15    #region Singleton Instances
 16
 17    /// <summary>
 18    /// Internal object pool for managing the lifecycle of pooled collections.
 19    /// Uses <see cref="Lazy{T}"/> to ensure lazy initialization.
 20    /// </summary>
 3021    private SwiftLazyDisposable<SwiftObjectPool<TCollection>> _lazyCollectionPool =
 3022        new SwiftLazyDisposable<SwiftObjectPool<TCollection>>(() =>
 2223        {
 2224            return new SwiftObjectPool<TCollection>(
 2725                    createFunc: () => new TCollection(),
 1826                    actionOnRelease: collection => collection.Clear()
 2227                );
 5228        });
 29
 30    /// <summary>
 31    /// Gets the shared instance of the pool.
 32    /// </summary>
 5133    public SwiftObjectPool<TCollection> CollectionPool => _lazyCollectionPool.Value;
 34
 35    #endregion
 36
 37    #region Methods
 38
 39    /// <summary>
 40    /// Rents a collection from the pool. If the pool is empty, a new collection is created.
 41    /// </summary>
 42    /// <returns>A collection instance of type <typeparamref name="TCollection"/>.</returns>
 43    public virtual TCollection Rent()
 3344    {
 3345        return CollectionPool.Rent();
 3346    }
 47
 48    /// <summary>
 49    /// Rents a collection from the pool and wraps it in a <see cref="SwiftPooledObject{TCollection}"/> for automatic re
 50    /// </summary>
 51    /// <param name="value">The rented collection.</param>
 52    /// <returns>A <see cref="SwiftPooledObject{TCollection}"/> instance wrapping the rented collection.</returns>
 53    public virtual SwiftPooledObject<TCollection> Get(out TCollection value)
 154    {
 155        return CollectionPool.Rent(out value);
 156    }
 57
 58    /// <summary>
 59    /// Releases a collection back to the pool for reuse.
 60    /// </summary>
 61    /// <param name="toRelease">The collection to release.</param>
 62    public virtual void Release(TCollection toRelease)
 1763    {
 1764        CollectionPool.Release(toRelease);
 1765    }
 66
 67    /// <summary>
 68    /// Clears all collections from the pool.
 69    /// </summary>
 70    public virtual void Clear()
 3071    {
 3072        if (_lazyCollectionPool.IsValueCreated)
 2773            _lazyCollectionPool.Value.Clear();
 3074    }
 75
 76    #endregion
 77
 78    #region IDisposable Implementation
 79
 80    /// <summary>
 81    /// Releases all resources used by the SwiftCollectionPool.
 82    /// </summary>
 83    public virtual void Flush()
 2684    {
 2685        if (_lazyCollectionPool.IsValueCreated)
 2386        {
 2387            _lazyCollectionPool.Value.Dispose();
 88
 2389            _lazyCollectionPool = new SwiftLazyDisposable<SwiftObjectPool<TCollection>>(() =>
 190            {
 191                return new SwiftObjectPool<TCollection>(
 192                        createFunc: () => new TCollection(),
 093                        actionOnRelease: collection => collection.Clear()
 194                    );
 2495            });
 2396        }
 97
 2698    }
 99
 100    #endregion
 101}