-
Notifications
You must be signed in to change notification settings - Fork 215
Improve performance and memory usage of TagHelperBinder #11671
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
Merged
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
Don't expose the Mappings dictionary. Instead, expose a GetBoundRules(...) method and update each access.
This change uses a single HashSet<TagHelperDescriptor> to ensure that there are no duplicates. This avoids needing to use a HashSet<TagHelperDescriptor> per rule.
Instead of creating a new Dictionary with an arbitrary capacity, use a pooled Dictionary to build the initial map and copy the results to a new Dictionary with the precise capacity.
This avoids an extra Dictionary lookup for '*' during binding.
Frozen dictionaries are expensive to construct and should be avoided in binding. Instead, this change updates TagHelperBinding to store all bound rules as an array of TagHelperBoundRulesInfo, which is a struct containing the mapping of TagHelperDescriptor to bound rules.
A common pattern is to iterate over TagHelperBinding.Descriptors, call GetBoundRules for each descriptor and iterate over the rules. Because TagHelperBinding no longer contains a Dictionary, these double-loops are now O(n^2). Fortunately, it's easy to just iterate over AllBoundRules, which is what these cases are really trying to do.
- Store TagHelperDocumentContext in a field on RazorCodeDocument rather than in ItemCollection. - Move the GetTagHelperDocumentContext() extension method to RazorCodeDocument. - Move the SetTagHelperDocumentContext() extension method to RazorCodeDocument. - Add RazorCodeDocument.TryGetTagHelperDocumentContext(...) method. - Audit and update uses of GetTagHelperDocumentContext() Important note: This change includes a nullability change to one of the Legacy Editor shim's APIs. This change makes the API correct and I verified that Web Tools already handles null for this API.
davidwengier
approved these changes
Mar 28, 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.
✅ Tooling
jjonescz
reviewed
Mar 31, 2025
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TagHelperBinder.cs
Outdated
Show resolved
Hide resolved
src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/Parsing/VisualStudioRazorParser.cs
Outdated
Show resolved
Hide resolved
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TagHelperBinder.cs
Outdated
Show resolved
Hide resolved
jjonescz
approved these changes
Mar 31, 2025
333fred
approved these changes
Mar 31, 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.
One small comment, otherwise LGTM
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TagHelperBinder.cs
Outdated
Show resolved
Hide resolved
This code can be simplified, since the null check will be performed on our behalf.
This was referenced Apr 11, 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.
While working on #11655, I noticed that
TagHelperBinder
accounts for a surprising amount of memory. This pull request aims to clean it up, reduce the amount of memory it holds onto, and improve the performance of binding operations. I recommend reviewing commit-by-commit as most commits have descriptions to aid reviewers.CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2675893&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/623866