-
Notifications
You must be signed in to change notification settings - Fork 219
Optimize allocations under RuntimeNodeWriter.WriteHtmlContent #11945
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
Optimize allocations under RuntimeNodeWriter.WriteHtmlContent #11945
Conversation
This method was creating a StringBuilder with unknown size allocating both a final string and char array along with resize allocations. Instead, calculate the result length and calculate a single char array. This reduced allocations in the typing scenario in the razor lsp speedometer test under this method from about 1.1% to 0.4%.
DustinCampbell
left a comment
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.
Looks good!
| WriteLiteral(textToRender); | ||
|
|
||
| charactersConsumed += textToRender.Length; | ||
| } while (charactersConsumed < literal.Length); |
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.
Consider changing this method to slice literal repeatedly rather than tracking charactersConsumed and charactersRemaining.
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.
OK, I think that simplified nicely. Please verify my logic as it changed a fair bit.
DustinCampbell
left a comment
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.
Nice!
...piler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/RuntimeNodeWriter.cs
Show resolved
Hide resolved
|
@dotnet/razor-compiler for 2nd review |
This method was creating a StringBuilder with unknown size allocating both a final string and char array along with resize allocations. Instead, calculate the result length and calculate a single char array. This reduced allocations in the typing scenario in the razor lsp speedometer test under this method from about 3.8% to 2.3%.
Speedometer run: https://dev.azure.com/devdiv/DevDiv/_apps/hub/ms-vseng.pit-vsengPerf.pit-hub?targetBuild=10711.132.dn-bot.250612.042302.643010&targetBranch=main&targetPerfBuildId=11746459&runGroup=Speedometer&baselineBuild=10711.80&baselineBranch=main
Allocations before change

Allocations with change
