Dev News Daily ENDE

Headstart patches rustc and cargo to start dependent crates early, cutting clean builds by up to 42%

Headstart, a project that reached the Hacker News front page on 4 October, is a set of patches to the Rust compiler and Cargo that changes when a crate is allowed to start compiling.

The idea. Today a crate waits until every crate it depends on has been fully checked, function bodies included. It does not actually need those bodies: it compiles against the dependency's interface, the metadata in its .rmeta file. Headstart makes rustc write an early metadata file as soon as the interface is checked, and makes Cargo start dependents on it while the dependency's bodies are still being checked.

How it behaves. For cargo check, dependents run to completion on the early metadata. For cargo build, they do all their analysis on it and then wait for the full metadata before generating code, giving their job slot back while they wait. If a body has an error, the build still fails with the same diagnostics and exit status as today; only progress lines and the order of JSON messages across crates can differ. The costs, as the author lists them, are downstream work that gets thrown away, errors reported slightly later, and more memory in use at once.

Headstart patches rustc and cargo to start dependent crates early, cutting clean builds by up to 42%
Headstart patches rustc and cargo to start dependent crates early, cutting clean builds by up to 42% — Dev News Daily

The patches. Six patches to rustc behind -Zearly-metadata and three to Cargo behind -Zheadstart, each with a commit message and tests.

The numbers. On rustc's default front end, clean builds of 13 real projects, among them rust-analyzer, Zed, Bevy, Lemmy and Polars, were up to 54% faster for cargo check and up to 42% for cargo build, and none was slower. With the parallel front end (-Zthreads=8), which overlaps with the same idea, the gain is up to 25%. These are 16-core figures: the benefit comes from cores a build would otherwise leave idle, so it shrinks on smaller machines. On 4 cores, rust-analyzer's check was 24% faster and its build 13–15%.

These are unstable flags in a patch series, not a merged feature; whether something like it lands upstream is a question for the Rust compiler and Cargo teams.

Primary source
headstart (GitHub)
https://github.com/PowderworksCode/headstart