Skip to content

Share syntax trees across language server workspaces - #84758

Merged
dibarbet merged 5 commits into
dotnet:mainfrom
dibarbet:dibarbet-investigate-syntax-tree-sharing
Aug 7, 2026
Merged

Share syntax trees across language server workspaces#84758
dibarbet merged 5 commits into
dotnet:mainfrom
dibarbet:dibarbet-investigate-syntax-tree-sharing

Conversation

@dibarbet

@dibarbet dibarbet commented Aug 4, 2026

Copy link
Copy Markdown
Member

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

  • 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 in daemon mode 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

/// cache retaining any syntax tree.
/// </para>
/// </remarks>
[ExportWorkspaceService(typeof(ISyntaxTreeCacheService), ServiceLayer.Host), Shared]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@CyrusNajmabadi Do you recall why we didn't do this approach?

Copilot AI review requested due to automatic review settings August 6, 2026 18:25

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.

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 jasonmalinowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread src/Workspaces/CSharpTest/CSharpSyntaxTreeFactoryServiceTests.cs Outdated
Copilot AI review requested due to automatic review settings August 7, 2026 20:05

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.

Copilot's findings

  • Files reviewed: 8/8 changed files
  • Comments generated: 0 new

Comment on lines +183 to +184
// Although the return type is non-nullable, IWorkspaceServiceFactory explicitly permits null when the service
// is not applicable to the workspace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oops we should fix that.

@dibarbet
dibarbet merged commit 19ea9f5 into dotnet:main Aug 7, 2026
25 checks passed
@dibarbet
dibarbet deleted the dibarbet-investigate-syntax-tree-sharing branch August 7, 2026 21:54
JoeRobich pushed a commit that referenced this pull request Aug 11, 2026
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)
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.

4 participants