Sub topic 6
Trim dependencies and features
Every crate in your dependency tree gets compiled, and often more than once. This chapter shows how to see what you pull in and how to cut it down. It assumes you can read Cargo.toml, know what a Cargo feature is, and have run cargo build --timings from the earlier chapter on measuring.
Why dependencies dominate
A clean build compiles every dependency from source. Your own crate may be small, yet a single cargo add can bring in dozens of transitive crates (dependencies of your dependencies). Incremental compilation helps your crate, but a changed dependency version or feature set recompiles that crate and everything that depends on it.
Two things drive the cost:
- The number of crates, since each one needs its own rustc and LLVM run.
- The enabled features, since each feature can add code and more dependencies.
Inspect what you have
Start with Cargo's own tools.
# The full tree, with the features enabled on each crate
cargo tree -e features
# Crates that appear in more than one version
cargo tree --duplicates
# Who depends on a given crate, and why it is here
cargo tree -i syncargo tree --duplicates is often the quickest win. If two versions of syn or hashbrown appear, each is compiled separately. Updating one dependency, or running cargo update, can often merge them.
To find crates you declared but never use, install cargo-machete (fast, name-based) or cargo-udeps (slower, needs nightly, more precise):
cargo install cargo-machete
cargo macheteTreat the output as a list of candidates. A crate used only through a macro or a build script can be reported wrongly, so check each one before deleting it.
Turn off default features
Many crates enable features by default that you do not need. Disable them and opt in to only what you use.
[dependencies]
serde = { version = "1", default-features = false, features = ["derive"] }
tokio = { version = "1", default-features = false, features = ["rt", "macros"] }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }Check each crate's documentation for its feature list, then build and add features back until the code compiles. cargo tree -e features shows which feature pulled in which crate, so you can trace a surprising dependency back to its source.
Keep features consistent in a workspace
In a workspace, declare shared dependencies once and inherit them. This keeps versions and feature sets identical, so each dependency compiles once.
# Cargo.toml (workspace root)
[workspace.dependencies]
serde = { version = "1", default-features = false, features = ["derive"] }
# member crate's Cargo.toml
[dependencies]
serde = { workspace = true }Use resolver = "2" (the default for edition 2021 and later) so that features for build-only tools and for different targets are not merged into your normal build.
Weigh each new dependency
Before adding a crate, ask:
- Does the standard library already do this?
- How many crates does it add? Run
cargo treebefore and after. - Does it depend on
synand procedural macros? These are expensive, as the next chapter explains. - Can a feature flag in your own crate make it optional?
Making a dependency optional lets users who do not need it skip the cost:
[dependencies]
serde_json = { version = "1", optional = true }
[features]
json = ["dep:serde_json"]Exercise
cargo tree --duplicates shows two versions of the same crate, and you set default-features = false on it in your own Cargo.toml. The crate still compiles with its default features. Give two likely reasons.
Show answer
Another crate in the dependency tree requests the default features, and Cargo unifies features across the build, so they stay enabled. Alternatively, the second version is pulled in by a different dependency that has not been updated, so your setting applies only to one of the two versions. Use cargo tree -e features -i <crate> to see which crate enables the feature.