Skip to content

Conversation

DustinCampbell
Copy link
Member

@DustinCampbell DustinCampbell commented Mar 27, 2025

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

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.
@DustinCampbell DustinCampbell requested review from a team as code owners March 27, 2025 23:52
Copy link
Member

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

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

✅ Tooling

@DustinCampbell
Copy link
Member Author

cc @chsienki, @333fred, @jjonescz and @jaredpar for compiler reviews.

Copy link
Member

@333fred 333fred left a 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

This code can be simplified, since the null check will be performed on our behalf.
@DustinCampbell DustinCampbell merged commit 3a13c27 into main Apr 1, 2025
11 checks passed
@DustinCampbell DustinCampbell deleted the dev/dustinca/improve-binder-memory branch April 1, 2025 20:41
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 1, 2025
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 20, 2025
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.

5 participants