| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core.Pooling; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Generic thread-local pool with global fallback for any type. |
| | | 7 | | /// Provides lock-free pooling with thread-local optimization to eliminate contention. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="T">Type to pool (must be a reference type)</typeparam> |
| | | 10 | | internal sealed class ThreadLocalPool<T> where T : class |
| | | 11 | | { |
| | | 12 | | private const int ThreadLocalCacheSize = 4; |
| | | 13 | | private const int GlobalPoolSize = 64; |
| | | 14 | | |
| | | 15 | | // Instance-based thread-local storage for per-thread caches to eliminate contention |
| | | 16 | | // CRITICAL FIX: Each instance gets its own ThreadLocal<>, not a shared [ThreadStatic] field |
| | | 17 | | // This prevents different generic types from corrupting each other's caches |
| | | 18 | | private readonly ThreadLocal<ThreadLocalCache> _threadLocalCache; |
| | | 19 | | |
| | | 20 | | // Global lock-free fallback pool using atomic operations |
| | 51 | 21 | | private readonly MonitoredLockFreeStack<T> _globalPool = new(); |
| | | 22 | | private volatile int _globalCount; |
| | | 23 | | |
| | | 24 | | private readonly Func<T> _factory; |
| | | 25 | | private readonly Action<T> _reset; |
| | | 26 | | private readonly Func<T, bool>? _validateForReturn; |
| | | 27 | | private readonly string _poolName; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Creates a new thread-local pool. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="factory">Factory function to create new instances</param> |
| | | 33 | | /// <param name="reset">Action to reset/clear an instance before returning to pool</param> |
| | | 34 | | /// <param name="validateForReturn">Optional validation - return false to reject item from pool</param> |
| | | 35 | | /// <param name="poolName">Name for diagnostics/metrics</param> |
| | 51 | 36 | | public ThreadLocalPool( |
| | 51 | 37 | | Func<T> factory, |
| | 51 | 38 | | Action<T> reset, |
| | 51 | 39 | | Func<T, bool>? validateForReturn = null, |
| | 51 | 40 | | string poolName = "unknown") |
| | | 41 | | { |
| | 51 | 42 | | _factory = factory ?? throw new ArgumentNullException(nameof(factory)); |
| | 48 | 43 | | _reset = reset ?? throw new ArgumentNullException(nameof(reset)); |
| | 45 | 44 | | _validateForReturn = validateForReturn; |
| | 45 | 45 | | _poolName = poolName; |
| | | 46 | | // Instance-based ThreadLocal ensures each pool instance has separate per-thread caches |
| | 45 | 47 | | _threadLocalCache = new ThreadLocal<ThreadLocalCache>(); |
| | 45 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Thread-local cache to minimize global pool access |
| | | 52 | | /// </summary> |
| | | 53 | | private sealed class ThreadLocalCache |
| | | 54 | | { |
| | 132 | 55 | | private readonly T?[] _items = new T?[ThreadLocalCacheSize]; |
| | | 56 | | private int _count; |
| | | 57 | | |
| | | 58 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 59 | | public bool TryGet(out T? item) |
| | | 60 | | { |
| | 8727 | 61 | | if (_count > 0) |
| | | 62 | | { |
| | 7500 | 63 | | var index = --_count; |
| | 7500 | 64 | | item = _items[index]!; |
| | 7500 | 65 | | _items[index] = null; // Clear reference |
| | 7500 | 66 | | return true; |
| | | 67 | | } |
| | 1227 | 68 | | item = null; |
| | 1227 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 73 | | public bool TryReturn(T item) |
| | | 74 | | { |
| | 8703 | 75 | | if (_count < ThreadLocalCacheSize) |
| | | 76 | | { |
| | 7647 | 77 | | _items[_count++] = item; |
| | 7647 | 78 | | return true; |
| | | 79 | | } |
| | 1056 | 80 | | return false; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets an item from thread-local cache first, then global pool, or creates new |
| | | 86 | | /// </summary> |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public T Get() |
| | | 89 | | { |
| | | 90 | | // ULTRA FAST PATH: Thread-local cache hit (no contention) |
| | | 91 | | // FIXED: Use instance-based ThreadLocal instead of static [ThreadStatic] field |
| | 8727 | 92 | | var cache = _threadLocalCache.Value ?? (_threadLocalCache.Value = new ThreadLocalCache()); |
| | 8727 | 93 | | if (cache.TryGet(out var item) && item is not null) |
| | | 94 | | { |
| | 7500 | 95 | | ThreadLocalMemoryManager.RecordThreadLocalHit(_poolName); |
| | 7500 | 96 | | return item; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // FAST PATH: Global lock-free pool |
| | 1227 | 100 | | if (_globalPool.TryPop(out item)) |
| | | 101 | | { |
| | 3 | 102 | | Interlocked.Decrement(ref _globalCount); |
| | 3 | 103 | | return item; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // SLOW PATH: Allocate new instance |
| | 1224 | 107 | | ThreadLocalMemoryManager.RecordAllocation(_poolName); |
| | 1224 | 108 | | return _factory(); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Returns item to thread-local cache first, then global pool |
| | | 113 | | /// </summary> |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 115 | | public void Return(T? item) |
| | | 116 | | { |
| | 8712 | 117 | | if (item == null) return; |
| | | 118 | | |
| | | 119 | | // Validate item if validator is provided |
| | 8706 | 120 | | if (_validateForReturn != null && !_validateForReturn(item)) |
| | | 121 | | { |
| | 3 | 122 | | return; // Item rejected, let GC handle it |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | // Reset/clear the item |
| | 8703 | 126 | | _reset(item); |
| | | 127 | | |
| | | 128 | | // ULTRA FAST PATH: Return to thread-local cache (no contention) |
| | | 129 | | // FIXED: Use instance-based ThreadLocal instead of static [ThreadStatic] field |
| | 8703 | 130 | | var cache = _threadLocalCache.Value ?? (_threadLocalCache.Value = new ThreadLocalCache()); |
| | 8703 | 131 | | if (cache.TryReturn(item)) |
| | | 132 | | { |
| | 7647 | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // FAST PATH: Return to global pool if not full (atomic increment with bounds check) |
| | 1056 | 137 | | var newCount = Interlocked.Increment(ref _globalCount); |
| | 1056 | 138 | | if (newCount <= GlobalPoolSize) |
| | | 139 | | { |
| | 564 | 140 | | _globalPool.Push(item); |
| | | 141 | | } |
| | | 142 | | else |
| | | 143 | | { |
| | | 144 | | // Pool is full, undo the increment and let GC handle it |
| | 492 | 145 | | Interlocked.Decrement(ref _globalCount); |
| | | 146 | | } |
| | 492 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Gets the approximate count of items in the global pool (for diagnostics) |
| | | 151 | | /// </summary> |
| | 3 | 152 | | public int ApproximateCount => _globalCount; |
| | | 153 | | } |