-
Notifications
You must be signed in to change notification settings - Fork 217
Compiler: Reduce allocations in tag helper matching #12134
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
Merged
DustinCampbell
merged 13 commits into
dotnet:main
from
DustinCampbell:reduce-allocations-in-tag-helper-matching
Aug 22, 2025
Merged
Compiler: Reduce allocations in tag helper matching #12134
DustinCampbell
merged 13 commits into
dotnet:main
from
DustinCampbell:reduce-allocations-in-tag-helper-matching
Aug 22, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It's unnecessary to create a new List<TagHelperDescriptor> for the TagHelperIntermediateNode.TagHelpers property and add tag helpers to it. Since TagHelperBinding.Descriptors is already an ImmutableArray<TagHelperDescriptor>, it can just be assigned to TagHelperIntermediateNode.TagHelpers.
Many intermediate nodes store several related tag helper objects. For example, TagHelperPropertyIntermediateNode stores both a BoundAttributeDesccriptor and a TagHelperDescriptor. However, for all intermediate nodes, the tag helper objects are always have a parent-child relationship. Now that tag helper objects actually provide a Parent property, we can use that and avoid extra fields.
TagHelperMatchingConventions.TryGetFirstBoundAttributeMatch(...) has four out parameters. This change consolidates them into a single out parameter of type TagHelperAttributeMatch. This allows some logic based on the out parameters to be encapsulated, such as whether a match expects a boolean or string value.
Add helper method to collect matches and use it throughout DefaultRazorIntermediateNodeLoweringPhase to reduce LINQ-related allocations.
The polyfill extension method in SpanExtensions were causing ambiguities on .NET builds.
Introduce DirectiveAttributeName helper struct that uses spans to defer creating strings until necessary.
- Enable nullability - Make Children property lazy - Remove unnecessary argument-null checks - Add constructor that takes an TagHelperAttributeMatch and verifies that its a parameter match. - Change IsIndexerNameMatch and BoundAttributeParameter properties to computed properties that return values from TagHelperAttributeMatch. - Change other properties to init-only - Make AttributeName, AttributeNameWithoutParameter, OriginalAttributeName, and AttributeStructure as required.
- Enable nullability - Make Children property lazy - Remove unnecessary argument-null checks - Add constructor that takes an TagHelperAttributeMatch - Change IsIndexerNameMatch and BoundAttribute properties to computed properties that return values from TagHelperAttributeMatch. - Change other properties to init-only - Make AttributeName, OriginalAttributeName, and AttributeStructure as required.
- Enable nullability - Make Children property lazy - Remove unnecessary argument-null checks - Add constructor that takes an TagHelperAttributeMatch - Change IsIndexerNameMatch and BoundAttribute properties to computed properties that return values from TagHelperAttributeMatch. - Change other properties to init-only - Make AttributeName, and AttributeStructure as required.
- Enable nullability - Make Children property lazy - Remove unnecessary argument-null checks - Change properties to init-only - Mark AttributeName, and AttributeStructure as required.
- Enable nullability - Make Children property lazy - Remove unnecessary argument-null checks - Change properties to init-only - Mark TagMode, and TagName as required.
ToddGrun
reviewed
Aug 21, 2025
...Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/TagHelperBlockRewriter.cs
Show resolved
Hide resolved
ToddGrun
reviewed
Aug 21, 2025
...rosoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorIntermediateNodeLoweringPhase.cs
Outdated
Show resolved
Hide resolved
333fred
approved these changes
Aug 21, 2025
ToddGrun
approved these changes
Aug 21, 2025
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.
![]()
davidwengier
approved these changes
Aug 22, 2025
This was referenced Aug 26, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change simplifies and reduces allocations during tag helper matching in
DefaultRazorIntermediateNodeLoweringPhase. In addition, a handful of related intermediate nodes have been cleaned up somewhat, reducing the number of fields and making things a bit lazier. I recommend reviewing commit-by-commit as each commit has a descriptive message.CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2777461&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/663965
Toolset Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2777462&view=results