< Summary

Information
Class: NGql.Core.Pooling.ThreadLocalMemoryManager
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Pooling/ThreadLocalMemoryManager.cs
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 92
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
RecordThreadLocalHit(...)100%11100%
RecordGlobalPoolHit(...)100%11100%
RecordAllocation(...)100%11100%

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    {
 762618        PoolingObservability.RecordThreadLocalHit(poolType);
 762619    }
 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    {
 181527        PoolingObservability.RecordGlobalPoolHit(poolType, poolSize);
 181528    }
 29
 30    /// <summary>
 31    /// Records a new allocation with observability
 32    /// </summary>
 33    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 34    internal static void RecordAllocation(string poolType)
 35    {
 128736        PoolingObservability.RecordAllocation(poolType);
 128737    }
 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
 53        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    {
 62        var newNode = new Node(item);
 63        Node? currentHead;
 64        do
 65        {
 66            currentHead = _head;
 67            newNode.Next = currentHead;
 68        } while (Interlocked.CompareExchange(ref _head, newNode, currentHead) != currentHead);
 69    }
 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        {
 80            currentHead = _head;
 81            if (currentHead == null)
 82            {
 83                item = default!;
 84                return false;
 85            }
 86        } while (Interlocked.CompareExchange(ref _head, currentHead.Next, currentHead) != currentHead);
 87
 88        item = currentHead.Item;
 89        ThreadLocalMemoryManager.RecordGlobalPoolHit("unknown", -1);
 90        return true;
 91    }
 92}