headstart

Start dependent crates before their dependencies finish type-checking.

Every crate waits for the crates it depends on to be fully checked, function bodies included, before it starts. It doesn’t need those bodies to type-check itself. 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. Each crate’s bodies are then checked while the crates downstream are already compiling.

  • cargo check: dependents run to completion on early metadata.
  • cargo build: dependents do all their analysis on early metadata, then wait for the dependency’s full metadata before generating code. While they wait, they give their job slot back.

If a body has an error, the build still fails with that error, with the same diagnostics and exit status as today; only progress lines and the cross-crate order of JSON messages can differ.

Pieces

  • rustc, -Zearly-metadata (6 patches):
    • a new analysis_interfaces query splits analysis into item interfaces and function bodies;
    • the driver writes .early-rmeta between the two;
    • crate loading accepts early metadata, and swaps in full metadata before code generation.
  • cargo, -Zheadstart (3 patches):
    • passes -Zearly-metadata to every compile;
    • starts dependents on the early-metadata notification, in both check and build;
    • gives a paused compilation’s job slot to other work.

Benchmarks

On rustc’s default front end, headstart makes clean builds of 13 real projects (rust-analyzer, zed, bevy, lemmy, polars and others) up to 54% faster for cargo check, and up to 42% for cargo build. None is slower. With the parallel front end (-Zthreads=8), which covers some of the same ground, it adds up to 25%.

On 16 cores, a clean cargo build of codex-rs is 37% faster. On 4 cores, rust-analyzer’s check is 24% faster and its build 13–15%, codex-rs’s check 14%.

The patches are a commit series meant to become upstream pull requests.