Skip to content

Commit 24150b9

Browse files
committed
Call for Testing: Speeding up compilation with hint-mostly-unused
1 parent 9cd39a0 commit 24150b9

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
+++
2+
path = "inside-rust/2025/07/14/call-for-testing-hint-mostly-unused"
3+
title = "Call for Testing: Speeding up compilation with `hint-mostly-unused`"
4+
authors = ["Josh Triplett"]
5+
+++
6+
7+
I'm pleased to announce, and call for testing of, the nightly-only `rustc`
8+
`-Zhint-mostly-unused` option, and the corresponding nightly Cargo features
9+
`profile.hint-mostly-unused` and `hints.mostly-unused`. These options can help
10+
accelerate your Rust compile time in some cases, by avoiding compilation of
11+
items from your dependencies that you aren't using. Your feedback will help
12+
evaluate these features and make progress towards stabilizing them in the
13+
future.
14+
15+
## Background
16+
17+
Some crates provide comprehensive APIs with a very large surface area, yet many
18+
of their users need only a few entry points. In such cases, the compiler
19+
currently spends time generating code for the entire crate, and then ends up
20+
throwing most of that code away.
21+
22+
This can waste a substantial amount of compile time. Some large crates can take
23+
minutes to compile, and when you use these large crates as dependencies, they
24+
can take a disproportionate amount of the entire compilation time of your
25+
top-level crate.
26+
27+
In some cases, crates add feature flags to control compilation of their API
28+
surface. This can improve compile time, but adds complexity for users, who now
29+
need to determine which features they need for the APIs they use. Features also
30+
constitute a stable interface of a crate, and changing feature flags can be a
31+
breaking change. And even with feature flags, not every enabled function will
32+
be needed; there is a balance between granularity and ease of use.
33+
34+
## Deferring code generation with `-Zhint-mostly-unused`
35+
36+
The latest nightly `rustc` compiler now supports an option
37+
`-Zhint-mostly-unused`, which tells `rustc` that the crate's API surface will
38+
mostly go unused. This is a hint, and `rustc` doesn't make guarantees about its
39+
exact behavior (so that we can extend or improve it in the future), but
40+
currently it causes the compiler to defer as much of code generation as
41+
possible.
42+
43+
Applying this option to key crates you depend on (and use only a small subset
44+
of) can provide a substantial reduction in compile time, for debug builds and
45+
especially for release builds.
46+
47+
## How does this perform?
48+
49+
| **Dependency Crate** | **Before** | **`hint-mostly-unused`** | **Delta** |
50+
| :- | -: | -: | -: |
51+
| `windows`, all Graphics/UI features | 18.3s | 10.7s | -42% |
52+
| `windows`, all features | 3m 48s | 2m 55s | -23% |
53+
| `rustix`, `all-apis` feature | 5.9s | 4.3s | -27% |
54+
| `x11rb` and `x11rb-protocol` | 5.3s | 2.6s | -51% |
55+
| `aws-sdk-ec2` | 4m 07s | 2m 04s | -50% |
56+
57+
This performance improvement comes from deferring code generation. For
58+
instance, the `windows` crate in the first example goes from building in 15.1s
59+
of which 49% is codegen, to building in 7.5s of which 1% is codegen.
60+
61+
Note that this option does not provide a universal performance improvement for
62+
every crate. Using it for crates whose API surface is mostly used, and/or used
63+
in multiple different crates or binaries (e.g. multiple test binaries that each
64+
test a substantial swath of the API), may result in redoing code generation for
65+
the same items repeatedly.
66+
67+
## Plumbing this through Cargo with profiles
68+
69+
In order to support compiling specific dependencies with this option, Cargo
70+
supports a [profile option
71+
`hint-mostly-unused`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-hint-mostly-unused-option)
72+
to mark a crate with this hint:
73+
74+
```toml
75+
[profile.dev.package.huge-mostly-unused-dependency]
76+
hint-mostly-unused = true
77+
78+
[profile.release.package.huge-mostly-unused-dependency]
79+
hint-mostly-unused = true
80+
```
81+
82+
Note that if you build in multiple profiles (e.g. the default dev profile and
83+
the `-r` release profile), you'll want to set this flag for both, as shown
84+
above.
85+
86+
Because this option is still nightly-only, and depends on a nightly-only
87+
`rustc` option as well, enabling it requires passing
88+
`-Zprofile-hint-mostly-unused` on the `cargo` command line. Without this
89+
option, cargo will ignore this with a warning (but not an error, as it's still
90+
just a hint). Note that as with any profile option, it only takes effect when
91+
set in the top-level crate you're building.
92+
93+
You should not, in general, set this flag for all your dependencies, or for
94+
your own crate; you should set it selectively and test to make sure it provides
95+
an improvement. Using the [cargo `--timings`
96+
option](https://doc.rust-lang.org/nightly/cargo/reference/timings.html) can
97+
help to identify crates that might benefit from this hint. And when testing
98+
this hint, `--timings` can help detect whether the build time of *other* crates
99+
in the dependency tree went up.
100+
101+
## Making this automatic: Cargo `[hints]`
102+
103+
A profile hint still requires the top-level crate to configure the hint for
104+
some of its dependencies. However, some crates know that almost all of their
105+
users will want this hint enabled. For such crates, we've introduced a new
106+
`hints` mechanism in Cargo. Unlike profiles, which only apply when set in the
107+
top-level crate you build, hints are set within individual crates in your
108+
dependency graph. Hints provide a default behavior that you can still override.
109+
110+
A crate that knows most of its users will not use most of its API surface can
111+
set this hint in its `Cargo.toml` manifest:
112+
113+
```toml
114+
[hints]
115+
mostly-unused = true
116+
```
117+
118+
Note that setting a hint does *not* increase the Minimum Supported Rust Version
119+
(MSRV) of your crate. Hints are always ignored if not understood. So, you can
120+
safely set this hint immediately, without waiting for this feature to be
121+
stabilized, and users of nightly will immediately benefit (if they pass
122+
`-Zprofile-hint-mostly-unused` to cargo to enable the feature).
123+
124+
### Future hints
125+
126+
The `hints` mechanism in Cargo is a general feature, and we plan to make use of
127+
it for other purposes in the future. For instance, we may offer a
128+
`min-opt-level` option, for crates that are so performance-sensitive (e.g.
129+
numerics code) that most users will want to build them with optimization even
130+
in development mode. As with other hints, such a mechanism would still always
131+
allow the top-level crate to override.
132+
133+
## How do I help?
134+
135+
We'd love for you to test out this feature on the latest Rust nightly compiler[^nightly].
136+
137+
[^nightly]: Make sure to run `rustup update nightly` (or however you manage your Rust releases).
138+
139+
If you maintain a crate that has a large API surface, and you expect that the
140+
typical user might use only a fraction of it, try setting `hints.mostly-unused`
141+
in your `Cargo.toml`:
142+
143+
```toml
144+
[hints]
145+
mostly-unused = true
146+
```
147+
148+
You can test the effect of this by building a *typical* crate that depends on
149+
your crate, with and without this hint set, using nightly Cargo:
150+
`cargo +nightly -Zprofile-hint-mostly-unused build -r`. If this provides a
151+
noticeable performance improvement, consider setting it in your published
152+
crate.
153+
154+
Please report any performance improvements, or unexpected performance issues,
155+
or *especially* any failures you observe, to the [tracking issue for
156+
profile-hint-mostly-unused](https://github.com/rust-lang/cargo/issues/15644).
157+
We'll take this feedback into account to fix any issues with either the rustc
158+
compiler feature or the Cargo features, and to evaluate when those features
159+
have seen enough testing to be ready to stabilize.
160+
161+
## Acknowledgements
162+
163+
Much appreciation to:
164+
- [Ben Kimock](https://github.com/saethlin), whose work towards MIR-only rlibs
165+
provided inspiration and infrastructure for this work.
166+
- The [Rust All Hands](https://rustweek.org/all-hands/) and its organizers, for
167+
providing a forum to discuss and progress this work.

0 commit comments

Comments
 (0)