You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 65bdfe5
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: src/attributes/codegen.md
+50Lines changed: 50 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,6 +235,56 @@ Only the first use of the `no_builtins` attribute has effect.
235
235
> [!NOTE]
236
236
> `rustc` lints against any use following the first.
237
237
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 [traitimpl], 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.
0 commit comments