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

Slightly reduce allocations in SymbolCompletionItem.AddSymbolInfo #76418

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

ToddGrun
Copy link
Contributor

This code creates a string from the SymbolKind enum and was showing up as 0.2% of allocations in the typing scenario in the csharp editing speedometer test.

Instead, mimic what the core runtime does in the Number class by keeping a small cache mapping int => string that is lazily populated.

I did check to see whether other places in the profile implicating FormatInt32 were candidates for changing to this, and neither of the two found locations fit well. The call from SymbolCompletionItem.CreateWorker has values that are too large and varied to consider caching, and the usage in SymbolKeyWriter.WriteIntegerRaw_DoNotCallDirectly would require the helper class to be available at the CodeStyle layer.

*** Allocations targeted with this PR ***
image

This code creates a string from the SymbolKind enum and was showing up as 0.2% of allocations in the typing scenario in the csharp editing speedometer test.

Instead, mimic what the core runtime does in the Number class by keeping a small cache mapping int => string that is lazily populated.
@ToddGrun ToddGrun requested a review from a team as a code owner December 13, 2024 15:12
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 13, 2024

[MethodImpl(MethodImplOptions.NoInlining)] // keep rare usage out of fast path
static string CreateAndCacheString(int value)
=> s_smallNumberCache[value] = value.ToString();
Copy link
Member

Choose a reason for hiding this comment

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

already an issue, but perhaps make this ordinal? seems odd that this would be impated by locale

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think the int32.ToString no arg implementation of this method is locale sensitive.

Copy link
Member

Choose a reason for hiding this comment

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

ah ok.

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