| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using NGql.Core.Observability; |
| | | 3 | | |
| | | 4 | | namespace 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> |
| | | 10 | | internal 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 | | { |
| | 7626 | 18 | | PoolingObservability.RecordThreadLocalHit(poolType); |
| | 7626 | 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 | | { |
| | 1815 | 27 | | PoolingObservability.RecordGlobalPoolHit(poolType, poolSize); |
| | 1815 | 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 | | { |
| | 1287 | 36 | | PoolingObservability.RecordAllocation(poolType); |
| | 1287 | 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> |
| | | 44 | | internal 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 | | } |