Sub topic 5
Incremental compilation and what invalidates it
This chapter assumes you can build and test a Cargo project, read Cargo.toml, and know that rustc splits a crate into codegen units before LLVM turns them into machine code. It builds on the dev profile settings from the previous chapter.
What incremental compilation does
Incremental compilation lets rustc reuse work from an earlier build of the same crate. The compiler records a dependency graph of its internal queries, such as "type-check this function" or "generate code for this item". After you edit a file, it re-runs only the queries whose inputs changed. It reuses the saved results for the rest.
Cargo turns it on by default for the dev profile and for workspace members. It is off by default for release, because incremental builds tend to produce slower code and release favors runtime speed over rebuild time. Dependencies from crates.io are never built incrementally. They change rarely, so the saved state would cost disk space and give little benefit.
The saved state lives under target/debug/incremental/. Each crate gets its own directory.
Notice that linking always runs. Incremental compilation saves front-end and codegen work, but it cannot skip the link step. This is why the linker chapter matters for small edits.
Controlling it
You normally do not need to do anything. These settings exist for diagnosis and for special cases.
# Cargo.toml
[profile.dev]
incremental = true
[profile.release]
# Turn it on for release only if you iterate on optimized builds.
incremental = falseYou can also override it for one command with the environment variable CARGO_INCREMENTAL=0 (off) or CARGO_INCREMENTAL=1 (on).
# Compare a clean build with and without incremental state
CARGO_INCREMENTAL=0 cargo build
CARGO_INCREMENTAL=1 cargo buildWhat invalidates it
The cache is only useful if most queries survive an edit. These things reduce reuse or discard the cache completely.
| Cause | Effect |
|---|---|
| Changing a public signature or a type used widely | Every query that depends on it re-runs, in this crate and in crates that depend on it. |
| Editing a function body | Usually only that function and its codegen unit are redone. Cheap. |
| Changing a macro or a `build.rs` output | Everything that expands the macro or reads the generated file re-runs. |
| Changing `rustc` version, target, or `RUSTFLAGS` | Cargo uses a different fingerprint, so it rebuilds from scratch. |
| Changing profile settings such as `opt-level` or `debug` | The saved state no longer matches, so the crate is rebuilt. |
| Running `cargo clean` or deleting `target/` | All state is lost. |
Two habits cause many accidental full rebuilds:
- Alternating between
cargo buildandcargo clippyorrust-analyzerwith different flags. Each flag set keeps its own artifacts, or overwrites them, depending on the setup. - Setting
RUSTFLAGSin your shell for one command. Cargo sees the change and treats every crate as dirty.
Checking that it works
Run a build, change one function body, and build again. The second build should be much faster than the first.
cargo build
# Edit a function body in src/lib.rs, then:
cargo buildIf a body-only edit still triggers a long build, look at cargo build --timings. Time spent in the final link step points to the linker. Time spent in one large crate points to splitting it, which later chapters cover.
Exercise
You edit the body of one private function in a large crate. The cargo build still takes 40 seconds, and --timings shows 30 of them in the final step after the crate compiles. Is incremental compilation failing? What should you look at?
Show answer
No. Incremental compilation did its job on the compile step, but it cannot skip linking. The 30 seconds are most likely the linker, so switch to a faster linker such as lld or mold and consider reducing debug info.
Exercise
Why does Cargo disable incremental compilation for the release profile by default, and how can you override it for a single command?
Show answer
Incremental builds split work in ways that can produce slower code, and release builds prioritize runtime speed. Set CARGO_INCREMENTAL=1 for that command, or set incremental = true in [profile.release].