roslyn
roslyn copied to clipboard
Bad performance in CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer with System.Text.Json source generator
We were trying to make use of System.Text.Json source generation with a class that is pretty deeply nested with a lot of classes and properties.
We noticed that this causes a huge slowdown in build performance. While a build previously took about 5s, the source generation caused it go up to about 40s.
It seems like the generated code by System.Text.Json causes some kind of performance issue in CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer
. I created a repro below with 1000 classes, resulting in 1000 source generated files by System.Text.Json.
Is there a way to disable this particular analyzer? Disabling IDE0005 in .editorconfig seems to have no effect. The only way we found to avoid this, is by disabling EnforceCodeStyleInBuild
completely.
I'm also wondering why the analyzer even analyses source generated code. Is this expected behaviour? Or is this actually not what is happening?
Version Used:
.NET 9 Preview 7. Also reproducible with .NET 8 .
Steps to Reproduce:
- git clone https://github.com/mus65/roslyn-stj-sourcegen-repro
- run
dotnet build -v diag | grep -B 4 -A 2 CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer
. This outputs the following on my local machine:
NOTE: Elapsed time may be less than analyzer execution time because analyzers can run concurrently. (TaskId:112)
Total analyzer execution time: 70.182 seconds. (TaskId:112)
Time (s) % Analyzer (TaskId:112)
69.104 98 Microsoft.CodeAnalysis.CSharp.CodeStyle, Version=4.12.10.37901, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (TaskId:112)
68.389 97 Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryImports.CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer (EnableGenerateDocumentationFile, IDE0005, IDE0005_gen, RemoveUnnecessaryImportsFixable) (TaskId:112)
0.350 <1 Microsoft.CodeAnalysis.CSharp.UseExpressionBody.UseExpressionBodyDiagnosticAnalyzer (IDE0021, IDE0022, IDE0023, IDE0024, IDE0025, IDE0026, IDE0027, IDE0061) (TaskId:112)
0.301 <1 Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer (IDE0058, IDE0059, IDE0060) (TaskId:112)
Diagnostic Id:
IDE0005: Remove unnecessary using directives
Expected Behavior:
Minimal to no impact on build performance.
Actual Behavior:
A lot of CPU time wasted in CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer
: