Rust enables parallel frontend on nightly: faster compilation

In this article
Background: the compiler's frontend and backend
The backend of the Rust compiler (which uses LLVM) has long run on multiple threads. In contrast, the frontend, which includes type checking and borrow checking, still runs sequentially on a single thread, which limits the compiler's overall performance.
The frontend has in fact supported parallel execution for a long time, but it was not enabled by default because of many outstanding issues. Three years ago, the development team made this feature opt-in on the nightly channel, with a plan to stabilize it in 2024. That goal was not met, although work continued slowly in the period afterward.
Last year, Vadim Petrochenkov and several other contributors launched a coordinated effort to bring the parallel frontend across the finish line. Many long-standing bugs were fixed and performance improved. The team wants to know what problems users run into in practice, so it is enabling the feature by default only on nightly. Once they are confident the feature works well, they will stabilize it.
Performance: measured results
The main benefit of the parallel frontend is faster compilation of Rust programs. The benchmark suite measures the compile time of leaf crates in a crate graph. The results are fairly varied: some benchmarks barely benefit, but when running cargo check on the nalgebra, cargo or diesel crates with 8 threads, compile time is nearly three times faster.
The table below shows the average change in wall-time (real elapsed time) across the real-world benchmarks, when building a crate from scratch, compared with the sequential frontend:
2 frontend threads: check down 21.95 %, clippy down 20.74 %, debug down 14.2 %, opt down 12.51 %.
4 frontend threads: check down 37.99 %, clippy down 36.01 %, debug down 29.14 %, opt down 23.67 %.
8 frontend threads: check down 44.23 %, clippy down 41.76 %, debug down 36.1 %, opt down 27.88 %.
The parallelization is not perfect, and the development team plans to invest more to improve it. Even so, with 8 threads, the end-to-end cargo check time of many of the crates tested is about 2 times faster.
For incremental rebuilds, the benefit is much more modest and harder to compare, because performance depends on the specific change and on how the codegen units are laid out. In the benchmark suite, the average improvement is 3-5 %. There is currently no good benchmark for this case, so real-world results may differ. Interestingly, enabling the parallel frontend sometimes makes the backend faster too.
When an entire crate graph is compiled
All the figures above measure the compilation of a single crate only. When an entire crate graph is compiled, the CPU cores may already be busy because of parallelization in the backend (LLVM) or because Cargo is compiling several crates at the same time. The final effect therefore depends heavily on the workload and the number of cores available.
To illustrate, the development team measured end-to-end cargo check on the cargo crate itself, on a machine with 8 CPU cores. With 8 threads, the end-to-end improvement is about 15 %:
36.7s with 1 frontend thread.
35.1s with 2 frontend threads.
31.9s with 4 frontend threads.
30.8s with 8 frontend threads.
When the CPU cores are already occupied by compiling many crates in parallel and by LLVM, the positive effect of the parallel frontend is reduced.
Tuning the number of threads and how to disable the feature
Initially, the nightly toolchain uses the parallel frontend with 2 threads by default. This is a conservative choice: it is still enough to uncover latent bugs without significantly increasing memory usage.
To try a different number of threads, use the compiler's --jobs-frontend flag. You can set this flag in the RUSTFLAGS environment variable or in the .cargo/config.toml file, for example:
[ build ]
rustflags = [ " -Zunstable-options " , " --jobs-frontend=8 " ]At present, the parallel frontend's performance most likely does not scale well beyond about 8 threads, but you can try it on your own workload. Higher thread counts generally make the compiler use more memory and, in extreme cases, can cause it to run out of memory.
This feature is enabled by default only on nightly. The stable toolchain still uses the sequential frontend. To disable it on nightly, pass -Zunstable-options --jobs-frontend=1 to rustc, via RUSTFLAGS or the configuration file:
[ build ]
rustflags = [ " -Zunstable-options " , " --jobs-frontend=1 " ]If you have to disable it, the development team would like to know why on GitHub or Zulip, especially cases where the compiler crashes or the generated binary is not reproducible when the parallel frontend is enabled.
Limitations and known issues
Because of the parallel execution, the order of diagnostics (error messages and warnings) may no longer be deterministic when using the parallel frontend.
In some situations, the generated binary may not be reproducible. The development team treats these cases as bugs and wants to identify and fix them.
There are currently some known issues related to compiler reproducibility when using the parallel frontend, and they are being addressed.
Next steps
Over the next few months, the development team will monitor GitHub and Zulip for bug reports about the parallel frontend. Once they are confident that the feature works well and has no major remaining issues, they want to move toward stabilization.
The team also wants to improve performance. So far most of the effort has gone into making the feature work correctly rather than fine-tuning every percentage point, so there is still room to speed it up.
In addition, many parts of the frontend still run sequentially even with parallel mode enabled, and work to parallelize more of it is underway. Last year a Google Summer of Code project focused on parallel macro expansion and name resolution, and there is also an open pull request aimed at parallelizing the lowering step from the Abstract Syntax Tree (AST) to the High-Level Intermediate Representation (HIR). There is still a lot of work to do in this area.


