Skip to content

I think that Partition.Trim(...) from ArraySharedPool<T> may be doing some unnecessary work #101025

@Enderlook

Description

@Enderlook

Hi, I was reading the source of SharedArrayPool<T> for curiosity and I think I may have found that it contains some old code which is irrelevant in the current implementation.

Here the array length inside the Partition class is defined:

private readonly Array?[] _arrays = new Array[SharedArrayPoolStatics.s_maxArraysPerPartition];

Here the trimming says that when there is high memory pressure it should clean a certain amount of elements:

int HighTrimCount = SharedArrayPoolStatics.s_maxArraysPerPartition; // Trim all items when pressure is high

You can realize that the cleaning amount is the same value used to declare the array length, in other words, it cleans the entire array.

That is used here:

int trimCount = LowTrimCount;
switch (pressure)
{
case Utilities.MemoryPressure.High:
trimCount = HighTrimCount;

Then, why does the next line add additional elements to trim based on the rented array lengths and element size?

int trimCount = LowTrimCount;
switch (pressure)
{
case Utilities.MemoryPressure.High:
trimCount = HighTrimCount;
// When pressure is high, aggressively trim larger arrays.
if (bucketSize > LargeBucket)
{
trimCount++;
}
if (_elementSize > ModerateTypeSize)
{
trimCount++;
if (_elementSize > LargeTypeSize)
{
trimCount++;
}
}
break;

It is already trimming the entire array, asking to trim more won't do anything as far as I know.

If this is not the case, could you please explain me the reasoning under that code? Thanks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions