Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Enderlook opened this issue Apr 14, 2024 · 4 comments · Fixed by #101410
Closed

Comments

@Enderlook
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 14, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 14, 2024
@danmoseley danmoseley added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 14, 2024
Copy link
Contributor

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

@stephentoub
Copy link
Member

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

@JeremyKuhne
Copy link
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?

@stephentoub
Copy link
Member

Ok, thanks

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants