-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Warning
If you are looking to submit an experience report, note that you should not be setting anything via RUSTFLAGS
; you should be setting this via either the Cargo profile setting in your top-level crate for a specific dependency, or the Cargo [hints]
table in a specific dependency.
Summary
Implementation: #15643, #15673
Documentation: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-hint-mostly-unused-option
This feature provides a new option in the [profile]
section to enable the rustc hint-mostly-unused
option. This is primarily useful to enable for specific dependencies:
[profile.dev.package.huge-mostly-unused-dependency]
hint-mostly-unused = true
To enable this feature, pass -Zprofile-hint-mostly-unused
. However, since this option is a hint, using it without passing -Zprofile-hint-mostly-unused
will only warn and ignore the profile option. Versions of Cargo prior to the introduction of this feature will give an "unused manifest key" warning, but will otherwise function without erroring. This allows using the hint in a crate's Cargo.toml
without mandating the use of a newer Cargo to build it.
Unresolved Issues
- Support in
.cargo/config.toml
To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Future Extensions
In the future, we may also want to provide a way for a crate to hint that it should have this option turned on by default, unless overridden by the top-level crate.
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Activity
mostly-unused
Cargo hint to optimize compile times for downstream users microsoft/windows-rs#3664biruburu commentedon Jul 15, 2025
papow65 commentedon Jul 15, 2025
Kobzol commentedon Jul 15, 2025
Just noting, in case it wasn't clear from the blog post, applying this flag to all your dependencies will most likely make your compilation times be terrible. It is designed to be only applied to a handful of selected large dependencies whose code is mostly unused in your crate graph. You should pass the flag to these dependencies using Cargo (https://blog.rust-lang.org/inside-rust/2025/07/15/call-for-testing-hint-mostly-unused/#plumbing-this-through-cargo-with-profiles).
Arnavion commentedon Jul 16, 2025
ze commentedon Jul 16, 2025
Tried on a crate of mine. I specifically only tried it on
aws-sdk-s3
.cargo 1.90.0-nightly (eabb4cd92 2025-07-09)
Each run had a
cargo clean
ran. There were two runs done.Before:
After:
To get a baseline on
cargo 1.88.0 (873a06493 2025-05-10)
:This looks great! 💯.
cemoktra commentedon Jul 16, 2025
workingjubilee commentedon Jul 16, 2025
@cemoktra How did you enable it?
Did you (incorrectly) add it to RUSTFLAGS as in #15644 (comment) or?
Per the blog post:
cemoktra commentedon Jul 16, 2025
omid commentedon Jul 16, 2025
JarredAllen commentedon Jul 16, 2025
On a decently-large project at my work (a bit over 1k crates including transitive dependencies, a bit over 100 binaries), I tested using
cargo clean && time cargo build --workspace
, oncargo 1.90.0-nightly (eabb4cd92 2025-07-09)
.Without this flag:
(a second try had a user time of 46m26s and a system time of 6m5s, so there's some variation, but small enough that I believe the results below are real differences)
When I applied the flag to tokio:
When I applied it to all of my dependencies:
It looks like this setting makes things take longer when applied indiscriminately, but it saves time when applied to the right crates. Just applying it to tokio was a 3% speedup on the entire build process, and I'd guess there are other, equally-sized wins in my dependency tree if I looked closer. (I think user and system times are the right values to look at, since I ran it on my laptop that has various background tasks that could interfere with the real time)
As a note, I applied it to all dependencies by putting this in my workspace-level
Cargo.toml
file:And I applied it just to tokio by doing the same thing but with
tokio
instead of"*"
.workingjubilee commentedon Jul 16, 2025
@omid @cemoktra Your reports are invalid. The RUSTFLAGS command is not the correct way to handle this.
Arnavion commentedon Jul 17, 2025
I put
[hints] mostly-unused = true
in k8s-openapi's Cargo.toml and then did a clean build of its two internal test suite crates withcargo test -Zprofile-hint-mostly-unused --no-run --timings ...
. k8s-openapi is a very large crate (auto-generated bindings from Kubernetes' OpenAPI spec) but the tests use a very small fraction of its types. For the sake of reproducability, these were the steps:Use this commit (nothing special about it, just latest master at the moment).
Add
[hints] mostly-unused = true
to the root Cargo.tomlRun:
Results, including the two
--timings
outputs:Without the hint, without
-j 1
: ~23sWith the hint, without
-j 1
: ~20sWithout the hint, with
-j 1
: ~58sWith the hint, with
-j 1
: ~49sWithout the hint, with
-j 1
, with--release
: ~144sWith the hint, with
-j 1
, with--release
: ~101slexeyOK commentedon Jul 17, 2025
I used hints on ferrumc-rs/ferrumc repository adding this to Cargo.toml
the release compile time improved from 506s to 367s (-26%) on 4 core machine (old)
i was calling cargo with
cargo build -r -Zprofile-hint-mostly-unused --timings
here are the timings: timings.zip
saethlin commentedon Jul 17, 2025
@lexeyOK How did you come up with that list of crates to apply the hint to? Instinct? Or did you test each one?
lexeyOK commentedon Jul 17, 2025
@saethlin
i picked the top crates with high codegen ratio from timings report + some experimentation
also other developer seems to not have any improvement with their's high end machine 😕 (codegen is fast on their end)
TypeLevelConsoli commentedon Jul 24, 2025
I know I'm using it incorrectly, but when I enable
rustflags = ["-Zhint-mostly-unused"]
in.cargo/config.toml
on anaxum
API project usingutoipa
andutoipa-swagger-ui
, the project fails to compile with afree(): invalid pointer
while attempting to download the UI during the build script.the error
You can reproduce the error by
cargo build
ing this project https://github.com/TypeLevelConsoli/mostly-unused-vs-swagger--cargo-errorIf you remove the
-Zhint-mostly-unused
, everything compiles without any issues.saethlin commentedon Jul 24, 2025
I can't reproduce that error. @TypeLevelConsoli can you file a bug on http://github.com/rust-lang/rust/issues/ and include what distro you are using? My instinct is that the bug you found is only exposed by the particular system libraries or linkage that is happening in your build. I'm hoping that just the distro/version is enough to cobble together a reproducer in Docker.
invalid pointer
error on build script while using-Zhint-mostly-unused
withopt-level=N>0
rust-lang/rust#144413TypeLevelConsoli commentedon Jul 24, 2025
I was able to narrow it down a bit. The issue only happens when I mix
-Zhint-most-unused
andopt-level={1,2,3}
(I had it set on my global config). That's probably why you couldn't reproduce it, @saethlin! (I updated the repo if you want to test again :))saethlin commentedon Jul 24, 2025
Yes, I can reproduce the crash now.
MarijnS95 commentedon Aug 10, 2025
As the maintainer of
ash
(low-level Vulkan bindings crate), we're also frequently plagued by (user reports of) relatively slow compiletimes for a rather large API surface that most users will not use (i.e. all Vulkan extensions under the sun). Per the blog-post, I'll share my findings here (also in case I'm doing something wrong). The nightly version isrustc 1.89.0 (29483883e 2025-08-04)
.If I understand this correctly the effect should apply to downstream consumers of this library crate, so I'm profiling this against our
ash-examples
crate rather than the library crate directly.Tip
EDIT: as it turns out this speedup applies to building the library crate directly as well, and my testing methodology was wrong; the clean build time of
ash-examples
was dominated by many smaller dependency crates. Changing the test to first build the crate once, then cleaning just the library crate usingcargo clean -p ash
before each test shows the same ±600ms speedup on the example, as we saw on the library as well. Results below have been rewritten.On my machine I've cloned https://github.com/ash-rs/ash at the latest commit ash-rs/ash@e044ecd1, and used the following command for a baseline test:
Then added this to
ash/Cargo.toml
:And re-ran the benchmark together with
-Zprofile-hint-mostly-unused
flag:That looks to be a nice ±600ms compile-time improvement for our library crate 🎉.
More or less coincidentally, also for completeness, I performed the same test against the library crate. Baseline:
And with
[hints] mostly-unused = true
:That's the same ±600ms improvement.
mostly-unused
hint to speed up compilation ash-rs/ash#1004