< Summary

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

File(s)

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

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Text;
 3
 4namespace NGql.Core.Pooling;
 5
 6/// <summary>
 7/// Lock-free pool for StringBuilder instances with thread-local optimization
 8/// </summary>
 9internal static class LockFreeStringBuilderPool
 10{
 11    private const int MaxCapacity = 2048; // Larger capacity limit for better reuse
 12    private const int InitialCapacity = 256;
 13
 314    private static readonly ThreadLocalPool<StringBuilder> _pool = new(
 1215        factory: () => new StringBuilder(InitialCapacity),
 4216        reset: sb => sb.Clear(),
 4217        validateForReturn: sb => sb.Capacity <= MaxCapacity, // Skip if capacity is too large (prevents memory bloat)
 318        poolName: "stringbuilder"
 319    );
 20
 21    /// <summary>
 22    /// Gets a pooled StringBuilder instance
 23    /// </summary>
 24    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 4225    internal static PooledStringBuilder Get() => new(_pool.Get());
 26
 27    /// <summary>
 28    /// Gets a pooled StringBuilder instance (alias for compatibility)
 29    /// </summary>
 30    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 4231    internal static PooledStringBuilder GetPooled() => Get();
 32
 33    /// <summary>
 34    /// Returns StringBuilder to the pool
 35    /// </summary>
 36    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 37    [System.Diagnostics.CodeAnalysis.SuppressMessage("Major Code Smell", "S3398:Move this method inside 'PooledStringBui
 4238    private static void Return(StringBuilder sb) => _pool.Return(sb);
 39
 40    /// <summary>
 41    /// Zero-allocation ref struct wrapper with lock-free pooling
 42    /// </summary>
 43    internal readonly ref struct PooledStringBuilder
 44    {
 45        public readonly StringBuilder StringBuilder;
 4246        internal PooledStringBuilder(StringBuilder sb) => StringBuilder = sb;
 4247        public void Dispose() => Return(StringBuilder);
 48    }
 49}