Repository navigation
Open
Description
on our repository (https://github.com/xSke/PluralKit), dotnet-format will take between 4 and 8 seconds to run, even when no changes need to be applied.
I saw #757 but I'm not sure if it's related, thus the new issue.
here's a -v diag logging of a particularly bad run:
$ dotnet-format -v diag
The dotnet format version is '5.1.225507+756d5a1c121be3e57e924788af64aa5607dc24e1'.
The dotnet runtime version is '5.0.9'.
The dotnet CLI version is '5.0.400'.
Using MSBuild.exe located in '/home/spiral/.dotnet/sdk/5.0.400/'.
Formatting code files in workspace '/home/spiral/sources/pluralkit/PluralKit.sln'.
Loading workspace.
Project PluralKit.Bot is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
Project PluralKit.Bot is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Bot/obj/Debug/net5.0/PluralKit.Bot.GeneratedMSBuildEditorConfig.editorconfig'.
Project PluralKit.Bot is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
Project PluralKit.Core is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
Project PluralKit.Core is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Core/obj/Debug/net5.0/PluralKit.Core.GeneratedMSBuildEditorConfig.editorconfig'.
Project PluralKit.Core is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
Project PluralKit.API is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
Project PluralKit.API is using configuration from '/home/spiral/sources/pluralkit/PluralKit.API/obj/Debug/net5.0/PluralKit.API.GeneratedMSBuildEditorConfig.editorconfig'.
Project PluralKit.API is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
Project PluralKit.Tests is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
Project PluralKit.Tests is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Tests/obj/Debug/net5.0/PluralKit.Tests.GeneratedMSBuildEditorConfig.editorconfig'.
Project PluralKit.Tests is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
Project Myriad is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
Project Myriad is using configuration from '/home/spiral/sources/pluralkit/Myriad/obj/Debug/net5.0/Myriad.GeneratedMSBuildEditorConfig.editorconfig'.
Project Myriad is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
Complete in 4845ms.
Determining formattable files.
Complete in 1144ms.
Running formatters.
Complete in 1146ms.
Formatted 0 of 309 files.
Format complete in 7138ms.
Reacted by Fabien Ménager, Thomas Howlett, Glen, Felix Schröter, JanH, Yavorski, wardie93, Timothee Guerin, Conan the Dev, Gunnar Jänicke and 34 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
erdalsivri commentedon Jan 4, 2022
I have been investigating
dotnet formatslowness in our large solution. Here is what I've found out so far:dotnet format whitespace --folderis much faster assuming you are using .Net 6 (with the new version ofdotnet format) and are only interested inwhitespaceformatting. If you needstyleandanalyzers, things get much slower due to compilation.dotnet formatunder a specific project instead of the whole solution helps. For example,dotnet format Solution.sln --include SomeProject/SomeFile.csis much slower thandotnet format SomeProject/SomeProject.csproj --include SomeProject/SomeFile.csfor some reason.Of course this workaround is only useful if you are doing a
git diffto format only affected files in a branch. I wrote a simple bash script to group modified files by project and rundotnet formatconcurrently for each affected project with--include <path>option. It helped reduce the total runtime from more than 2 mins to under 1 min in our solution assuming a couple of files are modified under a few different projects. We do needstyleandanalyzersso can't use the fastwhitespacecommand with the--folderoption.I suspect redundant compilation (due to referenced projects getting built/analyzed multiple times) is the root cause for the slowness but I am not sure yet. Needs more debugging
erdalsivri commentedon Jan 5, 2022
CSharpSimplifyTypeNamesDiagnosticAnalyzeris responsible for about 20 seconds of runtime for one of our projects. This is what I got after running analyzers separately with extra logging:Turns out there are known performance issues with this analyzer: dotnet/roslyn#18950
The runtime seems to be proportional to number of files (or number of lines of code) in the project. We have several projects with over 300 files and this analyzer takes at least 5 seconds to run for each of these projects.
erdalsivri commentedon Jan 5, 2022
Another thing I noticed is that
NamingStyleDiagnosticAnalyzeralways runs for all projects even if they are not included with--include. For example,dotnet format --include SomeProject/SomeFile.cswill run theNamingStyleDiagnosticAnalyzerinSomeOtherProjectand just discard the result as no files are specified underSomeOtherProject.Filtering out the analyzer based on
formattablePathsbefore the following line fixes the issue:format/src/Analyzers/AnalyzerFormatter.cs
Line 303 in e473b1d
GetSeverityAsyncdoes have aformattablePathscheck but that's after the line above soNamingStyleDiagnosticAnalyzeris always included.This fixed's reduced the runtime of
dotnet format --include SomeProject/SomeFile.csby 50% in our solution (from more than 2 minutes to 1 minute). I will prepare a PRFilter out project early if it doesn't have any formattable documents
Filter out project early if it doesn't have any formattable documents
Filter out project early if it doesn't have any formattable documents
erdalsivri commentedon Jan 5, 2022
Here is the PR to fix the issue with redundant runs of
NamingStyleDiagnosticAnalyzer: #1485Filter out project early if it doesn't have any formattable documents
sander1095 commentedon May 18, 2022
Hey @erdalsivri , could you perhaps share this script? Thanks!
sander1095 commentedon May 19, 2022
I think it's a good idea to link this to #1479 because both need to be fix to make this tool actually usable in pre commit hooks
JoeRobich commentedon May 19, 2022
Just wanted to share some comments from #1479
Two things go into why Style and Analyzer operations are slower.
First, they require that we have correct semantic information about your code. To do this we rely on MSBuild performing a design-time build where we can know the exact set of code files as well as what references to include with each project. This is equivalent to what Visual Studio or OmniSharp does when opening a project/solution. It is less costly than a full compile, but it can take anywhere from seconds to minutes depending on the size of your solution.
Second, we have to actually run the analyzers and apply code fixes. Running analyzers requires additional in-memory compilations. The results of one analysis become invalid as we apply code fixes from earlier analysis.
We have discussed alternate ways to speed up populating our workspace. Ultimately MSBuild will be involved at some point because .NET builds are so highly customizable the only way not to be wrong is to have MSBuild do the work.
We are not committing to either approach today, but know that this a problem that we are thinking on.
shtratos commentedon Apr 26, 2023
@JoeRobich could you please share what is the current state of this effort?
I just ran
dotnet formaton a unit tests project with 10 files which is a part of a large repository.Loading workspace took ridiculous amount of time:
What's puzzling is that it seems to figure out via MSBuild the whole dependency chain of projects to figure out their editorconfig settings and then simply discards them as they are not in scope.
.editorconfigfiles or just using user-specified.editorconfigwithout loading the whole project in memory?dotnet-formatas a default out-of-the-box formatter - this needs to be prioritized, funded and fixed.Happy to share my binlog if it helps.
shtratos commentedon Apr 26, 2023
Not to mention that
dotnet formatprocess consumes 25GB of RAM while running.niemyjski commentedon Feb 2, 2024
I don't think any teams from Microsoft are using this, because if they were they would find it unacceptable.
johnthagen commentedon Mar 27, 2024
I can confirm for my relatively small project
dotnet format whitespaceruns 2x as fast asdotnet format. Sadly, it still runs in 4 seconds even with onlywhitespace, which is still a noticeable slow down in development flow compared to something like Ruff format from the Python ecosystem which runs in imperceivable time.I had the following set up a File Watcher in Rider:
I was hoping that having it only format the single file I was working on would speed things up, but as mentioned above it seems like a full project build/analysis of some kind is involved.
9 remaining items
johnthagen commentedon Jun 1, 2024
I can't speak for everyone, but there is value in a tool that is installed by default with the
dotnetCLI, and a first party tool is generally more universally used than a third party. That said, I will probably try out CSharpier soon since in my experience opinionated formatters save so much time (dotnet formatis not very strict compared to things like Ruff format or Prettier).rmannibucau commentedon Sep 17, 2024
Same there, it is way too slow and even if I find csharpier insanely great it doesn't support analyzers but there should be something in between (js did it quite well, I don't see why c# can't, maybe using a post build phase instead of plugging into the build directly and just reusing loaded model instead of building twice).
abdulkareemnalband commentedon Jul 24, 2026
What if
dotnet formatsupported caching similar to https://eslint.org/docs/latest/use/command-line-interface#cachingbased hash of files content. then format can skip recomputing issues if file is not changed
rmannibucau commentedon Jul 24, 2026
caching would be great but on CI it would be disabled most of the time I think and still csharpier is fast without cache so maybe use cache as a final optimization rather than a first class citizen in the impl (for >= 2 executions, indeed cache in a single one is fine).
abdulkareemnalband commentedon Jul 24, 2026
You can cache inside CI also
Lot of CIs provide caching artifacts, which will be populated on next run for next run,
rmannibucau commentedon Jul 24, 2026
@abdulkareemnalband not saying you can't, more than 1. it will not be done that often, 2. it is often desired to start on the CI without cache (at least nightly builds for ex to ensure repeatability - even for formatting, cache can hide some issues), so will be not enabled/disabled
since we know it can be fast (csharpier) there is no reason to start by optimization based on cache IMHO
abdulkareemnalband commentedon Jul 24, 2026
@rmannibucau I understand you are worried about absolute perf gain.
But I think caching can be additional benefit for DX.
rmannibucau commentedon Jul 24, 2026
@abdulkareemnalband not again caching, just stating that it must be fast without and using chsarpier as a ref it doesn't need caching so I would put caching as a workaround and failure of the rework rather than a core feature for next iteration
Frulfump commentedon Sep 11, 2026
It's also slow on small repos dotnet/sdk#49910 (Shouldn't all issues be moved to the SDK repo now given this repos README.md?
)
christianomicron commentedon Sep 23, 2026
@rmannibucau I think you might be mixing two things - "cache" is a very broad subject, and in some cases, you just can't get around it (but often it might come up as a different name).
You have a valid point that caching dependencies in CI might not always be desirable, but caching the formatting results is something entirely different. Conceptually, the formatting tool would write out a (cache) file of:
Admittedly, I don't know the full details behind the
dotnet formatprocess - so perhaps a file cache might not help much if the slowness is coming from compilation (which might not be easily evaded). Otherwise, I think such a result cache system should be simple to implement and provide tangible benefits - even as a standalone app/script could provide a benefit by skippingdotnet formatwhen nothing has changed (yes, also in CI).rmannibucau commentedon Sep 23, 2026
@christianomicron not sure if I got it right but my point is you want cache on the CI for PR iterations etc and you want to drop it for master (at least nightly or friends) cause you don't want any state can impact the execution or skip any processing, so a cache is not a real solution there for me, in particular when alternative formatter are insanely fast already so it reall sounds like something to fix and not workaround with a cache to me
christianomicron commentedon Sep 23, 2026
@rmannibucau as I said, it depends on what we mean by cache:
Pretty much any formatter can cheaply do this: hash the file, compare it with the hash from the cache, and skip it if it hasn't changed. Since formatting is deterministic, if the same input was already formatted with the same formatter version, there's no reason to do the work again. Hash collisions are also negligible with a suitable algorithm (and they're extremely fast).
rmannibucau commentedon Sep 23, 2026
@christianomicron nop, if I want to go on your reasoning then I would cache dependencies cause they are immutable but point to have some CI job cache-less is to ensure there is no local side effects and it includes the formatting, there is nothing specific there.
Once again, the impl would be optimized and there would be no way to make it fast I would look for some fingerprint based cache yes, but there it is clearly not the case, it is just that the impl is not done to be fast as some other formatters so cache is not the primary solution for this issue.
Frulfump commentedon Sep 24, 2026
From the README.md of this repo:
And pinned issue #2152
So I think ideally this conversation should be moved there.
Interestingly enough Microsoft has performance as a feature and does blogs like this https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-11/ each year. Ideally we would get Stephen Toub interested in fixing this issue.
I assume issues and PRs will be closed/moved/locked and the repo archived but at least new issue/PR creation turned off after .NET 8 and 9 goes out of support in November.
rmannibucau commentedon Sep 24, 2026
maybe some proposal: dotnet/sdk#56394
Enables a 3.5× speedup, followed by an additional 1.5× speedup with caching (25%): 7s → 2s → 1.3s.