The frontend of the Rust compiler, which handles tasks like parsing, type checking, and borrow checking, has implemented support for parallel execution, significantly reducing compilation time. Parallelization is already available in Rust nightly builds and can be enabled using the option '-Z threads=8'. The stable branch plans to include this capability in 2024.
Efforts to reduce compilation time in Rust have been underway for several years. In the first 10 months of 2023, the average compilation time was reduced by 13%, peak memory consumption decreased by 15%, and the size of generated files was cut by 7%. At this stage, acceleration was achieved through optimizations to the compiler itself. After this, developers shifted their focus to speeding up operations through parallelization during compilation.
Until now, parallelization in Rust was primarily achieved at the process level, for instance, the Cargo package manager can run multiple rustc processes to compile several packages simultaneously. Support for parallelization is also available on the backend, which handles code generation tasks—the Rust backend can generate code in parts that LLVM can then process in parallel. However, the frontend could only handle source code in a single-threaded mode until now.
To support parallelization, the frontend has been transitioned to use the Rayon library and significantly revamped; for example, many of its components are now synchronized using mutexes and read/write locks, and atomic types are employed in the code. In performance testing, the new parallelizable implementation was able to compile up to 2% slower when operating in single-threaded mode (-Z threads=1), but when more than one thread was used, the speed increased significantly. For instance, with 8 threads set (-Z threads=8), compilation time was successfully reduced by 50% in certain situations.
The result greatly depends on the environment settings and the code being compiled— for very small programs that compile quickly, compilation in multithreaded mode may actually be slower. Additionally, memory consumption in multithreaded mode can increase significantly, with tests showing memory usage rising by up to 35%.
Source: opennet.ru
