Enabling the parallel frontend on nightly

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Enabling the parallel frontend on nightly

2026年11月16日 · Jakub Beránek 代表编译器团队。简而言之:我们正在 nightly 版本中默认启用 Rust 编译器的并行前端,以便在正式稳定发布前发现剩余的问题。

Nov. 16, 2026 · Jakub Beránek on behalf of the Compiler Team TL;DR: We are enabling the parallel frontend of the Rust compiler on nightly by default, to find remaining issues in preparation for its stabilization.

背景:长期以来,Rust 编译器一直使用多线程来处理其“后端”部分(使用 LLVM)的代码编译。然而,其“前端”部分(包括类型检查和借用检查)一直是在单线程上顺序执行的,这显然限制了编译器的性能。如果您对 Rust 编译器中的并行性有更深入的了解兴趣,请参阅这篇之前的文章。

Context The Rust compiler has been using multiple threads to compile code in its “backend” part (using LLVM) for a long time. However, its “frontend” part (which includes type checking and borrow checking) has been executing sequentially, on a single thread, which of course limits the performance of the compiler. If you are interested in a more in-depth explanation of parallelism in the Rust compiler, see this previous post.

编译器前端实际上很早就支持并行运行,但由于各种问题,它并未被默认启用。三年前,我们使其能够在 nightly 通道上选择性开启并行前端,并计划在 2024 年将其稳定。显然,我们没有实现这一目标,但在此期间,并行前端的工作仍在缓慢推进。

The compiler frontend actually had support for running in parallel for a long time, but it was not enabled by default due to various issues. Three years ago, we made it possible to opt into the parallel frontend on the nightly channel, with the plan to stabilize it in 2024. Clearly, we did not achieve that goal, but work on the parallel frontend was still slowly progressing in the meantime.

去年,Vadim Petrochenkov 与其他几位贡献者一起,开始了一项协调工作,旨在最终完成并行前端的开发。从那时起,许多长期存在的问题得到了修复,性能也得到了提升。现在我们已经到了希望将其广泛应用的阶段。但在那之前,我们想了解人们在实际使用中是否会遇到问题,这就是为什么我们仅在 nightly 通道上默认启用它。一旦我们确信它运行良好,我们就会将其稳定下来。并行前端自 nightly-2026-11-14 起已在 nightly 通道上默认启用。

Last year, Vadim Petrochenkov, together with several other contributors, started a coordinated effort to finally get the parallel frontend over the finish line. Since then, many long-standing issues were fixed, and its performance was improved. Now we are at a point where we would like to make it widely available. But before that, we want to find out if people encounter issues with it in the wild, which is why we are enabling it by default only on the nightly channel. Once we will be confident enough that it is working well, we want to stabilize it. The parallel frontend has been enabled on the nightly channel by default since nightly-2026-11-14.

收益:启用并行前端的主要好处当然是加快 Rust 程序的编译速度。我们在基准测试套件中对并行前端进行了测试,该套件测量了编译 crate 图中叶子 crate 所需的时间。和往常一样,结果各不相同。在某些基准测试中,并行前端几乎没有帮助。而在其他测试中,例如在对 nalgebra、cargo 或 diesel crate 执行 cargo check 时,使用 8 个线程的编译速度几乎快了三倍。

Benefits The primary benefit of enabling the parallel frontend is, of course, faster compilation of Rust programs. We benchmarked the parallel frontend in our benchmark suite, which measures the duration to compile the leaf crate in a crate graph. As usual, the results were varied. In some benchmarks, the parallel frontend barely helped. In others, for example when performing a cargo check of the nalgebra, cargo or diesel crates, the compilation was almost three times faster, using 8 threads.

在下表中,您可以找到我们的套件中针对现实世界基准测试的汇总挂钟时间平均变化,这是在多种配置下从头构建单个 crate 时,与使用顺序前端的相同构建相比的结果。

In the table below, you can find the aggregated wall-time mean change for the real-world benchmarks in our suite, when building a single crate from scratch across several configurations, compared to the same build using a sequential frontend.

Frontend threads / Profilecheckclippydebugopt
2-21.95%-20.74%-14.2%-12.51%
4-37.99%-36.01%-29.14%-23.67%
8-44.23%-41.76%-36.1%-27.88%

很明显,并行化并不完美,我们绝对计划投入更多精力来提高其性能。尽管如此,使用 8 个线程,它可以使我们测试的几个 crate 的 cargo check 端到端编译时间快约 2 倍,这仍然是一个相当大的改进。

It is clear that the parallelization is not perfect, and we definitely plan to invest more effort into improving its performance. Nevertheless, with 8 threads, it can make the end-to-end compilation time of cargo check ~2x faster for several crates that we tested, which is still quite an improvement.

