Skip to content

Conversation

@ToddGrun
Copy link
Contributor

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
image

Allocations with change
image

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%.
@ToddGrun ToddGrun requested a review from a team as a code owner June 12, 2025 13:34
Copy link
Member

@DustinCampbell DustinCampbell left a 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);
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

Nice!

@ToddGrun
Copy link
Contributor Author

@dotnet/razor-compiler for 2nd review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants