< Summary

Information
Class: NGql.Core.Pooling.LockFreeHashSetPool
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Pooling/LockFreeHashSetPool.cs
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
GetPooled()100%11100%
GetPooled(...)100%22100%
GetPooled(...)100%22100%
Return(...)100%11100%
.ctor(...)100%11100%
Dispose()100%11100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Pooling/LockFreeHashSetPool.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2
 3namespace NGql.Core.Pooling;
 4
 5/// <summary>
 6/// Lock-free pool for HashSet&lt;string&gt; instances with thread-local optimization
 7/// </summary>
 8internal static class LockFreeHashSetPool
 9{
 10    private const int MaxSize = 128; // Prevent memory bloat from large sets
 11
 312    private static readonly ThreadLocalPool<HashSet<string>> _pool = new(
 7513        factory: () => new HashSet<string>(StringComparer.OrdinalIgnoreCase),
 18314        reset: set => set.Clear(),
 18315        validateForReturn: set => set.Count <= MaxSize, // Skip very large sets to prevent memory bloat
 316        poolName: "hashset"
 317    );
 18
 19    /// <summary>
 20    /// Gets an empty pooled HashSet (for warmup/testing)
 21    /// </summary>
 22    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1523    internal static PooledHashSet GetPooled() => new(_pool.Get());
 24
 25    /// <summary>
 26    /// Gets a pooled HashSet populated from source
 27    /// </summary>
 28    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 29    internal static PooledHashSet GetPooled(HashSet<string> source)
 30    {
 331        var set = _pool.Get();
 2432        foreach (var item in source)
 933            set.Add(item);
 334        return new PooledHashSet(set);
 35    }
 36
 37    /// <summary>
 38    /// Gets a pooled HashSet populated from source
 39    /// </summary>
 40    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 41    internal static PooledHashSet GetPooled(IEnumerable<string> source)
 42    {
 16543        var set = _pool.Get();
 54044        foreach (var item in source)
 10545            set.Add(item);
 16546        return new PooledHashSet(set);
 47    }
 48
 49    /// <summary>
 50    /// Returns HashSet to the pool
 51    /// </summary>
 52    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 53    [System.Diagnostics.CodeAnalysis.SuppressMessage("Major Code Smell", "S3398:Move this method inside 'PooledHashSet'"
 18354    private static void Return(HashSet<string> set) => _pool.Return(set);
 55
 56    /// <summary>
 57    /// Zero-allocation ref struct wrapper with lock-free pooling
 58    /// </summary>
 59    internal readonly ref struct PooledHashSet
 60    {
 61        public readonly HashSet<string> Set;
 18362        internal PooledHashSet(HashSet<string> set) => Set = set;
 18363        public void Dispose() => Return(Set);
 64    }
 65}