Skip to content

Commit 14ab0ce

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 37fbc42 commit 14ab0ce

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
@@ -584,9 +584,9 @@ Individual hints may have an associated unstable feature gate that you need to
584584
pass in order to apply the configuration they specify, but if you don't specify
585585
that unstable feature gate, you will again get only a warning, not an error.
586586

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

591591
> **MSRV:** Respected as of 1.90.
592592

doc/book/src/reference/unstable.md

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

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

0 commit comments

Comments
 (0)