nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Multithreading and performance optimization

Open oxalica opened this issue 1 year ago • 2 comments

It seems that we are only using a single thread when formatting tons of files. But this can be trivially parallelized. This should also ease the adoption of format-checking in git commit hook as mentioned in https://github.com/NixOS/nixpkgs/pull/322537#issuecomment-2196405557

Eg: fd -e nix --exec-batch nixfmt (--exec-batch passes as much files to nixfmt as possible in a single exec) It takes takes 1:49.27 (~110s) on my machine with only a single thread (~99% CPU) being occupied.

oxalica avatar Jun 28 '24 17:06 oxalica

We previously decided to deprecate the directory mode of nixfmt and instead encourage treefmt, for which I recently implemented parallel formatting.

So I don't think there's anything to do here, let's keep the complexity of this codebase at a minimum :)

infinisil avatar Jun 28 '24 18:06 infinisil

I think that there is quite a bit of code performance which Nixfmt currently leaves on the table. Someone with knowledge of profiling Haskell applications would likely find quite a few low-hanging fruits.

Off the top of my head I can already think of https://github.com/NixOS/nixfmt/blob/master/src/Nixfmt/Predoc.hs#L178-L192 which has overall quadratic complexity, but by delaying computations into a single pass at the end this could be reduced to linear. (I didn't do it when I wrote the code because it requires a minor data types refactoring and I wanted to get stuff to work first.) Of course I can't tell how much this actually affects overall performance, but there are probably quite a few like this which add up.

piegamesde avatar Jun 29 '24 08:06 piegamesde

You can also run it with GNU parallel: parallel "nixfmt {}" ::: *.nix

toastal avatar Nov 29 '24 10:11 toastal

I think we can close this actually, because we now deprecated recursive mode, and are recommending treefmt instead, which does run in parallel.

infinisil avatar Nov 29 '24 10:11 infinisil

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-12-10-and-2025-01-07/58466/1

nixos-discourse avatar Jan 07 '25 21:01 nixos-discourse