Skip to content

Reduce allocations from Razor type inference - #86206

Merged
davidwengier merged 2 commits into
dotnet:mainfrom
davidwengier:razor-inference-allocations
Oct 26, 2026
Merged

davidwengier merged 2 commits into
dotnet:mainfrom
davidwengier:razor-inference-allocations

Conversation

@davidwengier

@davidwengier davidwengier commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

In the dumps in https://developercommunity.visualstudio.com/t/Significant-intellisense-lag-when-adding/11169845 there were allocations showing up in handling generic components to do with .ToList() and .OfType() usage. This replaces those with pooled lists, and a custom struct enumerator. I also rolled the changes out to other places with the same pattern, that didn't strictly show up in the dumps.

Reductions are up to about 55% depending on the complexity of the Razor code being compiled, though to be fair some of the improvements are only around 5%.

Microsoft Reviewers: Open in CodeFlow

@davidwengier
davidwengier requested review from a team as code owners October 26, 2026 05:27
Copilot AI balanced review requested due to automatic review settings October 26, 2026 05:27
@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.

🔵 Needs a closer look

Focused coverage of the custom enumerator’s lifecycle and interface behavior is missing, leaving its compatibility insufficiently verified for automated approval.

1 open issue
What changed in this PR

This PR reduces allocations in Razor generic-component processing by replacing temporary lists with pooled collections and LINQ-based child filtering with a struct enumerator.

Changes:

  • Adds a typed child view and enumerator for component and markup nodes.
  • Pools temporary lists used during generic-type inference and code generation.
  • Preserves snapshots when removing dictionary entries during iteration.
File Description
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Intermediate/​MarkupElementIntermediateNode.cs Uses typed views for attributes, captures, and keys.
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Intermediate/​IntermediateNodeCollection.TypedEnumerable.cs Adds allocation-conscious filtering and enumeration.
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Intermediate/​ComponentIntermediateNode.cs Uses typed views for component children.
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Components/​ComponentNodeWriter.cs Pools type-inference parameter lists.
src/​Razor/​src/​Compiler/​Microsoft.CodeAnalysis.Razor.Compiler/​src/​Language/​Components/​ComponentGenericTypePass.cs Pools temporary type-parameter and binding lists.

Review effort: Balanced

@davidwengier
davidwengier merged commit f6fcd14 into dotnet:main Oct 26, 2026
25 checks passed
@davidwengier
davidwengier deleted the razor-inference-allocations branch October 26, 2026 20:34
@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.

4 participants