You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
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.
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
6
7
7
Example workflows to consider include:
8
8
- Compiler feedback as you develop (`cargo check` after making a code change)
9
9
- Test feedback as you develop (`cargo test` after making a code change)
10
10
- CI builds
11
11
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
13
13
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
15
21
16
22
Recommendation: Add to your `Cargo.toml` or `.cargo/config.toml`:
17
23
@@ -29,43 +35,42 @@ debug = true
29
35
30
36
This will:
31
37
- 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
34
40
- Provide an opt-in for when debugging via [`--profile debugging`](../reference/profiles.md#custom-profiles)
35
41
36
42
Trade-offs:
37
-
- ✅ Faster per-crate build times
43
+
- ✅ Faster build times
38
44
- ✅ Faster link times
39
-
- ✅ Smaller disk usage of the `target` directory
45
+
- ✅ Smaller disk usage of the `target` directory
40
46
- ❌ Requires a full rebuild to have a high-quality debugger experience
41
47
42
-
## Use an alternative codegen backend
48
+
###Use an alternative codegen backend
43
49
44
50
> **This requires nightly/unstable features**
45
51
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:
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:
61
53
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
- 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.
63
65
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.
64
67
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)
0 commit comments