@@ -41,7 +41,7 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
4141 /// </summary>
4242 private bool _isDisposed ; // To detect redundant calls
4343
44- private readonly PinnedBlockMemoryPoolMetrics _metrics ;
44+ private readonly PinnedBlockMemoryPoolMetrics ? _metrics ;
4545
4646 private long _currentMemory ;
4747 private long _evictedMemory ;
@@ -60,14 +60,9 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
6060 /// </summary>
6161 private const int AnySize = - 1 ;
6262
63- public PinnedBlockMemoryPool ( )
64- : this ( NoopMeterFactory . Instance )
63+ public PinnedBlockMemoryPool ( IMeterFactory ? meterFactory = null )
6564 {
66- }
67-
68- public PinnedBlockMemoryPool ( IMeterFactory meterFactory )
69- {
70- _metrics = new ( meterFactory ) ;
65+ _metrics = meterFactory is null ? null : new PinnedBlockMemoryPoolMetrics ( meterFactory ) ;
7166 }
7267
7368 /// <summary>
@@ -95,16 +90,16 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
9590
9691 if ( _blocks . TryDequeue ( out var block ) )
9792 {
98- _metrics . UpdateCurrentMemory ( - block . Memory . Length ) ;
99- _metrics . Rent ( block . Memory . Length ) ;
93+ _metrics ? . UpdateCurrentMemory ( - block . Memory . Length ) ;
94+ _metrics ? . Rent ( block . Memory . Length ) ;
10095 Interlocked . Add ( ref _currentMemory , - block . Memory . Length ) ;
10196
10297 // block successfully taken from the stack - return it
10398 return block ;
10499 }
105100
106- _metrics . IncrementTotalMemory ( BlockSize ) ;
107- _metrics . Rent ( BlockSize ) ;
101+ _metrics ? . IncrementTotalMemory ( BlockSize ) ;
102+ _metrics ? . Rent ( BlockSize ) ;
108103
109104 // We already counted this Rent call above, but since we're now allocating (need more blocks)
110105 // that means the pool is 'very' active and we probably shouldn't evict blocks, so we count again
@@ -134,7 +129,7 @@ internal void Return(MemoryPoolBlock block)
134129
135130 if ( ! _isDisposed )
136131 {
137- _metrics . UpdateCurrentMemory ( block . Memory . Length ) ;
132+ _metrics ? . UpdateCurrentMemory ( block . Memory . Length ) ;
138133 Interlocked . Add ( ref _currentMemory , block . Memory . Length ) ;
139134
140135 _blocks . Enqueue ( block ) ;
@@ -201,8 +196,8 @@ internal void PerformEviction()
201196 // Remove from queue and let GC clean the memory up
202197 while ( burstAmount > 0 && _blocks . TryDequeue ( out var block ) )
203198 {
204- _metrics . UpdateCurrentMemory ( - block . Memory . Length ) ;
205- _metrics . EvictBlock ( block . Memory . Length ) ;
199+ _metrics ? . UpdateCurrentMemory ( - block . Memory . Length ) ;
200+ _metrics ? . EvictBlock ( block . Memory . Length ) ;
206201 Interlocked . Add ( ref _currentMemory , - block . Memory . Length ) ;
207202 Interlocked . Add ( ref _evictedMemory , block . Memory . Length ) ;
208203
@@ -236,15 +231,4 @@ protected override void Dispose(bool disposing)
236231
237232 // Used for testing
238233 public int BlockCount ( ) => _blocks . Count ;
239-
240- private sealed class NoopMeterFactory : IMeterFactory
241- {
242- public static NoopMeterFactory Instance { get ; } = new ( ) ;
243-
244- public Meter Create ( MeterOptions options ) => new Meter ( options ) ;
245-
246- public void Dispose ( )
247- {
248- }
249- }
250234}
0 commit comments