@@ -92,9 +92,7 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
9292 MemoryPoolThrowHelper . ThrowObjectDisposedException ( MemoryPoolThrowHelper . ExceptionArgument . MemoryPool ) ;
9393 }
9494
95- //Interlocked.Increment(ref _rentCount);
96- //++_rentCount;
97- ScalableCount ( ref _rentCount ) ;
95+ Interlocked . Increment ( ref _rentCount ) ;
9896
9997 if ( _blocks . TryDequeue ( out var block ) )
10098 {
@@ -108,13 +106,11 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
108106
109107 _metrics . IncrementTotalMemory ( BlockSize ) ;
110108 _metrics . Rent ( BlockSize ) ;
111- //Interlocked.Increment(ref _rentCount);
112- //++_rentCount;
113109
114110 // We already counted this Rent call above, but since we're now allocating (need more blocks)
115111 // that means the pool is 'very' active and we probably shouldn't evict blocks, so we count again
116112 // to reduce the chance of eviction occurring this cycle.
117- ScalableCount ( ref _rentCount ) ;
113+ Interlocked . Increment ( ref _rentCount ) ;
118114
119115 return new MemoryPoolBlock ( this , BlockSize ) ;
120116 }
@@ -135,9 +131,7 @@ internal void Return(MemoryPoolBlock block)
135131 block . IsLeased = false ;
136132#endif
137133
138- //Interlocked.Increment(ref _returnCount);
139- //++_returnCount;
140- ScalableCount ( ref _returnCount ) ;
134+ Interlocked . Increment ( ref _returnCount ) ;
141135
142136 if ( ! _isDisposed )
143137 {
@@ -248,32 +242,4 @@ public void Dispose()
248242 {
249243 }
250244 }
251-
252- // https://github.com/dotnet/runtime/blob/db681fb307d754c3746ffb40e0634e4c4e0caa9e/docs/design/features/ScalableApproximateCounting.md
253- static void ScalableCount ( ref uint counter )
254- {
255- // Start using random for counting after 2^12 (4096)
256- //const int threshold = 12;
257- uint count = counter ;
258- uint delta = 1 ;
259- #if false
260- if ( count > 0 )
261- {
262- int logCount = 31 - ( int ) uint . LeadingZeroCount ( count ) ;
263-
264- if ( logCount >= threshold )
265- {
266- delta = 1u << ( logCount - ( threshold - 1 ) ) ;
267- uint rand = ( uint ) Random . Shared . Next ( ) ;
268- bool update = ( rand & ( delta - 1 ) ) == 0 ;
269- if ( ! update )
270- {
271- return ;
272- }
273- }
274- }
275- #endif
276-
277- Interlocked . Add ( ref counter , delta ) ;
278- }
279245}
0 commit comments