I’m using crate2nix to build a Rust project with a large amount of Crate dependencies. This of course takes a long time.
I would like to set the value of codegenUnits that is provided to buildRustCrate to the rustc/Cargo default of 16 for every Crate instead of the default 1. I could set this for each individual crate, but this would be unmaintainable. Is it possible to override the default for buildRustCrate as called by crate2nix?
I opened a PR last year to fix this. It got a negative review which I can only summarize as “I don’t think this will speed up builds please do some benchmarking to prove me wrong” and arguing about it was not really a high priority for me. It’s pretty obvious to anybody who uses buildRustCrate regularly that this makes builds way faster.
So I just carry this patch in my local tree instead. It still works great. Even better with -Zthreads=$NIX_BUILD_CORES
Setting this to 1 may improve the performance of generated code, but may be slower to compile.
It was in fact benchmarked by other contributors who found little improvement in build times, it’s a bit unfair to imply the reviewer just came up with the thought out of thin air.
I’m willing to raise a PR to increase codegen-units to 16 if I can actually find evidence of it improving compile times while not making a significant difference to runtime performance and output size.
Ideally we could override this in crate2nix since doing it for an individual invocation of buildRustCrate is easy enough, not so easy for hundreds of invocations.
I’ll have to look more into why, but increasing Nix’s cores setting and increasing the cores allocated to the VM reduced the build times for COSMIC Desktop by a few hours. That was however using buildRustPackage and not buildRustCrate, however this does at least imply a large benefit to increase codegen units (assuming that’s what the cores setting affects with buildRustPackage).
I finally got around to attempting a benchmark using the fhir-sdk crate (one that I’ve been working with which consistently takes up a majority of the build time).
Unless I’ve somehow set codegen-units wrong (all I’m doing is setting the RUSTFLAGS environment variable to -Ccodegen-units=a number), increasing codegen-units beyond 1 actually makes the build slower.
All testing was performed on an AWS Graviton4 CPU with 16 cores allocated with Rust 1.81.0. Results are as follows:
1 codegen unit: 34m 3s
10 codegen units: 39m 6s
16 codegen units: 40m 3s
Even setting -Zthreads=16 on a Nightly compiler had a negative effect.