dotnet / dotnet/roslyn

[LSP] Microsoft.CodeAnalysis.LanguageServer shows high CPU usage when performing `switch` on a `union` in C# 15.

Open
#83,687 1 comment 0 reactions 0 assignees View on GitHub
Feature - Unions
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:

- .NET 11 Preview 4 (11.0.100-preview.4.26230.115)
- VSCode
- C# 2.136.19
- C# Dev Kit 3.14.196

**Steps to Reproduce:**

Creating C# 15 `union` with several dozen elements and performing pattern matching with a switch statement will make the language server extremely slow. (The CPU usage of the Microsoft.CodeAnalysis.LanguageServer process becomes extremely high.)

```cs
Alphabet a = new A();
switch (a)
{
case A:
case B:
...
}

union Alphabet(A, B, C, ..., Z);
```

This issue does not occur if you replace it with a switch statement using `object? Value`.

```cs
Alphabet a = new A();
switch (a.Value)
{
case A:
case B:
...
}

union Alphabet(A, B, C, ..., Z);
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue in VSCode using .NET 11 Preview 4, C# 2.136.19, and C# Dev Kit 3.14.196 with a several-dozen-element C# 15 union. Compare pattern matching on the union with matching on object? Value; done means the Microsoft.CodeAnalysis.LanguageServer no longer shows extremely high CPU or becomes very slow.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.