Skip to content

Commit 4b297b3

Browse files
stephentoubmichaelgsharp
authored andcommitted
Remove some dead code from SharedArrayPool (dotnet#101410)
In Trim, for high memory pressure, we're already setting the number of arrays to trim equal to the max number possible, so the subsequent logic that would increase the count wasn't useful. Deleting that also deleted the last use of _elementSize, which in turn allowed for the path that provided that value to be cleaned up.
1 parent 6a6376e commit 4b297b3

File tree

1 file changed

+15
-53
lines changed

1 file changed

+15
-53
lines changed

src/libraries/System.Private.CoreLib/src/System/Buffers/SharedArrayPool.cs

+15-53
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ internal sealed partial class SharedArrayPool<T> : ArrayPool<T>
4040
/// <summary>Allocate a new <see cref="SharedArrayPoolPartitions"/> and try to store it into the <see cref="_buckets"/> array.</summary>
4141
private unsafe SharedArrayPoolPartitions CreatePerCorePartitions(int bucketIndex)
4242
{
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();
4744
return Interlocked.CompareExchange(ref _buckets[bucketIndex], inst, null) ?? inst;
4845
}
4946

@@ -199,7 +196,7 @@ public bool Trim()
199196
SharedArrayPoolPartitions?[] perCoreBuckets = _buckets;
200197
for (int i = 0; i < perCoreBuckets.Length; i++)
201198
{
202-
perCoreBuckets[i]?.Trim(currentMilliseconds, Id, pressure, Utilities.GetMaxSizeForBucket(i));
199+
perCoreBuckets[i]?.Trim(currentMilliseconds, Id, pressure);
203200
}
204201

205202
// 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
323320
private readonly Partition[] _partitions;
324321

325322
/// <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()
328324
{
329325
// Create the partitions. We create as many as there are processors, limited by our max.
330326
var partitions = new Partition[SharedArrayPoolStatics.s_partitionCount];
331327
for (int i = 0; i < partitions.Length; i++)
332328
{
333-
partitions[i] = new Partition(elementSize);
329+
partitions[i] = new Partition();
334330
}
335331
_partitions = partitions;
336332
}
@@ -374,23 +370,20 @@ public bool TryPush(Array array)
374370
return null;
375371
}
376372

377-
public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure pressure, int bucketSize)
373+
public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure pressure)
378374
{
379375
Partition[] partitions = _partitions;
380376
for (int i = 0; i < partitions.Length; i++)
381377
{
382-
partitions[i].Trim(currentMilliseconds, id, pressure, bucketSize);
378+
partitions[i].Trim(currentMilliseconds, id, pressure);
383379
}
384380
}
385381

386382
/// <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
389384
{
390385
/// <summary>The arrays in the partition.</summary>
391386
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;
394387
/// <summary>Number of arrays stored in <see cref="_arrays"/>.</summary>
395388
private int _count;
396389
/// <summary>Timestamp set by Trim when it sees this as 0.</summary>
@@ -437,20 +430,11 @@ public bool TryPush(Array array)
437430
return arr;
438431
}
439432

440-
public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure pressure, int bucketSize)
433+
public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure pressure)
441434
{
442435
const int TrimAfterMS = 60 * 1000; // Trim after 60 seconds for low/moderate pressure
443436
const int HighTrimAfterMS = 10 * 1000; // Trim after 10 seconds for high pressure
444437

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-
454438
if (_count == 0)
455439
{
456440
return;
@@ -477,38 +461,16 @@ public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure press
477461
}
478462

479463
// 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.
481465

482-
ArrayPoolEventSource log = ArrayPoolEventSource.Log;
483-
int trimCount = LowTrimCount;
484-
switch (pressure)
466+
int trimCount = pressure switch
485467
{
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+
};
511472

473+
ArrayPoolEventSource log = ArrayPoolEventSource.Log;
512474
while (_count > 0 && trimCount-- > 0)
513475
{
514476
Array? array = _arrays[--_count];

0 commit comments

Comments
 (0)