Skip to content

Commit ff1b273

Browse files
committed
docs(hints): explain minimum optimization levels
Document the unstable gate, accepted range, profile precedence, non-recursive scope, and the narrow cases where packages should request optimization. Include an explicit debugging override so top-level users retain control. Signed-off-by: 0xPoe <poe.liu@pm.me>
1 parent 646e4fd commit ff1b273

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

doc/book/src/reference/manifest.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,9 @@ Individual hints may have an associated unstable feature gate that you need to
590590
pass in order to apply the configuration they specify, but if you don't specify
591591
that unstable feature gate, you will again get only a warning, not an error.
592592

593-
There are no stable hints at this time. See the [hint-mostly-unused
594-
documentation](unstable.md#profile-hint-mostly-unused-option) for information
595-
on an unstable hint.
593+
There are no stable hints at this time. See the documentation for the unstable
594+
[`mostly-unused`](unstable.md#profile-hint-mostly-unused-option) and
595+
[`min-opt-level`](unstable.md#package-min-opt-level-hint) hints.
596596

597597
> **MSRV:** Respected as of 1.90.
598598

doc/book/src/reference/unstable.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Each new feature described below should explain how to use it.
110110
* `Cargo.toml` extensions
111111
* [Profile `rustflags` option](#profile-rustflags-option) --- Passed directly to rustc.
112112
* [Profile `hint-mostly-unused` option](#profile-hint-mostly-unused-option) --- Hint that a dependency is mostly unused, to optimize compilation time.
113+
* [Package `min-opt-level` hint](#package-min-opt-level-hint) --- Request a numeric optimization floor for one package.
113114
* [codegen-backend](#codegen-backend) --- Select the codegen backend used by rustc.
114115
* [per-package-target](#per-package-target) --- Sets the `--target` to use for each individual package.
115116
* [artifact dependencies](#artifact-dependencies) --- Allow build artifacts to be included into other build artifacts and build them for different targets.
@@ -924,6 +925,54 @@ This will cause the crate to default to hint-mostly-unused, unless overridden
924925
via `profile`, which takes precedence, and which can only be specified in the
925926
top-level crate being built.
926927

928+
## Package `min-opt-level` hint
929+
* Tracking Issue: [#17334](https://github.com/rust-lang/cargo/issues/17334)
930+
* RFC: [#3924](https://github.com/rust-lang/rfcs/pull/3924)
931+
932+
The `min-opt-level` hint lets a package request a numeric optimization floor
933+
for itself:
934+
935+
```toml
936+
# In example-dependency's Cargo.toml
937+
[hints]
938+
min-opt-level = 2
939+
```
940+
941+
To enable this feature, pass `-Zhint-min-opt-level`. Without the flag, Cargo
942+
warns and ignores a positive hint. Older versions of Cargo may instead report
943+
an unused manifest key, but specifying the hint does not change the package's
944+
MSRV.
945+
946+
The hint accepts the numeric optimization levels 0, 1, 2, and 3. Other values
947+
produce a warning and are ignored. If the selected [profile](profiles.md) has a
948+
lower numeric [`opt-level`](profiles.md#opt-level), Cargo raises it to the
949+
hinted minimum for that package. A higher numeric level remains unchanged. The
950+
hint also applies to Cargo's built-in `opt-level = 0` default for build
951+
dependencies. The size optimization levels `"s"` and `"z"` override a numeric
952+
hint.
953+
954+
Explicit [package overrides](profiles.md#overrides) from the top-level package,
955+
including `[profile.dev.package."*"]`, take precedence over a hint, as do
956+
explicit build overrides. A lower numeric `opt-level` in the general profile
957+
cannot reduce the hinted floor. For example:
958+
959+
```toml
960+
# In the top-level package's Cargo.toml
961+
962+
# Does not affect example-dependency: the hint keeps it at opt-level 2.
963+
[profile.dev]
964+
opt-level = 0
965+
966+
# Takes precedence over the hint, forcing example-dependency back to no
967+
# optimization when debugging it.
968+
[profile.dev.package.example-dependency]
969+
opt-level = 0
970+
```
971+
972+
A hint only affects the package that declares it, not its dependencies. Use
973+
this hint when optimization either makes a typical full build faster or when
974+
the package is many times slower without it.
975+
927976
## rustdoc-map
928977
* Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296)
929978

0 commit comments

Comments
 (0)