Skip to content

Commit ee5040b

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

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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. (The compiler does this using the same machinery as
42+
`#[inline(always)]`.)
43+
44+
Applying this option to key crates you depend on (and use only a small subset
45+
of) can provide a substantial reduction in compile time, for debug builds and
46+
especially for release builds.
47+
48+
## How does this perform?
49+
50+
| **Dependency Crate** | **Before** | **`hint-mostly-unused`** | **Delta** |
51+
| :- | -: | -: | -: |
52+
| `windows`, all Graphics/UI features | 18.3s | 10.7s | -42% |
53+
| `windows`, all features | 3m 48s | 2m 55s | -23% |
54+
| `rustix`, `all-apis` feature | 5.9s | 4.3s | -27% |
55+
| `x11rb` and `x11rb-protocol` | 5.3s | 2.6s | -51% |
56+
| `aws-sdk-ec2` | 4m 07s | 2m 04s | -50% |
57+
58+
This performance improvement comes from deferring code generation. For
59+
instance, the `windows` crate in the first example goes from building in 15.1s
60+
of which 49% is codegen, to building in 7.5s of which 1% is codegen.
61+
62+
Note that this option does not provide a universal performance improvement for
63+
every crate. Using it for crates whose API surface is mostly used, and/or used
64+
in multiple different crates or binaries (e.g. multiple test binaries that each
65+
test a substantial swath of the API), may result in redoing code generation for
66+
the same items repeatedly.
67+
68+
## Plumbing this through Cargo with profiles
69+
70+
In order to support compiling specific dependencies with this option, Cargo
71+
supports a [profile option
72+
`hint-mostly-unused`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-hint-mostly-unused-option)
73+
to mark a crate with this hint:
74+
75+
```toml
76+
[profile.dev.package.huge-mostly-unused-dependency]
77+
hint-mostly-unused = true
78+
79+
[profile.release.package.huge-mostly-unused-dependency]
80+
hint-mostly-unused = true
81+
```
82+
83+
Note that if you build in multiple profiles (e.g. the default dev profile and
84+
the `-r` release profile), you'll want to set this flag for both, as shown
85+
above.
86+
87+
Because this option is still nightly-only, and depends on a nightly-only
88+
`rustc` option as well, enabling it requires passing
89+
`-Zprofile-hint-mostly-unused` on the `cargo` command line. Without this
90+
option, cargo will ignore this with a warning (but not an error, as it's still
91+
just a hint). Note that as with any profile option, it only takes effect when
92+
set in the top-level crate you're building.
93+
94+
You should not, in general, set this flag for all your dependencies, or for
95+
your own crate; you should set it selectively and test to make sure it provides
96+
an improvement. Using the [cargo `--timings`
97+
option](https://doc.rust-lang.org/nightly/cargo/reference/timings.html) can
98+
help to identify crates that might benefit from this hint. And when testing
99+
this hint, `--timings` can help detect whether the build time of *other* crates
100+
in the dependency tree went up.
101+
102+
## Making this automatic: Cargo `[hints]`
103+
104+
A profile hint still requires the top-level crate to configure the hint for
105+
some of its dependencies. However, some crates know that almost all of their
106+
users will want this hint enabled. For such crates, we've introduced a new
107+
`hints` mechanism in Cargo. Unlike profiles, which only apply when set in the
108+
top-level crate you build, hints are set within individual crates in your
109+
dependency graph. Hints provide a default behavior that you can still override.
110+
111+
A crate that knows most of its users will not use most of its API surface can
112+
set this hint in its `Cargo.toml` manifest:
113+
114+
```toml
115+
[hints]
116+
mostly-unused = true
117+
```
118+
119+
Note that setting a hint does *not* increase the Minimum Supported Rust Version
120+
(MSRV) of your crate. Hints are always ignored if not understood. So, you can
121+
safely set this hint immediately, without waiting for this feature to be
122+
stabilized, and users of nightly will immediately benefit (if they pass
123+
`-Zprofile-hint-mostly-unused` to cargo to enable the feature).
124+
125+
### Future hints
126+
127+
The `hints` mechanism in Cargo is a general feature, and we plan to make use of
128+
it for other purposes in the future. For instance, we may offer a
129+
`min-opt-level` option, for crates that are so performance-sensitive (e.g.
130+
numerics code) that most users will want to build them with optimization even
131+
in development mode. As with other hints, such a mechanism would still always
132+
allow the top-level crate to override.
133+
134+
## How do I help?
135+
136+
We'd love for you to test out this feature on the latest Rust nightly compiler[^nightly].
137+
138+
[^nightly]: Make sure to run `rustup update nightly` (or however you manage your Rust releases).
139+
140+
If you maintain a crate that has a large API surface, and you expect that the
141+
typical user might use only a fraction of it, try setting `hints.mostly-unused`
142+
in your `Cargo.toml`:
143+
144+
```toml
145+
[hints]
146+
mostly-unused = true
147+
```
148+
149+
You can test the effect of this by building a *typical* crate that depends on
150+
your crate, with and without this hint set, using nightly Cargo:
151+
`cargo +nightly -Zprofile-hint-mostly-unused build -r`. If this provides a
152+
noticeable performance improvement, consider setting it in your published
153+
crate.
154+
155+
Please report any performance improvements, or unexpected performance issues,
156+
or *especially* any failures you observe, to the [tracking issue for
157+
profile-hint-mostly-unused](https://github.com/rust-lang/cargo/issues/15644).
158+
We'll take this feedback into account to fix any issues with either the rustc
159+
compiler feature or the Cargo features, and to evaluate when those features
160+
have seen enough testing to be ready to stabilize.
161+
162+
## Acknowledgements
163+
164+
Much appreciation to:
165+
- [Ben Kimock](https://github.com/saethlin), whose work towards MIR-only rlibs
166+
provided inspiration and infrastructure for this work.
167+
- The [Rust All Hands](https://rustweek.org/all-hands/) and its organizers, for
168+
providing a forum to discuss and progress this work.

0 commit comments

Comments
 (0)