Skip to content

Commit 6b8de48

Browse files
committed
Stabilize #[optimize] attribute
1 parent 01b0ee7 commit 6b8de48

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/attributes/codegen.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,49 @@ Only the first use of the `no_builtins` attribute has effect.
220220
> [!NOTE]
221221
> `rustc` lints against any use following the first.
222222
223+
<!-- template:attributes -->
224+
r[attributes.codegen.optimize]
225+
## The `optimize` attribute
226+
227+
r[attributes.codegen.optimize.intro]
228+
The *`optimize` [attribute]* suggests whether a function, inherent method, trait method, or closure should be optimized for size or speed.
229+
230+
> [!EXAMPLE]
231+
> ```rust
232+
> #[optimize(size)]
233+
> pub fn example1() {}
234+
>
235+
> #[optimize(speed)]
236+
> pub fn example2() {}
237+
> ```
238+
239+
r[attributes.codegen.optimize.syntax]
240+
The syntax for the `optimize` attribute is:
241+
242+
```grammar,attributes
243+
@root OptimizeAttribute ->
244+
`optimize` `(` `size` `)`
245+
| `optimize` `(` `speed` `)`
246+
```
247+
248+
r[attributes.codegen.optimize.allowed-positions]
249+
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].
250+
251+
r[attributes.codegen.optimize.duplicates]
252+
Only the first use of `optimize` on a function has effect.
253+
254+
> [!NOTE]
255+
> `rustc` lints against any use following the first.
256+
257+
r[attributes.codegen.optimize.modes]
258+
The `optimize` attribute supports these modes:
259+
260+
- `#[optimize(size)]` suggests prioritizing minimizing code size, potentially at the cost of execution performance.
261+
- `#[optimize(speed)]` suggests prioritizing execution performance, potentially at the cost of code size.
262+
263+
> [!NOTE]
264+
> In every form the attribute is a hint. The compiler may ignore it.
265+
223266
r[attributes.codegen.target_feature]
224267
## The `target_feature` attribute
225268

0 commit comments

Comments
 (0)