增量重建的性能收益要温和得多,而且也很难比较,因为重建的性能取决于更改的内容以及代码生成单元的布局方式。在我们的基准测试套件中,我们看到增量重建的平均改进为 3-5%。但我们目前还没有针对此用例的优秀基准测试,因此您的结果可能会有所不同。

The performance benefits for incremental rebuilds are much more modest, and also very hard to compare, because the performance of rebuilds depends on what was changed and how were the codegen units laid out. In our benchmark suite, we saw a mean improvement of 3-5% for incremental rebuilds. But we currently do not have great benchmarks for this use-case, so your results might vary.

我们还可以放大到单个 crate。以下是编译 nalgebra 0.33 crate 时,在多种构建配置下,针对完整非增量构建 (full)、完整增量构建 (incr-full) 和增量重建 (incr-patched) 的挂钟时间缩放图表:

We can also zoom in on an individual crate. Here is a chart of how the wall-time scales when compiling the nalgebra 0.33 crate, for a full non-incremental build (full), full incremental build (incr-full) and an incremental rebuild (incr-patched) across several build configurations:

有趣的是,启用并行前端实际上也可以使后端更快,原因在此处有解释。需要注意的是,上述所有基准测试结果仅测量了单个 crate 的编译。在编译整个 crate 图时,您的 CPU 核心可能已经因为后端 (LLVM) 并行性或 Cargo 并行编译多个 crate 而饱和。因此,最终的性能效果将根据您运行的工作负载以及您可用的核心数量而有很大差异。

What is interesting to note is that enabling the parallel frontend can actually also make the backend faster, for reasons explained here. It is important to note that all benchmark results presented above measured only the compilation of a single crate. When compiling a whole crate graph, your CPU cores might already be saturated because of backend (LLVM) parallelism or by Cargo compiling multiple crates in parallel. So the final performance effect will vary heavily based on the workload that you are running, and on the number of cores that you have available.

我们还在一台拥有 8 个 CPU 核心的机器上对 cargo crate 本身进行了 cargo check 端到端(检查整个 crate 图)的基准测试。使用 8 个线程,它带来了约 15% 的端到端改进:1 个前端线程为 36.7 秒,2 个前端线程为 35.1 秒,4 个前端线程为 31.9 秒,8 个前端线程为 30.8 秒。从上面的结果可以看出,当 CPU 核心已经因并行 crate 编译和 LLVM 而饱和时,并行前端的积极效果会降低。

We also benchmarked cargo check end-to-end (checking the whole crate graph) on the cargo crate itself on a machine with 8 CPU cores. With 8 threads, it resulted in a ~15% end-to-end improvement: 36.7s with 1 frontend thread 35.1s with 2 frontend threads 31.9s with 4 frontend threads 30.8s with 8 frontend threads As can be seen from the results above, when the CPU cores are already saturated with parallel crate compilation and LLVM, the positive effect of the parallel frontend is reduced.

调整线程数:起初,nightly 工具链将默认使用仅 2 个线程的并行前端。这是一个保守的选择,它应该仍然允许我们发现潜在的问题,同时不会大幅增加内存使用量。如果您想测试不同线程数的性能,可以使用 —jobs-frontend 编译器标志覆盖前端使用的线程数。它可以在 RUSTFLAGS 环境变量中指定,或者您可以将其放入 .cargo/config.toml 文件中:

Tuning the thread count At the start, the nightly toolchain will default to the parallel frontend using only 2 threads. This is a conservative choice that should still allow us to find potential issues while not increasing memory usage a lot at the same time. If you want to test out the performance with different thread counts, you can override the number of threads used for the frontend using the —jobs-frontend compiler flag. It can be specified either in the RUSTFLAGS environment variable, or you can put it into a .cargo/config.toml file:

[build]
rustflags = ["-Zunstable-options", "--jobs-frontend=8"]

目前,并行前端的性能在超过约 8 个线程时可能不会很好地扩展,但您可以在您的工作负载上尝试它以测试会发生什么。请注意,使用更高的线程数可能会导致编译器更高的内存使用量,在极端情况下,它可能会导致您内存不足。

Currently, the performance of the parallel frontend will likely not scale very well above ~8 threads, but you can try it on your workload to test what happens. Note that using higher thread counts will likely lead to higher memory usage of the compiler, and in extreme cases it might cause you to run out of memory.

限制和已知问题:由于使用了并行性,在使用并行前端时,产生的诊断顺序可能不是确定性的。在某些情况下,生成的二进制文件也可能不可重现,尽管我们目前认为这些情况是错误,我们希望识别并修复它们。目前有一些与使用并行前端时 Rust 编译器的可重现性相关的已知问题,这些问题正在被…

Limitations and known issues Due to the used parallelism, the produced order of diagnostics might not be deterministic when using the parallel frontend. In some situations, the generated binary might also not be reproducible, though we currently consider these cases bugs, and we would like to identify them and fix them. There are currently some known issues related to the reproducibility of the Rust compiler when using the parallel frontend, which are being wo