< Summary

Information
Class: NGql.Core.Pooling.MonitoredLockFreeStack<T>
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Pooling/ThreadLocalMemoryManager.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 92
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
.ctor(...)100%11100%
Push(...)100%22100%
TryPop(...)100%44100%

File(s)

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

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using NGql.Core.Observability;
 3
 4namespace NGql.Core.Pooling;
 5
 6/// <summary>
 7/// Centralized thread-local memory management with integrated observability.
 8/// Provides thread-affinity optimizations and comprehensive telemetry.
 9/// </summary>
 10internal static class ThreadLocalMemoryManager
 11{
 12    /// <summary>
 13    /// Records a thread-local cache hit with observability
 14    /// </summary>
 15    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 16    internal static void RecordThreadLocalHit(string poolType)
 17    {
 18        PoolingObservability.RecordThreadLocalHit(poolType);
 19    }
 20
 21    /// <summary>
 22    /// Records a global pool hit with observability
 23    /// </summary>
 24    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 25    internal static void RecordGlobalPoolHit(string poolType, int poolSize = -1)
 26    {
 27        PoolingObservability.RecordGlobalPoolHit(poolType, poolSize);
 28    }
 29
 30    /// <summary>
 31    /// Records a new allocation with observability
 32    /// </summary>
 33    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 34    internal static void RecordAllocation(string poolType)
 35    {
 36        PoolingObservability.RecordAllocation(poolType);
 37    }
 38}
 39
 40/// <summary>
 41/// Enhanced lock-free stack with performance monitoring integration
 42/// </summary>
 43/// <typeparam name="T">Type of items in the stack</typeparam>
 44internal sealed class MonitoredLockFreeStack<T> where T : class
 45{
 46    private volatile Node? _head;
 47
 48    private sealed class Node
 49    {
 50        public readonly T Item;
 51        public Node? Next;
 52
 475253        public Node(T item) => Item = item;
 54    }
 55
 56    /// <summary>
 57    /// Pushes an item onto the stack using compare-and-swap
 58    /// </summary>
 59    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60    public void Push(T item)
 61    {
 237662        var newNode = new Node(item);
 63        Node? currentHead;
 64        do
 65        {
 237666            currentHead = _head;
 237667            newNode.Next = currentHead;
 237668        } while (Interlocked.CompareExchange(ref _head, newNode, currentHead) != currentHead);
 237669    }
 70
 71    /// <summary>
 72    /// Attempts to pop an item from the stack using compare-and-swap with monitoring
 73    /// </summary>
 74    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 75    public bool TryPop(out T item)
 76    {
 77        Node? currentHead;
 78        do
 79        {
 14879680            currentHead = _head;
 14879681            if (currentHead == null)
 82            {
 14698183                item = default!;
 14698184                return false;
 85            }
 181586        } while (Interlocked.CompareExchange(ref _head, currentHead.Next, currentHead) != currentHead);
 87
 181588        item = currentHead.Item;
 181589        ThreadLocalMemoryManager.RecordGlobalPoolHit("unknown", -1);
 181590        return true;
 91    }
 92}

Methods/Properties

.ctor(T)
Push(T)
TryPop(T&)