-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add "Optimizing Build Performance" section to the Cargo book #15924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7b6d35f
79f56d5
5940262
97ec21d
df7e02d
5c1631c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Optimizing Build Performance | ||
|
||
Cargo uses configuration defaults that try to balance various aspects, including debuggability, runtime performance, build performance, binary size and others. Because of that, build performance is sometimes traded off for other benefits which may not be as important for your circumstances. This guide will step you through changes you can make to improve build performance. | ||
epage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Same as when optimizing runtime performance, be sure to measure these changes against the workflows you actually care about, as we provide general guidelines and your circumstances may be different. | ||
|
||
Example workflows to consider include: | ||
- Compiler feedback as you develop (`cargo check` after making a code change) | ||
- Test feedback as you develop (`cargo test` after making a code change) | ||
- CI builds | ||
|
||
All approaches described below require you to modify [Cargo configuration](#where-to-apply-configuration-changes). Note that some of them currently require using the nightly toolchain. | ||
|
||
## Reduce amount of generated debug information | ||
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Recommendation: Add to your `Cargo.toml` or `.cargo/config.toml`: | ||
|
||
```toml | ||
[profile.dev] | ||
debug = "line-tables-only" | ||
|
||
[profile.dev.package."*"] | ||
debug = false | ||
|
||
[profile.debugging] | ||
inherits = "dev" | ||
debug = true | ||
``` | ||
|
||
This will: | ||
- Change the [`dev` profile](../reference/profiles.md#dev) (default for development commands) to: | ||
- Limit [debug information](../reference/profiles.md#debug) for workspace members to what is needed for useful panic backtraces | ||
- Avoid generating any debug information for dependencies | ||
- Provide an opt-in for when debugging via [`--profile debugging`](../reference/profiles.md#custom-profiles) | ||
|
||
Trade-offs: | ||
- ✅ Faster per-crate build times | ||
- ✅ Faster link times | ||
- ✅ Smaller disk usage of the `target` directory | ||
- ❌ Requires a full rebuild to have a high-quality debugger experience | ||
|
||
## Use an alternative codegen backend | ||
|
||
> **This requires nightly/unstable features** | ||
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
The component of the Rust compiler that generates executable code is called a "codegen backend". The default backend is LLVM, which produces very optimized code, at the cost of relatively slow compilation time. You can try to use a different codegen backend in order to speed up the compilation of your crate. | ||
|
||
You can use the [Cranelift](https://github.com/rust-lang/rustc_codegen_cranelift) backend, which is designed for fast(er) compilation time. You can install this backend using rustup: | ||
|
||
```console | ||
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly | ||
``` | ||
|
||
and then enable it for a given Cargo profile using the `codegen-backend` option in `Cargo.toml`: | ||
```toml | ||
[profile.dev] | ||
codegen-backend = "cranelift" | ||
``` | ||
|
||
Since this is currently an unstable option, you will also need to either pass `-Z codegen-backend` to Cargo, or enable this unstable option in the `.cargo/config.toml` file. You can find more information about the unstable `codegen-backend` profile option [here](../reference/unstable.md#codegen-backend). | ||
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Note that the Cranelift backend might not support all features used by your crate. It is also available only for a limited set of targets. | ||
|
||
|
||
|
||
## Where to apply configuration changes | ||
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
You can apply the configuration changes described above in several places: | ||
|
||
|
||
- If you apply them to the `Cargo.toml` manifest, they will affect all developers who work on the given crate/project. | ||
- If you apply them to the `<workspace>/.cargo/config.toml` file, they will affect only you (unless this file is checked into version control). | ||
Kobzol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- If you apply them to the `$CARGO_HOME/.cargo/config.toml` file, they will be applied globally to all Rust projects that you work on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some "tips" at https://doc.rust-lang.org/nightly/cargo/reference/timings.html, should we switch that to a link to this page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe fold that page into this one and setup a redirect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to include a "how to profile my builds" in this page, which could link to the timings page as one of the points. And the timings page could then cross-link here for "how to optimize build perf.".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering bringing up dividing this into two sections
--timings
, compiler passes, macro lines,cargo llvm-lines
)I was just split on that vs framing those in terms of the current organization. For example, we could have a section on reducing the impact of generics and talk through using
cargo llvm-lines
to find large, repeated generic functions and discuss how to improve that.