Skip to content

Commit df7e02d

Browse files
committed
Split text into two sections
1 parent 97ec21d commit df7e02d

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed
Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
# Optimizing Build Performance
22

3-
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.
3+
This guide will step you through Cargo configuration options and source code organization patterns that can help improve build performance, by prioritizing it over other aspects which may not be as important for your circumstances.
44

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.
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.
66

77
Example workflows to consider include:
88
- Compiler feedback as you develop (`cargo check` after making a code change)
99
- Test feedback as you develop (`cargo test` after making a code change)
1010
- CI builds
1111

12-
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.
12+
## Cargo and Compiler Configuration
1313

14-
## Reduce amount of generated debug information
14+
> Note that some approaches described in this section currently require using the nightly toolchain.
15+
16+
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.
17+
18+
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.
19+
20+
### Reduce amount of generated debug information
1521

1622
Recommendation: Add to your `Cargo.toml` or `.cargo/config.toml`:
1723

@@ -29,43 +35,42 @@ debug = true
2935

3036
This will:
3137
- Change the [`dev` profile](../reference/profiles.md#dev) (default for development commands) to:
32-
- Limit [debug information](../reference/profiles.md#debug) for workspace members to what is needed for useful panic backtraces
33-
- Avoid generating any debug information for dependencies
38+
- Limit [debug information](../reference/profiles.md#debug) for workspace members to what is needed for useful panic backtraces
39+
- Avoid generating any debug information for dependencies
3440
- Provide an opt-in for when debugging via [`--profile debugging`](../reference/profiles.md#custom-profiles)
3541

3642
Trade-offs:
37-
- ✅ Faster per-crate build times
43+
- ✅ Faster build times
3844
- ✅ Faster link times
39-
- ✅ Smaller disk usage of the `target` directory
45+
- ✅ Smaller disk usage of the `target` directory
4046
- ❌ Requires a full rebuild to have a high-quality debugger experience
4147

42-
## Use an alternative codegen backend
48+
### Use an alternative codegen backend
4349

4450
> **This requires nightly/unstable features**
4551
46-
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.
47-
48-
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:
49-
50-
```console
51-
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
52-
```
53-
54-
and then enable it for a given Cargo profile using the `codegen-backend` option in `Cargo.toml`:
55-
```toml
56-
[profile.dev]
57-
codegen-backend = "cranelift"
58-
```
59-
60-
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).
52+
Recommendation:
6153

62-
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.
54+
- Install the Cranelift codegen backend rustup component
55+
```console
56+
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
57+
```
58+
- Add to your `Cargo.toml` or `.cargo/config.toml`:
59+
```toml
60+
[profile.dev]
61+
codegen-backend = "cranelift"
62+
```
63+
- Run Cargo with `-Z codegen-backend` or enable the [`codegen-backend`](../reference/unstable.md#codegen-backend) feature in `.cargo/config.toml`.
64+
- This is required because this is currently an unstable feature.
6365

66+
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.
6467

65-
## Where to apply configuration changes
66-
67-
You can apply the configuration changes described above in several places:
68-
69-
- If you apply them to the `Cargo.toml` manifest, they will affect all developers who work on the given crate/project.
70-
- If you apply them to the `<workspace>/.cargo/config.toml` file, they will affect only you (unless this file is checked into version control).
71-
- 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.
68+
Trade-offs:
69+
- ✅ Faster build times
70+
- ❌ Worse runtime performance of the generated code
71+
- ❌ Requires using nightly Rust and an unstable feature
72+
- ❌ Only available for [certain targets](https://github.com/rust-lang/rustc_codegen_cranelift?tab=readme-ov-file#platform-support)
73+
- ❌ Might not support all Rust features (e.g. unwinding)
74+
75+
## Source code organization
76+
TODO

0 commit comments

Comments
 (0)