Optimizing C# code analysis for quicker .NET compilation
Tags: programming, dotnet, csharp, tutorial
Published: 2023-11-14 12:30:00

As a .NET solution grows, the time spent on Roslyn analyzers during compilation increases. I have witnessed a web solution where the execution time of the Roslyn analyzers was simply absurd. In particular, **the ratio was 70% of the time spent on the Roslyn analyzers** and 30% on the rest of the compilation. The total build time was about 2 to 3 minutes depending on the machine's specifications.

Such a compilation duration has a direct impact on three points:

- The productivity of developers, whose time spent waiting each day can reach significant values.
- Metrics related to delivery and continuous deployment. A slower compilation means longer validation times for pull requests and release publications.
- The satisfaction and motivation of developers, who are frustrated by waiting with each code change.

The compilation duration of a .NET solution can be justified by several aspects, including the amount of code, dependencies between projects, coupling between projects, etc. In this first article, we will focus only on the impact of Roslyn analyzers on the compilation time. After reading this article, you will be able to identify their impact on your .NET solutions and take action to reduce it, without neglecting the quality and assurance they provide.

## The usual suspects

Before going any further, let's look at some well-known Roslyn analyzers that may already be enabled in your solutions.

.NET includes [Roslyn analyzers for code style and quality](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview). These are the rules with codes starting with `CAxxxx`, `CSxxxx`, and `IDExxxx`. Depending on the target framework, a subset of these analyzers is enabled by default. The [AnalysisLevel](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview#enable-additional-rules) property can be changed to include more or fewer rules.

Several well-known NuGet packages such as [xUnit.net](https://xunit.net/), [FluentAssertions](https://github.com/fluentassertions/fluentassertions.analyzers), [StyleCop](https://github.com/DotNetAnalyzers/StyleCopAnalyzers), [Entity Framework Core](https://learn.microsoft.com/en-us/ef/core/), and others include by default a significant number of Roslyn analyzers. They help you adhere to the conventions and best practices of these libraries.

The problem is that you may not need all the analysis rules included in these packages. Recently, I became aware of a recent .NET solution that had a transitive reference to Entity Framework Core, while the latter was not used. After analysis, I discovered that the Entity Framework Core analysis rules added a second to the total compilation time. Let's find out how I proceeded to identify this problem.

## Analyzing the impact of Roslyn analyzers on compilation time

To begin, you'll need to install an external tool created by [Kirill Osenkov](https://github.com/KirillOsenkov). Kirill is an absolute expert in MSBuild and a Principal Software Engineer at Microsoft. He developed a tool called [MSBuild Structured Log Viewer](https://msbuildlog.com/), which allows you to read compilation logs generated by MSBuild. Follow the link and install it.

Secondly, you will need to [enable analyzer reporting during the compilation](https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Report%20Analyzer%20Format.md) of your projects. This can be accomplished by adding this line to your project. Ideally, you should add it to all your projects in your solution, and the most efficient way to do this is to use a [Directory.Build.props](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory) file at the root of your solution.

```xml
<Project>
  <PropertyGroup>
    <ReportAnalyzer>true</ReportAnalyzer>
  </PropertyGroup>
</Project>
```

Thirdly, you need to compile your solution and [generate a `*.binlog` file](https://learn.microsoft.com/en-us/visualstudio/msbuild/obtaining-build-logs-with-msbuild?view=vs-2022#save-a-binary-log). You can do this from the command line with the following command:

```bash
dotnet build /bl
```

`/bl` is shorthand for `/binarylogger`. Once the compilation is complete, you should have a `*.binlog` file in the root folder of your solution or project. Double-click this file to open it with MSBuild Structured Log Viewer.

![MSBuild Structured Log Viewer project analysis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i71yf6l2vdf4d1oex00.png)

[Rider](https://www.jetbrains.com/rider/) users are in luck because they don't need to run any CLI commands. The integration of MSBuild Structured Log Viewer is included. Right-click on a project or solution, choose "Advanced Build Actions," and click on "Rebuild Solution with Diagnostics" for a whole solution, or "Rebuild Selected Projects with Diagnostics" for one or more individual projects. The log viewer will automatically open after the compilation.

![MSBuild Structured Log Viewer integration in Rider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00g5o2h6jukhxww5k0fv.png)

## Identifying the Roslyn analyzers with the most impact

When you open a binary log file created with the `ReportAnalyzer` property set to `true`, MSBuild Structured Log Viewer displays an *Analyzer Summary* section at the bottom of the main tree view. This section can be expanded, and you will then have a detailed analysis of each Roslyn analyzer, from the slowest to the fastest. Each Roslyn analyzer can also be expanded to display the individual rules that were executed, with their execution time, and also in the order from slowest to fastest.

In this example project (which is real), we can see that some Roslyn analyzers have a significant impact on compilation time. In particular:

- StyleCop has a huge impact, **with more than a minute and a half**.
- FakeItEasy has a considerable impact of over 30 seconds.
- [AsyncFixer](https://github.com/semihokur/AsyncFixer) adds almost 30 seconds.

> It is important to clarify that code analysis is generally executed in parallel, which means that the total execution time of Roslyn analyzers is not the sum of each rule.

![Code analysis impact on build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh7donohvunlzxfoxxtp.png)

An experienced developer might wonder, for example, if AsyncFixer is really necessary. The recent versions of .NET (6, 7, 8) include more and more Roslyn analyzers aimed at increasing code quality, particularly with the use of *async/await*. However, few people know that certain rules need to be activated with the `AnalysisLevel` property, for example:

```xml
<PropertyGroup>
  <!-- Enables all .NET built-in analysis rules that come from the .NET 6 SDK -->
  <!-- Simply a must-have for projects targeting .NET Standard 2.0 as the default level is very low -->
  <AnalysisLevel>6.0-All</AnalysisLevel>
</PropertyGroup>
```

## Disabling unnecessary Roslyn analyzers

Once you have identified the Roslyn analyzers that have the most impact on compilation time, you can do some research and decide as a team if certain rules are necessary or not. If you decide to disable some rules, the quickest way to do so is to create an `.editorconfig` file at the root location of your solution. In this example, I disable the StyleCop analysis rule `SA0001`, as well as several rules from the AWS SDK which is referenced transitively in my project, but which I do not use.

```ini
[*.{cs,vb}]
dotnet_diagnostic.SA0001.severity = none
dotnet_diagnostic.SecurityTokenService1000.severity = none
dotnet_diagnostic.SecurityTokenService1001.severity = none
dotnet_diagnostic.SecurityTokenService1002.severity = none
dotnet_diagnostic.SecurityTokenService1003.severity = none
dotnet_diagnostic.SecurityTokenService1004.severity = none
```

Refer to [this Microsoft documentation to learn more about how to suppress warnings from Roslyn analyzers](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).

## Compile production builds faster

If your team works with pull requests to merge your features into the main branch, there's a good chance you have quality gates in place that ensure your project compiles successfully, your tests are green, there are [no warnings and errors](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#treatwarningsaserrors), etc. This usually means that the code in the main branch is of high quality and can potentially be deployed to production.

Considering that the code analysis rules have already been run during CI, it may not be necessary to burden the compilation time of the production deliverable with additional code analysis. You can simply disable the execution of Roslyn analyzers by modifying the `dotnet build` or `dotnet publish` command with the `RunAnalyzers` property set to `false`:

```bash
dotnet build -c Release -p:RunAnalyzers=false
```

In my example project, disabling code analysis **reduces the compilation time from ~2 minutes to less than 50 seconds**!

If your team has a very high velocity and you deploy to production several times a day, this can have a significant impact on your delivery and continuous deployment metrics. If you use a cloud provider to compile your Docker images, this can also reduce your costs.

## Conclusion

In this first article of a series concerning the optimization of the compilation time of .NET solutions, we have seen how to identify the Roslyn analyzers that have the most impact on compilation time. We have also seen how to disable code analysis rules that are not necessary, and how to disable the execution of Roslyn analyzers during the compilation of production deliverables.

I hope this article has been helpful to you. Feel free to react in the comments or contact me on Twitter [@asimmon971](https://twitter.com/asimmon971).