Compiler: Avoid allocating strings with writing integer literals#12101
Merged
DustinCampbell merged 2 commits intodotnet:mainfrom Aug 18, 2025
Merged
Conversation
Whenever the Razor compiler writes a integer value, it calls `ToString(CultureInfo.InvariantCulture)` on the integer to allocate a new string. To avoid these extra strings, this change introduces a new `CodeWriter.WriteIntegerLiteral(int)` extension method that can directly write an integer value with invariant culture. This method uses a lookup table of precomputed string values to avoid allocations.
Use the `CodeWriter.WriteIntegerLiteral` extension method in the Razor compiler to avoid extra string allocations (where possible).
davidwengier
approved these changes
Aug 12, 2025
Member
davidwengier
left a comment
There was a problem hiding this comment.
I wonder how long a test would take if you just use [CombinatorialData] :D
chsienki
approved these changes
Aug 13, 2025
| private static readonly ReadOnlyMemory<char> s_true = "true".AsMemory(); | ||
| private static readonly ReadOnlyMemory<char> s_false = "false".AsMemory(); | ||
|
|
||
| private static readonly ReadOnlyMemory<char> s_zeroes = "0000000000".AsMemory(); // 10 zeros |
Member
There was a problem hiding this comment.
Why we do we need so many zeros? We only ever need up to three, right?
Member
Author
There was a problem hiding this comment.
10 is the largest number of 0s in an int. We don't need the extras for this algorithm, but it seemed reasonable (at the time 😄) to just put the maximum in there.
Member
Author
There was a problem hiding this comment.
@chsienki: I'll go ahead and merge since you didn't block on this. However, I'm happy to make a change here if you like.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When writing integer literals, the compiler first allocates a string using the invariant culture and then writes the string. This is pretty wasteful and the compiler writes a lot of integer literals for
#linedirectives. This change introduces a newCodeWriter.WriteIntegerLiteralfunction that writes integers directly without allocations using a lookup table.CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2770483&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/661077
Toolset Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2770489&view=results