Skip to content

Commit 01defa4

Browse files
committed
Add optimization hints section to attributes
1 parent 13d9a03 commit 01defa4

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/attributes.md

+31-14
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ names have meaning.
126126
- `ignore` - indicates that this test function is disabled.
127127
- `should_panic` - indicates that this test function should panic, inverting the
128128
success condition.
129-
- `cold` - The function is unlikely to be executed, so optimize it (and calls
130-
to it) differently.
131129

132130
## FFI attributes
133131

@@ -405,25 +403,43 @@ When used on a function in an implementation, the attribute does nothing.
405403
The `must_use` attribute may also include a message by using
406404
`#[must_use = "message"]`. The message will be given alongside the warning.
407405
408-
### Inline attribute
406+
### Optimization Hints
409407
410-
The inline attribute suggests that the compiler should place a copy of
411-
the function or static in the caller, rather than generating code to
412-
call the function or access the static where it is defined.
408+
The `cold` and `inline` attributes give suggestions to the compiler to compile
409+
your code in a way that may be faster than what it would do without the hint.
410+
The attributes are only suggestions, and the compiler may choose to ignore it.
413411
414-
The compiler automatically inlines functions based on internal heuristics.
415-
Incorrectly inlining functions can actually make the program slower, so it
416-
should be used with care.
412+
#### `inline` Attribute
417413
418-
`#[inline]` and `#[inline(always)]` always cause the function to be serialized
419-
into the crate metadata to allow cross-crate inlining.
414+
The *`inline` attribute* suggests to the compiler that it should place a copy of
415+
the attributed function in the caller, rather than generating code to call the
416+
function where it is defined.
420417
421-
There are three different types of inline attributes:
418+
This attribute can be used on [functions] and function prototypes, although it
419+
does not do anything on function prototypes. When this attribute is applied to
420+
a function in a [trait], it applies only to that function when used as a default
421+
function for a trait implementation and not to all trait implementations.
422+
423+
> ***Note***: The compiler automatically inlines functions based on internal
424+
> heuristics. Incorrectly inlining functions can actually make the program
425+
> slower, so this attibute should be used with care.
426+
427+
There are three ways of using the inline attribute:
422428
423429
* `#[inline]` hints the compiler to perform an inline expansion.
424430
* `#[inline(always)]` asks the compiler to always perform an inline expansion.
425431
* `#[inline(never)]` asks the compiler to never perform an inline expansion.
426432
433+
#### `cold` Attribute
434+
435+
The *`cold` attribute* suggests to the compiler that the attributed function is
436+
unlikely to be called.
437+
438+
This attribute can be used on [functions] and function prototypes, although it
439+
does not do anything on function prototypes. When this attribute is applied to
440+
a function in a [trait], it applies only to that function when used as a default
441+
function for a trait implementation and not to all trait implementations.
442+
427443
### `derive`
428444
429445
The `derive` attribute allows certain traits to be automatically implemented
@@ -454,7 +470,7 @@ impl<T: PartialEq> PartialEq for Foo<T> {
454470
}
455471
```
456472

457-
You can implement `derive` for your own type through [procedural macros].
473+
You can implement `derive` for your own traits through [procedural macros].
458474

459475
[Doc comments]: comments.html#doc-comments
460476
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
@@ -482,4 +498,5 @@ You can implement `derive` for your own type through [procedural macros].
482498
[match expressions]: expressions/match-expr.html
483499
[external blocks]: items/external-blocks.html
484500
[items]: items.html
485-
[conditional compilation]: conditional-compilation.html
501+
[conditional compilation]: conditional-compilation.html
502+
[trait]: items/traits.html

0 commit comments

Comments
 (0)