| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core.Pooling; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Lock-free pool for HashSet<string> instances with thread-local optimization |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class LockFreeHashSetPool |
| | | 9 | | { |
| | | 10 | | private const int MaxSize = 128; // Prevent memory bloat from large sets |
| | | 11 | | |
| | 3 | 12 | | private static readonly ThreadLocalPool<HashSet<string>> _pool = new( |
| | 75 | 13 | | factory: () => new HashSet<string>(StringComparer.OrdinalIgnoreCase), |
| | 183 | 14 | | reset: set => set.Clear(), |
| | 183 | 15 | | validateForReturn: set => set.Count <= MaxSize, // Skip very large sets to prevent memory bloat |
| | 3 | 16 | | poolName: "hashset" |
| | 3 | 17 | | ); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets an empty pooled HashSet (for warmup/testing) |
| | | 21 | | /// </summary> |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 15 | 23 | | internal static PooledHashSet GetPooled() => new(_pool.Get()); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets a pooled HashSet populated from source |
| | | 27 | | /// </summary> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | internal static PooledHashSet GetPooled(HashSet<string> source) |
| | | 30 | | { |
| | 3 | 31 | | var set = _pool.Get(); |
| | 24 | 32 | | foreach (var item in source) |
| | 9 | 33 | | set.Add(item); |
| | 3 | 34 | | return new PooledHashSet(set); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets a pooled HashSet populated from source |
| | | 39 | | /// </summary> |
| | | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 41 | | internal static PooledHashSet GetPooled(IEnumerable<string> source) |
| | | 42 | | { |
| | 165 | 43 | | var set = _pool.Get(); |
| | 540 | 44 | | foreach (var item in source) |
| | 105 | 45 | | set.Add(item); |
| | 165 | 46 | | return new PooledHashSet(set); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns HashSet to the pool |
| | | 51 | | /// </summary> |
| | | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 53 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Major Code Smell", "S3398:Move this method inside 'PooledHashSet'" |
| | 183 | 54 | | private static void Return(HashSet<string> set) => _pool.Return(set); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Zero-allocation ref struct wrapper with lock-free pooling |
| | | 58 | | /// </summary> |
| | | 59 | | internal readonly ref struct PooledHashSet |
| | | 60 | | { |
| | | 61 | | public readonly HashSet<string> Set; |
| | 183 | 62 | | internal PooledHashSet(HashSet<string> set) => Set = set; |
| | 183 | 63 | | public void Dispose() => Return(Set); |
| | | 64 | | } |
| | | 65 | | } |