@@ -13,7 +13,8 @@ internal sealed class PinnedBlockMemoryPoolFactory : IMemoryPoolFactory<byte>, I
1313{
1414 private readonly IMeterFactory _meterFactory ;
1515 private readonly TimeProvider _timeProvider ;
16- private readonly ConcurrentDictionary < PinnedBlockMemoryPool , PinnedBlockMemoryPool > _pools = new ( ) ;
16+ // micro-optimization: Using nuint as the value type to avoid GC write barriers; could replace with ConcurrentHashSet if that becomes available
17+ private readonly ConcurrentDictionary < PinnedBlockMemoryPool , nuint > _pools = new ( ) ;
1718
1819 public PinnedBlockMemoryPoolFactory ( IMeterFactory meterFactory , TimeProvider ? timeProvider = null )
1920 {
@@ -25,11 +26,11 @@ public MemoryPool<byte> Create()
2526 {
2627 var pool = new PinnedBlockMemoryPool ( _meterFactory ) ;
2728
28- _pools . TryAdd ( pool , pool ) ;
29+ _pools . TryAdd ( pool , nuint . Zero ) ;
2930
3031 pool . OnPoolDisposed ( static ( state , self ) =>
3132 {
32- ( ( ConcurrentDictionary < PinnedBlockMemoryPool , PinnedBlockMemoryPool > ) state ! ) . TryRemove ( self , out _ ) ;
33+ ( ( ConcurrentDictionary < PinnedBlockMemoryPool , nuint > ) state ! ) . TryRemove ( self , out _ ) ;
3334 } , _pools ) ;
3435
3536 return pool ;
@@ -40,7 +41,7 @@ public void OnHeartbeat()
4041 var now = _timeProvider . GetUtcNow ( ) ;
4142 foreach ( var pool in _pools )
4243 {
43- pool . Value . TryScheduleEviction ( now ) ;
44+ pool . Key . TryScheduleEviction ( now ) ;
4445 }
4546 }
4647}
0 commit comments