Sub topic 7
The cost of proc macros and build scripts
This chapter assumes you can read a Cargo.toml, run cargo build, and have seen #[derive(...)] in Rust code. You should also know the --timings report from the measuring chapter. Procedural macros and build scripts both run code during compilation, so they can add time that has nothing to do with your own source.
Two kinds of code that run at build time
A procedural macro (proc macro) is a Rust function that rustc loads and runs while it compiles your crate. It reads tokens and returns new tokens. #[derive(Serialize)] is one example.
A build script is a build.rs file at the crate root. Cargo compiles it into a program and runs it before compiling the crate. Build scripts often generate code, compile C libraries, or detect system features.
Both cost time in two ways:
- The macro crate or build script must itself be compiled before anything that uses it.
- Running them takes time, and rustc cannot start on the dependent crate until they finish.
Why proc macros are expensive
Most proc macros depend on syn and quote. These crates parse and build Rust syntax, and they take a while to compile. With the default features, syn supports every kind of syntax. Many macros need only a small part of that.
The macro crate is also compiled for the host machine, not the target. Rustc loads it as a dynamic library, so its compile time is added to every clean build.
Two things to watch:
- Many macro crates in the tree. Each one may pull in
synwith different features, which can force extra work. - Heavy expansion. A derive on a large struct can generate a lot of code. That code then goes through type checking and LLVM like any other.
Find the offenders
Use the tools you already have. In the --timings report, look for syn, serde_derive, and any crate with a build-script entry that runs for a long time.
# Which crates depend on syn, and which features are enabled
cargo tree -i syn -e features
# Which crates have build scripts (look for "build.rs" in each manifest)
cargo metadata --format-version 1 | grep -o '"kind":\["custom-build"\]' | wc -lThe second command only counts build scripts. The timings report shows how long each one takes.
Reduce the cost
- Drop derives you do not use. If a crate only needs
serdefor one type, do not enable derive on every type. - Disable default features on dependencies that pull in macro crates, then enable only what you need.
- Prefer plain code for small cases. A short hand-written
implcan replace a derive and removes a macro from the path. - Optimize the macro crates in dev builds. Macros run on every build, so a faster macro can help. Add this to your manifest:
# Cargo.toml
[profile.dev.build-override]
opt-level = 3build-override applies to build scripts, proc macros, and their dependencies. A higher opt-level makes them slower to compile once, then faster to run on each rebuild. This helps most when expansion is heavy. Check with --timings that it pays off for your project.
Write cheap build scripts
If you own a build.rs, tell Cargo when to rerun it. Without this, Cargo reruns the script when any file in the package changes.
// build.rs
fn main() {
// Rerun only when this file changes.
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=schema/api.proto");
}Without any rerun-if-changed line, the script runs after most source edits. A script that compiles C code or runs a code generator then slows every edit-compile cycle.
Exercise
Your --timings report shows serde_derive and syn taking a large share of a clean build, and cargo tree -i syn -e features shows syn built with the full feature. Name two changes that could reduce this cost.
Show answer
Remove or narrow derive usage and disable default features on dependencies that enable syn/full unnecessarily, so fewer or lighter macro crates are built. Setting [profile.dev.build-override] opt-level higher can also speed up macro execution, but verify it with --timings.