Skip to content

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

Closed

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

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Apr 14, 2024
  2. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Apr 14, 2024
  3. dotnet-policy-service commented on Apr 14, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  4. stephentoub commented on Apr 22, 2024

    @stephentoub
    Member

    @JeremyKuhne, it looks like this originates from dotnet/coreclr#17078. Do you remember what was the intention here?

  5. JeremyKuhne commented on Apr 22, 2024

    @JeremyKuhne
    Member

    Presumably I didn't start with trim everything and missed that I upped it to everything? Or meant to pull it back from everything?

  6. stephentoub commented on Apr 23, 2024

    @stephentoub
    Member

    Ok, thanks

  7. locked and limited conversation to collaborators on May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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