Sub topic 9
Split large crates into a workspace
This chapter assumes you can build and test a Rust project with Cargo, read a Cargo.toml, and have already measured a build with cargo build --timings. Here you will see why one large crate compiles slowly and how a workspace of smaller crates helps.
Why one big crate is slow
The crate is the unit of compilation in Rust. Cargo invokes rustc once per crate, and incremental compilation and parallelism work within that unit. If you edit one function in a single 100,000-line crate, rustc must re-check the crate and re-run code generation for every part the change touches. The front end is also mostly serial, so extra cores help little.
Splitting the code into several crates changes two things:
- Independent crates compile in parallel. Cargo schedules them as soon as their dependencies finish.
- A change rebuilds only the edited crate and the crates that depend on it. Crates below it in the dependency graph are reused.
A workspace layout
A workspace is a set of crates that share one Cargo.lock and one target directory. The root manifest lists the members.
# Cargo.toml (workspace root)
[workspace]
resolver = "2"
members = ["app", "core", "storage"]
[workspace.dependencies]
serde = { version = "1", default-features = false, features = ["derive"] }Each member then inherits the shared declaration. This keeps one version and one feature set across the workspace.
# storage/Cargo.toml
[package]
name = "storage"
version = "0.1.0"
edition = "2021"
[dependencies]
core = { path = "../core" }
serde = { workspace = true }Here core is a name chosen for this example. In real projects, avoid naming a crate core, because it clashes with Rust's built-in core crate. Pick a name such as app_core.
Choosing where to cut
The shape of the dependency graph decides how much you gain. Compare these two layouts.
In the first layout, storage and net build at the same time once app_core is done. In the second, each crate waits for the previous one, so you get little parallelism. A change in a also rebuilds everything above it.
Good cut points follow these rules:
- Put stable, widely used code at the bottom (types, traits, utilities). Put frequently edited code near the top.
- Keep the lowest crates small, since every change to them invalidates everything above.
- Separate heavy dependencies into their own crates. A crate that wraps a large library only rebuilds when its own code changes.
- Avoid cycles. Cargo rejects them, so shared types must move down into a common crate.
Check the result
Run the timings report again after the split.
cargo build --timingsOpen target/cargo-timings/cargo-timing.html and look at the bars. You want several crates running side by side, and no single long bar at the end that holds up the rest.
Costs and gotchas
- Items used across crates must be
pub, which can widen your public API. Usepub(crate)inside crates and keep the shared surface deliberate. - Generic and
#[inline]functions are compiled in the crate that uses them, so splitting does not remove their cost. See the chapter on monomorphization. - Too many tiny crates add manifest overhead and link steps. Splitting a 10,000-line crate into twenty crates rarely pays off.
- Passing
--workspacebuilds every member. During development, build only what you need withcargo check -p app.
Exercise
A workspace has crates a, b, c and app. Crate b depends on a, c depends on a, and app depends on b and c. You edit a function in c. Which crates are rebuilt, and which can build in parallel on a clean build?
Show answer
Editing c rebuilds c and app. Crates a and b are reused from the earlier build. On a clean build, a builds first. Then b and c build in parallel, and app builds last.