dotnet / roslyn-analyzers

MIT License
1.56k stars 462 forks source link

CA1508 performance decrease in .NET 8 vs .NET 7 #7125

Open edwiles opened 6 months ago

edwiles commented 6 months ago

Analyzer

Diagnostic ID: CA1508: Avoid dead conditional code

Analyzer source

SDK: Built-in CA analyzers in .NET 8 SDK

Version: SDK 8.0.100

Describe the bug

CA1508 is now significantly slower to analyze under certain conditions (see below).

Steps to reproduce

  1. Create a file Test.cs as follows:
namespace CA1508Issue
{
    public class Test
    {
        private void TestMethod()
        {
            var inputs = new Input[]
            {
                new Input { Prop1 = "abc", Prop2 = "def", Prop3 = "ghi" },
                new Input { Prop1 = "abc", Prop2 = "def", Prop3 = "ghi" },
                new Input { Prop1 = "abc", Prop2 = "def", Prop3 = "ghi" },
                // NB cut and paste this line 1000 times, so that the inputs array contains 1000 elements
            };

            foreach (var input in inputs)
            {
                // The follow line may contain any conditional (I believe)
                var test = (1 == 2) ? 3 : 4;
            }
        }

        private class Input
        {
            public string Prop1 { get; set; }
            public string Prop2 { get; set; }
            public string Prop3 { get; set; }
        }
    }
}
  1. Create a file CA1508Issue.csproj as follows:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AnalysisMode>All</AnalysisMode>
    <Platforms>x64</Platforms>
  </PropertyGroup>

</Project>
  1. Create a file global.json as follows:
{
  "sdk": {
    "version": "7.0.404"
  }
}
  1. Run dotnet build. The build executes successfully in a reasonable amount of time (for me, 13 seconds). Various warnings are correctly returned.

  2. In global.json, change the SDK version to 8.0.100.

  3. Run dotnet build. The build has the same output, but executes much more slowly (for me, 1 minute 42 seconds). The same warnings are returned. This is also the case if the .csproj file targets net8.0.

  4. Create a file .editorconfig as follows:

[*.cs]
dotnet_diagnostic.CA1508.severity = none
  1. Run dotnet build. The build executes quickly again (for me, 3 seconds).
N-Olbert commented 4 months ago

Noticed the same Issue, see Buildlog:

image