Skip to content

Commit aa7f1d0

Browse files
committed
nuint
1 parent 35cac4e commit aa7f1d0

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/Servers/Kestrel/Core/src/Internal/PinnedBlockMemoryPoolFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Servers/Kestrel/Core/test/PinnedBlockMemoryPoolFactoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void DisposePoolRemovesFromFactory()
4040
var pool = factory.Create();
4141
Assert.NotNull(pool);
4242

43-
var dict = (ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool>)(typeof(PinnedBlockMemoryPoolFactory)
43+
var dict = (ConcurrentDictionary<PinnedBlockMemoryPool, nuint>)(typeof(PinnedBlockMemoryPoolFactory)
4444
.GetField("_pools", BindingFlags.NonPublic | BindingFlags.Instance)
4545
?.GetValue(factory));
4646
Assert.Single(dict);

0 commit comments

Comments
 (0)