-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
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.
|
||
[MethodImpl(MethodImplOptions.NoInlining)] // keep rare usage out of fast path | ||
static string CreateAndCacheString(int value) | ||
=> s_smallNumberCache[value] = value.ToString(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok.
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](https://private-user-images.githubusercontent.com/6785178/395605381-3f34609e-7bac-48eb-bd1e-fcd8bc93839f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwNTg0NDMsIm5iZiI6MTczOTA1ODE0MywicGF0aCI6Ii82Nzg1MTc4LzM5NTYwNTM4MS0zZjM0NjA5ZS03YmFjLTQ4ZWItYmQxZS1mY2Q4YmM5MzgzOWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMjM0MjIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NTM5ODQyODc3YzMyNTlmMDY3MjhkMjZjODQ0YmQ1YTRmYzg0YTZhNWY5MzYwMDdjNThiOWYzOTJkZTkxZWJlMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.-nGIJqn8VSCeYUi5ffOOxifIkQWBw0Mz91-4_Qn2JqA)