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

Reduce lock usage during EditDistance.GetEditDistance and BKTree.Find #75610

Conversation

ToddGrun
Copy link
Contributor

The SimplePool and ArrayPool classes previously always locked on Allocate/Free which is showing up in a cpu usage in a trace David Kean referred to us. Instead, as the char arrays being allocated are typically quite small, utilize the stack for these temporary char array locations when possible, falling back to heap allocating in the extremely rare occasions where it's not.

Below is the cpu usage that David pointed us to:
image

The SimplePool and ArrayPool classes previously used always locked on Allocate/Free which is showing up in a trace David Kean referred us to. Instead, as the char arrays being allocated are typically quite small, utilize the stack for these temporary char array locations when possible, falling back to heap allocating in the rare occasions where it's not.
@ToddGrun ToddGrun requested a review from a team as a code owner October 23, 2024 21:44
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 23, 2024
lowerCaseCharacters[i] = CaseInsensitiveComparison.ToLower(value[i]);
Span<char> lowerCaseCharacters = value.Length < 512
? stackalloc char[value.Length]
: new char[value.Length];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still use the pool for large strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just here, or in EditDistance too? I'm not sure about here, but I wouldn't think the EditDistance code would almost ever exceed 512 chars.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure, I don't think the old code was pooling large strings, as ArrayPool checked against MaxPooledArraySize for both retrieving and returning from the pool.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Never mind :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants