Skip to content

Commit 65bdfe5

Browse files
committed
Stabilize #[optimize] attribute
1 parent e24eecf commit 65bdfe5

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎src/attributes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ The following is an index of all built-in attributes.
255255
- [`cold`] --- Hint that a function is unlikely to be called.
256256
- [`naked`] --- Prevent the compiler from emitting a function prologue and epilogue.
257257
- [`no_builtins`] --- Disables use of certain built-in functions.
258+
- [`optimize`] --- Controls the level of optimization to be applied to the function.
258259
- [`target_feature`] --- Configure platform-specific code generation.
259260
- [`track_caller`] --- Pass the parent call location to `std::panic::Location::caller()`.
260261
- [`instruction_set`] --- Specify the instruction set used to generate a function's code.
@@ -328,6 +329,7 @@ The following is an index of all built-in attributes.
328329
[`no_mangle`]: abi.md#the-no_mangle-attribute
329330
[`no_std`]: names/preludes.md#the-no_std-attribute
330331
[`non_exhaustive`]: attributes/type_system.md#the-non_exhaustive-attribute
332+
[`optimize`]: attributes/codegen.md#the-optimize-attribute
331333
[`panic_handler`]: panic.md#the-panic_handler-attribute
332334
[`path`]: items/modules.md#the-path-attribute
333335
[`proc_macro_attribute`]: procedural-macros.md#the-proc_macro_attribute-attribute

‎src/attributes/codegen.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,56 @@ Only the first use of the `no_builtins` attribute has effect.
235235
> [!NOTE]
236236
> `rustc` lints against any use following the first.
237237
238+
<!-- template:attributes -->
239+
r[attributes.codegen.optimize]
240+
## The `optimize` attribute
241+
242+
r[attributes.codegen.optimize.intro]
243+
The *`optimize` [attribute]* suggests whether a function, inherent method, trait method, or closure should be optimized for size or speed.
244+
245+
This attribute overrides optimiziation settings for the function, and is applied regardless of the global `opt-level`.
246+
247+
> [!EXAMPLE]
248+
> ```rust
249+
> #[optimize(size)]
250+
> pub fn example1() {}
251+
>
252+
> #[optimize(speed)]
253+
> pub fn example2() {}
254+
>
255+
> #[optimize(none)]
256+
> pub fn example3() {}
257+
> ```
258+
259+
r[attributes.codegen.optimize.syntax]
260+
The syntax for the `optimize` attribute is:
261+
262+
```grammar,attributes
263+
@root OptimizeAttribute ->
264+
`optimize` `(` `size` `)`
265+
| `optimize` `(` `speed` `)`
266+
| `optimize` `(` `none` `)`
267+
```
268+
269+
r[attributes.codegen.optimize.allowed-positions]
270+
The `optimize` attribute may only be applied to [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [trait impl], and associated functions in a [trait definition].
271+
272+
r[attributes.codegen.optimize.closures]
273+
Unless overridden, [closures] inherit the `optimize` attribute of the enclosing function.
274+
275+
r[attributes.codegen.optimize.duplicates]
276+
Applying multiple `optimize` attributes to the same function is an error.
277+
278+
r[attributes.codegen.optimize.modes]
279+
The `optimize` attribute supports these modes:
280+
281+
- `#[optimize(size)]` suggests prioritizing minimizing code size, potentially at the cost of execution performance.
282+
- `#[optimize(speed)]` suggests prioritizing execution performance, potentially at the cost of code size.
283+
- `#[optimize(none)]` suggests not optimizing the function, with similar effects to applying -Copt-level=0 locally.
284+
285+
> [!NOTE]
286+
> In every form the attribute is a hint. The compiler may ignore it.
287+
238288
r[attributes.codegen.target_feature]
239289
## The `target_feature` attribute
240290

‎src/items/functions.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ The attributes that have meaning on a function are:
707707
- [`link_section`]
708708
- [`must_use`]
709709
- [`no_mangle`]
710+
- [`optimize`]
710711
- [Lint check attributes]
711712
- [Procedural macro attributes]
712713
- [Testing attributes]
@@ -764,6 +765,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
764765
[`export_name`]: ../abi.md#the-export_name-attribute
765766
[`link_section`]: ../abi.md#the-link_section-attribute
766767
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
768+
[`optimize`]: ../attributes/codegen.md#the-optimize-attribute
767769
[built-in attributes]: ../attributes.md#built-in-attributes-index
768770
[trait item]: traits.md
769771
[method]: associated-items.md#methods

0 commit comments

Comments
 (0)