Compiler: Avoid allocating strings for various variable and parameter names during code generation#12103
Conversation
...ysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.WriteInterpolatedStringHandler.cs
Show resolved
Hide resolved
| { | ||
| public static int CountDigits(this int number) | ||
| { | ||
| var value = number < 0 ? (uint)-(number + 1) + 1 : (uint)number; |
There was a problem hiding this comment.
Nit: I get what this is avoiding, but it took me a moment, and iirc you had to do something similar in a recent PR? Is there something we can extract?
There was a problem hiding this comment.
I did have to do something similar in #12101, is that what you were thinking of? It's not really shareable though.
There was a problem hiding this comment.
Yes, that's what I was thinking of. If I hadn't glanced through that PR, I probably would have struggled more with what this is doing. Could we leave a comment about using uint to avoid overflow on minval?
There was a problem hiding this comment.
Could you be less smart and just cast to a long? Ie, something like:
var value = number < 0 ? -(long)number : number;
There was a problem hiding this comment.
Just out of curiosity, was using a long less desirable?
`IWriteableValue` is an abstraction for values that can be written to a `CodeWriter`.
This is a straightforward extension method in int that will count the number of digits in the int.
Introduce `BuilderName` to avoid allocating a new string for every "builder" variable in scope stack.
These ensure that the compiler doesn't allocate new strings for the rendermode and formname variable names in each scope.
- Remove #nullable disable - Mark as sealed - Store current entry in field and defer creating stack - Add Scope ref struct to auto-close scopes. - Avoid boxing `CSharpCodeWritingScope` to `IDisposable`
Avoid allocating extra strings for generic type parameters and arguments.
Add debugger display for BuilderVariableName, RenderModeVariableName, FormNameVariableName, SeqName, ParameterName, and TypeInferenceArgName.
ed2bd41 to
28e803a
Compare
This change is targeted primarily at
ScopeStackandTypeInferenceMethodParameter. Each of these holds onto strings that are created for variable names written during code generation. These variable names all take the form of some well-formed prefix and a suffix generated from int values, i.e."__seq0". We can avoid allocating these strings by providing lightweight abstractions to hold the bits that change (i.e. the suffix data) and write the data directly.CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2771119&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/661321
Toolset Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2771120&view=results