Share syntax trees across language server workspaces - #84758
Conversation
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 5933d2c9-9c2d-4165-9afe-65237e606db3
Simplify the cache to a single get-or-create operation, track all live red roots weakly, periodically prune dead entries, and key sharing by content plus parse options. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1f56c314-c0d3-44d9-8630-c978f659a9ae
ebc9f07 to
3a92f93
Compare
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1f56c314-c0d3-44d9-8630-c978f659a9ae
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR introduces an optional host-level syntax tree cache used by Language Server host workspaces to share immutable green syntax across workspaces while keeping only weak references to workspace-owned red roots. C# and Visual Basic ISyntaxTreeFactoryService implementations are updated to route parsing through the cache when available, and new unit/integration tests validate cross-workspace/server sharing behavior.
Changes:
- Added
ISyntaxTreeCacheServiceand integrated it intoAbstractSyntaxTreeFactoryServiceso parsing can be cached when the service is present. - Implemented a Language Server host workspace
SyntaxTreeCacheServicekeyed bySourceText.GetContentHash()+ParseOptions, with per-key serialization and periodic pruning of dead entries. - Updated C# and VB syntax tree factory services to be created via
ExportLanguageServiceFactory, enabling optional cache injection, and added tests for factory creation and cache sharing.
Show a summary per file
| File | Description |
|---|---|
| src/Workspaces/VisualBasicTest/VisualBasicSyntaxTreeFactoryServiceTests.vb | Adds a basic regression test ensuring the VB syntax tree factory can be created and parses a tree. |
| src/Workspaces/VisualBasic/Portable/Workspace/LanguageServices/VisualBasicSyntaxTreeFactoryService.vb | Switches VB syntax tree factory to a language service factory and routes parsing through AbstractSyntaxTreeFactoryService’s cache-aware flow. |
| src/Workspaces/CSharpTest/CSharpSyntaxTreeFactoryServiceTests.cs | Adds a basic regression test ensuring the C# syntax tree factory can be created and parses a tree (no cache scenario). |
| src/Workspaces/CSharp/Portable/Workspace/LanguageServices/CSharpSyntaxTreeFactoryService.cs | Switches C# syntax tree factory to a language service factory and routes parsing through AbstractSyntaxTreeFactoryService’s cache-aware flow. |
| src/Workspaces/Core/Portable/Workspace/Host/SyntaxTreeFactory/ISyntaxTreeCacheService.cs | Introduces the internal workspace service contract for syntax tree caching. |
| src/Workspaces/Core/Portable/Workspace/Host/SyntaxTreeFactory/AbstractSyntaxTreeFactoryService.cs | Adds optional cache integration and introduces ParseSyntaxTreeCore for derived services to implement actual parsing. |
| src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/SyntaxTreeCacheService.cs | Implements the host-level cache with weak root tracking, per-key locking, and periodic cleanup. |
| src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer.UnitTests/HostWorkspace/SyntaxTreeCacheServiceTests.cs | Adds unit tests validating green-node sharing, parse-options separation, concurrency behavior, cleanup, and cross-language non-sharing. |
| src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer.UnitTests/Daemon/LanguageServerDaemonTests.cs | Adds a daemon-level integration test ensuring green syntax is shared across servers/clients. |
Copilot's findings
- Files reviewed: 9/9 changed files
- Comments generated: 1
| /// cache retaining any syntax tree. | ||
| /// </para> | ||
| /// </remarks> | ||
| [ExportWorkspaceService(typeof(ISyntaxTreeCacheService), ServiceLayer.Host), Shared] |
There was a problem hiding this comment.
@CyrusNajmabadi might also have some thoughts on this, but this is a very different approach taken than what we do for linked syntax trees reusing trees across multiple targets. Should we be moving that mechanism to also use a cache like this, or should we leverage that other mechanism instead?
There was a problem hiding this comment.
@CyrusNajmabadi Do you recall why we didn't do this approach?
There was a problem hiding this comment.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Note
This error may be related to your runner configuration. You can now configure runners for Copilot code review separately from Copilot cloud agent by creating a copilot-code-review.yml file with your setup steps. Read the docs for details.
jasonmalinowski
left a comment
There was a problem hiding this comment.
Lots of small comments, but otherwise OK with this. I think there's still deeper concerns about whether or not the hashing means we're spending time getting to trees later than we'd like, or whether this should in some way be unified with our logic for sharing linked files. But I don't have concerns about the correctness of the code and since the cache is well-isolated it means we can easily measure stuff and adjust accordingly.
| // Although the return type is non-nullable, IWorkspaceServiceFactory explicitly permits null when the service | ||
| // is not applicable to the workspace. |
There was a problem hiding this comment.
Oops we should fix that.
Backs the `features/sonic` decl/impl split out of `main` to unblock insertion. Reverts the merge (#84778) and the commits that stacked on top of it and cannot stand without it. ## What reverts - **#84778** `Merge/sonic into main` -- the whole feature body (reverted with `-m 1`, keeping pre-sonic `main`). - **#84818** fallback child-content discovery fix -- patches sonic's fast/slow discovery, which no longer exists. - **#84813** restore `GetCSharpDocument()`/`GeneratedCode` -- these were re-added to shim sonic's API removal; reverting the merge restores the originals, so the shim would double-define them. - **#84795** always pass C# formatting options -- edits `CSharpFormattingOptionsHelper.cs`, a file that only exists because of sonic. ## What is kept The other post-merge commits are independent of sonic and stay: arcade bumps, SDL param removal, CS8802, #84758 (syntax-tree sharing), and the two Razor features that merely shared files -- **#84760** (unbound-attribute warnings) and **#84764** (empty-outer-tag warning). Preserving #84760 needed one manual resolution in `UnresolvedAttributeIntermediateNode.cs`: sonic added the `CloneNode()`/`Clone()` overrides and #84760 later added one property assignment inside `CloneNode`. The overrides are sonic infrastructure and go with the revert; #84760's `IsDirectiveAttributeCandidate` property and all its real usages are untouched. ## Validation - `Razor.slnf` builds clean (compiler + all tooling projects, including the ones #84795 touched). - `Microsoft.NET.Sdk.Razor.SourceGenerators.UnitTests`: 215 passed. - `Microsoft.AspNetCore.Razor.Language.UnitTests` (component / tag helper / bind / unbound-attribute): 1593 passed -- confirms #84760's kept feature still works. ## Note Reverting the merge means re-landing `features/sonic` later will need this revert itself reverted first -- expected for a back-out. ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/roslyn/pull/84831)
Summary
In daemon mode if multiple solutions from the same worktree are loaded, they are very likely to have exactly the same trees for the majority of files. We can cache these and share them to reduce incremental memory usage
From benchmarks, this showed roughly a ~500MB reduction in incremental memory usage when loading two versions of Roslyn in daemon mode and making 3 diagnostics requests for 3 different projects.
Microsoft Reviewers: Open in CodeFlow