Skip to content

Share syntax trees across language server workspaces - #84758

Open
dibarbet wants to merge 3 commits into
dotnet:mainfrom
dibarbet:dibarbet-investigate-syntax-tree-sharing
Open

Share syntax trees across language server workspaces#84758
dibarbet wants to merge 3 commits into
dotnet:mainfrom
dibarbet:dibarbet-investigate-syntax-tree-sharing

Conversation

@dibarbet

@dibarbet dibarbet commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

  • add a host-level syntax tree cache for language server workspaces
  • share immutable green syntax across servers while retaining only weak references to workspace-owned red roots
  • key entries by source content and parse options, serialize creation per key, and periodically prune dead entries
  • route C# and Visual Basic syntax-tree factories through the optional cache

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

@azure-pipelines

Copy link
Copy Markdown
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
dibarbet force-pushed the dibarbet-investigate-syntax-tree-sharing branch from ebc9f07 to 3a92f93 Compare August 5, 2026 18:17
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 1f56c314-c0d3-44d9-8630-c978f659a9ae
@dibarbet
dibarbet marked this pull request as ready for review August 5, 2026 23:26
@dibarbet
dibarbet requested a review from a team as a code owner August 5, 2026 23:26
Copilot AI review requested due to automatic review settings August 5, 2026 23:26
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 ISyntaxTreeCacheService and integrated it into AbstractSyntaxTreeFactoryService so parsing can be cached when the service is present.
  • Implemented a Language Server host workspace SyntaxTreeCacheService keyed by SourceText.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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants