Skip to content

Commit 7b6d35f

Browse files
committed
Add "Optimizing Build Performance" section to the Cargo book
1 parent 761c465 commit 7b6d35f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Optimizing Build Performance
2+
3+
Compilation of Rust crates can sometimes be rather slow, due to various reasons (such as the used compilation model and the design of the Rust language and its compiler). There are various approaches that can be used to optimize build performance, which mostly fall under two categories:
4+
5+
- Modify compiler or Cargo flags
6+
- Modify the source code of your crate(s)
7+
8+
This guide focuses on the first approach.
9+
10+
Below, you can find several methods that can be used to optimize build performance. It is important to nore that their effect varies a lot based on the compiled crate, and in some cases they can actually make compilation slower. You should always measure build performance on your crate(s) to determine if a given method described here is effective for your crate.
11+
12+
Note that some of these approaches currently require using the nightly toolchain.
13+
14+
## Reduce amount of generated debug information
15+
16+
By default, the `dev` [profile](../reference/profiles.md) enables generation of full debug information (debuginfo) both for local crates and also for all dependencies. This is useful if you want to debug your code with a debugger or profile it with a profiler, but it can also have a significant compilation and linking time cost.
17+
18+
You can reduce that cost by reducing the amount of debuginfo that is generated. The fastest option is `debug = false`, which completely turns off debuginfo generation, but a reasonable trade-off could also be setting `debug = "line-tables-only"`, which only generates enough debuginfo to support proper source code links in backtraces, which are generated e.g. when a panic happens.
19+
20+
Here is an example of configuring debuginfo generation in `Cargo.toml`:
21+
```toml
22+
[profile.dev]
23+
debug = false # or "line-tables-only"
24+
```
25+
26+
If you want to keep debuginfo for your crate only, but you do not need it for your dependencies, you can set `debug = false` as the default value for a given profile, and then enable debuginfo only for your crate:
27+
28+
```toml
29+
[profile.dev]
30+
debug = false
31+
32+
[profile.dev.package]
33+
<your-crate-name>.debug = true
34+
```
35+
36+
## Use an alternative codegen backend
37+
38+
> **This requires nightly/unstable features**
39+
40+
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.
41+
42+
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:
43+
44+
```console
45+
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
46+
```
47+
48+
and then enable it for a given Cargo profile using the `codegen-backend` option in `Cargo.toml`:
49+
```toml
50+
[profile.dev]
51+
codegen-backend = "cranelift"
52+
```
53+
54+
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).
55+
56+
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.

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)