Skip to content

Commit fbe6193

Browse files
authored
Add "Optimizing Build Performance" section to the Cargo book (#15924)
### What does this PR try to resolve? This PR introduces a new section into the Cargo book, which teaches Rust users how to optimize the build performance of crates. The motivation comes from the results of the Compiler performance survey, where people expressed interest in having an official guide for improving compilation times. This has been discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Build.20performance.20section.20in.20the.20Cargo.20book/with/537603929) briefly. ### How to test and review this PR? This is not expected to be complete to avoid a very large PR that gets blocked on the details over every part. Instead this will be merged after beta branch with the expectation that it will be sufficiently filled out by the next beta branch. Initially, we'll be focusing on content from https://corrode.dev/blog/tips-for-faster-rust-compile-times/. [Rendered](https://github.com/Kobzol/cargo/blob/performance-guide/src/doc/src/guide/build-performance.md)
2 parents 1194cda + 5c1631c commit fbe6193

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/doc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [Continuous Integration](guide/continuous-integration.md)
1818
* [Publishing on crates.io](reference/publishing.md)
1919
* [Cargo Home](guide/cargo-home.md)
20+
* [Optimizing Build Performance](guide/build-performance.md)
2021

2122
* [Cargo Reference](reference/index.md)
2223
* [The Manifest Format](reference/manifest.md)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Optimizing Build Performance
2+
3+
Cargo configuration options and source code organization patterns can help improve build performance, by prioritizing it over other aspects which may not be as important for your circumstances.
4+
5+
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, it is possible that some of these approaches might actually make build performance worse for your use-case.
6+
7+
Example workflows to consider include:
8+
- Compiler feedback as you develop (`cargo check` after making a code change)
9+
- Test feedback as you develop (`cargo test` after making a code change)
10+
- CI builds
11+
12+
## Cargo and Compiler Configuration
13+
14+
Cargo uses configuration defaults that try to balance several aspects, including debuggability, runtime performance, build performance, binary size and others. This section describes several approaches for changing these defaults that should be designed to maximize build performance.
15+
16+
You can set the described options either in the [`Cargo.toml` manifest](../reference/profiles.md), which will make them available for all developers who work on the given crate/project, or in the [`config.toml` configuration file](../reference/config.md), where you can apply them only for you or even globally for all your local projects.
17+
18+
### Reduce amount of generated debug information
19+
20+
Recommendation: Add to your `Cargo.toml` or `.cargo/config.toml`:
21+
22+
```toml
23+
[profile.dev]
24+
debug = "line-tables-only"
25+
26+
[profile.dev.package."*"]
27+
debug = false
28+
29+
[profile.debugging]
30+
inherits = "dev"
31+
debug = true
32+
```
33+
34+
This will:
35+
- Change the [`dev` profile](../reference/profiles.md#dev) (default for development commands) to:
36+
- Limit [debug information](../reference/profiles.md#debug) for workspace members to what is needed for useful panic backtraces
37+
- Avoid generating any debug information for dependencies
38+
- Provide an opt-in for when debugging via [`--profile debugging`](../reference/profiles.md#custom-profiles)
39+
40+
Trade-offs:
41+
- ✅ Faster build times
42+
- ✅ Faster link times
43+
- ✅ Smaller disk usage of the `target` directory
44+
- ❌ Requires a full rebuild to have a high-quality debugger experience
45+
46+
### Use an alternative codegen backend
47+
48+
Recommendation:
49+
50+
- Install the Cranelift codegen backend rustup component
51+
```console
52+
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
53+
```
54+
- Add to your `Cargo.toml` or `.cargo/config.toml`:
55+
```toml
56+
[profile.dev]
57+
codegen-backend = "cranelift"
58+
```
59+
- Run Cargo with `-Z codegen-backend` or enable the [`codegen-backend`](../reference/unstable.md#codegen-backend) feature in `.cargo/config.toml`.
60+
- This is required because this is currently an unstable feature.
61+
62+
This will change the [`dev` profile](../reference/profiles.md#dev) to use the [Cranelift codegen backend](https://github.com/rust-lang/rustc_codegen_cranelift) for generating machine code, instead of the default LLVM backend. The Cranelift backend should generate code faster than LLVM, which should result in improved build performance.
63+
64+
Trade-offs:
65+
- ✅ Faster code generation (`cargo build`)
66+
- ❌ **Requires using nightly Rust and an unstable Cargo feature**
67+
- ❌ Worse runtime performance of the generated code
68+
- Speeds up build part of `cargo test`, but might increase its test execution part
69+
- ❌ Only available for [certain targets](https://github.com/rust-lang/rustc_codegen_cranelift?tab=readme-ov-file#platform-support)
70+
- ❌ Might not support all Rust features (e.g. unwinding)

src/doc/src/guide/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ develop Rust packages.
1313
* [Continuous Integration](continuous-integration.md)
1414
* [Publishing on crates.io](../reference/publishing.md)
1515
* [Cargo Home](cargo-home.md)
16+
* [Optimizing Build Performance](build-performance.md)

0 commit comments

Comments
 (0)