Skip to content

Avoid intermediate strings in Razor component code generation - #86203

Merged
davidwengier merged 1 commit into
dotnet:mainfrom
davidwengier:razor-text-allocations
Oct 26, 2026
Merged

davidwengier merged 1 commit into
dotnet:mainfrom
davidwengier:razor-text-allocations

Conversation

@davidwengier

@davidwengier davidwengier commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Sadly there were no improvements possible to the major string allocations in the dumps in https://developercommunity.visualstudio.com/t/Significant-intellisense-lag-when-adding/11169845 but I found these little ones while looking anyway.

Scenario Before After Saved
Single 512-character HTML content token 1,048 B 0 B 1,048 B (100%)
Single 512-character HTML attribute value 1,496 B 448 B 1,048 B (70%)
Generic method emission with three cascading type parameters 1,200 B 992 B 208 B (17%)
Generic method emission with one cascading type parameter 1,104 B 992 B 112 B (10%)

Existing test coverage includes all of the scenarios being modified here.

Microsoft Reviewers: Open in CodeFlow

@davidwengier
davidwengier requested review from a team as code owners October 26, 2026 01:48
Copilot AI balanced review requested due to automatic review settings October 26, 2026 01:48
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The localized optimizations preserve emission semantics and existing fallbacks, with no unresolved review findings.

No open issues 🎉

What changed in this PR

This PR reduces temporary string allocations in Razor component code generation without changing the intended generated output.

Changes:

  • Writes generic type parameters directly to the code writer instead of constructing joined strings.
  • Reuses existing strings for single-token HTML content and attribute values.
  • Retains pooled-builder fallbacks for multi-token content.
File Description
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Components/​ComponentNodeWriter.cs Avoids intermediate strings when emitting type parameters, HTML content, and attribute values.

Review effort: Balanced

@davidwengier
davidwengier merged commit 9a64591 into dotnet:main Oct 26, 2026
25 checks passed
@davidwengier
davidwengier deleted the razor-text-allocations branch October 26, 2026 20:35
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Oct 26, 2026
@akhera99 akhera99 modified the milestones: Next, 18.13 Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants