Skip to content
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

Don't hold onto Roslyn projects longer than necessary #11458

Merged

Conversation

DustinCampbell
Copy link
Member

@DustinCampbell DustinCampbell commented Feb 6, 2025

In general, it's good practice to avoid holding onto Roslyn projects longer than necessary because it results in a lot of memory hanging around for the GC to clean up at once. Unfortunately, that is exactly what Razor's WorkspaceProjectStateChangeDetector does. It holds Roslyn projects and Razor project snapshots in an async batching work queue, only to throw most of them away when the batch is processed. This change avoids that by changing the work queue to hold Razor ProjectKeys and Roslyn ProjectIds. In addition, a lot of refactoring has been done to ensure that the code is more readable, more maintainable, and has far fewer allocations. In particular, there were a lot of extra string allocations coming from file path normalization because ProjectKeys were being created over and over again. So, a simple cache has been introduced so that a ProjectKey is only created once per assembly path.

As part of this change, I've renamed and organized all of the files related to tag helper discovery in Microsoft.VisualsStudio.LanguageServices.Razor. So, I recommend reviewing commit-by-commit, because the final PR looks like a bunch of new files replacing deleted files.

CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2636157&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/608737

Calling Project.Matches(...) rather than Project.ToProjectKey() avoids a string allocation per project.
Both the WorkspaceProjectStateChangeDetector and ProjectWorkspaceStateGenerator capture Roslyn Projects and Razor ProjectSnapshots in an async batching work queue and then just use the last one. This change avoids holding onto all of that extra memory by just capturing the Roslyn ProjectId and Razor ProjectKey.

This change includes a fairly substantial update to tests, but they're a bit more comprehensible now, IMHO.
We had taken the time to ensure that the vs-threading analyzers could look for our custom JTF assert helpers, but it stopped working when they were moved to a different namespace. This fixes that problem, which allowed the vs-threading analyzers to find a bit of suspicious code. (The suspicious code is fully intentional and there's a comment explaining it, but it's great to see the analyzer point this out!)
This mechanical change organizes all code related to tag helper discovery in the same place.
This change attempts to tame ProjectStateChangeDetector a bit.

- Remove duplicated code
- Add cache for assembly path to ProjectKey to avoid extra allocations
- Avoid LINQ code
- Make it clearer when a project update is enqueued vs. a project remove.
- Fix a few race conditions in tests (causing failures now that it's slightly faster)
@DustinCampbell DustinCampbell requested a review from a team as a code owner February 6, 2025 01:13
@DustinCampbell
Copy link
Member Author

Looks like some good wins in this change.

image

@DustinCampbell
Copy link
Member Author

The only failing integration test is the same Code Folding test that has failed since FUSE was enabled by default. I'll call that a pass for this PR. 😄

@DustinCampbell DustinCampbell merged commit 01b8677 into dotnet:main Feb 7, 2025
15 of 17 checks passed
@DustinCampbell DustinCampbell deleted the dont-hold-project-snapshots branch February 7, 2025 17:22
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Feb 7, 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.

2 participants