@@ -40,10 +40,7 @@ internal sealed partial class SharedArrayPool<T> : ArrayPool<T>
40
40
/// <summary>Allocate a new <see cref="SharedArrayPoolPartitions"/> and try to store it into the <see cref="_buckets"/> array.</summary>
41
41
private unsafe SharedArrayPoolPartitions CreatePerCorePartitions ( int bucketIndex )
42
42
{
43
- #pragma warning disable 8500 // sizeof of managed types
44
- int elementSize = sizeof ( T ) ;
45
- #pragma warning restore 8500
46
- var inst = new SharedArrayPoolPartitions ( elementSize ) ;
43
+ var inst = new SharedArrayPoolPartitions ( ) ;
47
44
return Interlocked . CompareExchange ( ref _buckets [ bucketIndex ] , inst , null ) ?? inst ;
48
45
}
49
46
@@ -199,7 +196,7 @@ public bool Trim()
199
196
SharedArrayPoolPartitions ? [ ] perCoreBuckets = _buckets ;
200
197
for ( int i = 0 ; i < perCoreBuckets . Length ; i ++ )
201
198
{
202
- perCoreBuckets [ i ] ? . Trim ( currentMilliseconds , Id , pressure , Utilities . GetMaxSizeForBucket ( i ) ) ;
199
+ perCoreBuckets [ i ] ? . Trim ( currentMilliseconds , Id , pressure ) ;
203
200
}
204
201
205
202
// Trim each of the TLS buckets. Note that threads may be modifying their TLS slots concurrently with
@@ -323,14 +320,13 @@ internal sealed class SharedArrayPoolPartitions
323
320
private readonly Partition [ ] _partitions ;
324
321
325
322
/// <summary>Initializes the partitions.</summary>
326
- /// <param name="elementSize">The size of the elements stored in arrays.</param>
327
- public SharedArrayPoolPartitions ( int elementSize )
323
+ public SharedArrayPoolPartitions ( )
328
324
{
329
325
// Create the partitions. We create as many as there are processors, limited by our max.
330
326
var partitions = new Partition [ SharedArrayPoolStatics . s_partitionCount ] ;
331
327
for ( int i = 0 ; i < partitions . Length ; i ++ )
332
328
{
333
- partitions [ i ] = new Partition ( elementSize ) ;
329
+ partitions [ i ] = new Partition ( ) ;
334
330
}
335
331
_partitions = partitions ;
336
332
}
@@ -374,23 +370,20 @@ public bool TryPush(Array array)
374
370
return null ;
375
371
}
376
372
377
- public void Trim ( int currentMilliseconds , int id , Utilities . MemoryPressure pressure , int bucketSize )
373
+ public void Trim ( int currentMilliseconds , int id , Utilities . MemoryPressure pressure )
378
374
{
379
375
Partition [ ] partitions = _partitions ;
380
376
for ( int i = 0 ; i < partitions . Length ; i ++ )
381
377
{
382
- partitions [ i ] . Trim ( currentMilliseconds , id , pressure , bucketSize ) ;
378
+ partitions [ i ] . Trim ( currentMilliseconds , id , pressure ) ;
383
379
}
384
380
}
385
381
386
382
/// <summary>Provides a simple, bounded stack of arrays, protected by a lock.</summary>
387
- /// <param name="elementSize">The size of the elements stored in arrays.</param>
388
- private sealed class Partition ( int elementSize )
383
+ private sealed class Partition
389
384
{
390
385
/// <summary>The arrays in the partition.</summary>
391
386
private readonly Array ? [ ] _arrays = new Array [ SharedArrayPoolStatics . s_maxArraysPerPartition ] ;
392
- /// <summary>The size of the elements stored in arrays.</summary>
393
- private readonly int _elementSize = elementSize ;
394
387
/// <summary>Number of arrays stored in <see cref="_arrays"/>.</summary>
395
388
private int _count ;
396
389
/// <summary>Timestamp set by Trim when it sees this as 0.</summary>
@@ -437,20 +430,11 @@ public bool TryPush(Array array)
437
430
return arr ;
438
431
}
439
432
440
- public void Trim ( int currentMilliseconds , int id , Utilities . MemoryPressure pressure , int bucketSize )
433
+ public void Trim ( int currentMilliseconds , int id , Utilities . MemoryPressure pressure )
441
434
{
442
435
const int TrimAfterMS = 60 * 1000 ; // Trim after 60 seconds for low/moderate pressure
443
436
const int HighTrimAfterMS = 10 * 1000 ; // Trim after 10 seconds for high pressure
444
437
445
- const int LargeBucket = 16384 ; // If the bucket is larger than this we'll trim an extra when under high pressure
446
-
447
- const int ModerateTypeSize = 16 ; // If T is larger than this we'll trim an extra when under high pressure
448
- const int LargeTypeSize = 32 ; // If T is larger than this we'll trim an extra (additional) when under high pressure
449
-
450
- const int LowTrimCount = 1 ; // Trim one item when pressure is low
451
- const int MediumTrimCount = 2 ; // Trim two items when pressure is moderate
452
- int HighTrimCount = SharedArrayPoolStatics . s_maxArraysPerPartition ; // Trim all items when pressure is high
453
-
454
438
if ( _count == 0 )
455
439
{
456
440
return ;
@@ -477,38 +461,16 @@ public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure press
477
461
}
478
462
479
463
// We've elapsed enough time since the first item went into the partition.
480
- // Drop the top item so it can be collected and make the partition look a little newer .
464
+ // Drop the top item(s) so they can be collected.
481
465
482
- ArrayPoolEventSource log = ArrayPoolEventSource . Log ;
483
- int trimCount = LowTrimCount ;
484
- switch ( pressure )
466
+ int trimCount = pressure switch
485
467
{
486
- case Utilities . MemoryPressure . High :
487
- trimCount = HighTrimCount ;
488
-
489
- // When pressure is high, aggressively trim larger arrays.
490
- if ( bucketSize > LargeBucket )
491
- {
492
- trimCount ++ ;
493
- }
494
-
495
- if ( _elementSize > ModerateTypeSize )
496
- {
497
- trimCount ++ ;
498
-
499
- if ( _elementSize > LargeTypeSize )
500
- {
501
- trimCount ++ ;
502
- }
503
- }
504
-
505
- break ;
506
-
507
- case Utilities . MemoryPressure . Medium :
508
- trimCount = MediumTrimCount ;
509
- break ;
510
- }
468
+ Utilities . MemoryPressure . High => SharedArrayPoolStatics . s_maxArraysPerPartition ,
469
+ Utilities . MemoryPressure . Medium => 2 ,
470
+ _ => 1 ,
471
+ } ;
511
472
473
+ ArrayPoolEventSource log = ArrayPoolEventSource . Log ;
512
474
while ( _count > 0 && trimCount -- > 0 )
513
475
{
514
476
Array ? array = _arrays [ -- _count ] ;
0 commit comments