Share syntax trees across language server workspaces - #84758
Open
dibarbet wants to merge 3 commits into
Open
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
dibarbet
force-pushed
the
dibarbet-investigate-syntax-tree-sharing
branch
from
August 5, 2026 18:17
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. |
Contributor
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
Comment on lines
+19
to
+22
| [Theory] | ||
| [InlineData(LanguageNames.CSharp, "class C { }", "first.cs", "second.cs")] | ||
| public async Task IdenticalDocumentsInDifferentWorkspacesShareGreenNodes( | ||
| string language, string source, string firstPath, string secondPath) |
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
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.
Summary
From benchmarks, this showed roughly a ~500MB reduction in incremental memory usage when loading two versions of Roslyn and making 3 diagnostics requests for 3 different projects.
Microsoft Reviewers: Open in CodeFlow