Skip to content

Commit 672a470

Browse files
committed
Add optimization hints section to attributes
1 parent 7e042cd commit 672a470

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

@@ -495,25 +493,43 @@ When used on a function in an implementation, the attribute does nothing.
495493
The `must_use` attribute may also include a message by using
496494
`#[must_use = "message"]`. The message will be given alongside the warning.
497495
498-
### Inline attribute
496+
### Optimization Hints
499497
500-
The inline attribute suggests that the compiler should place a copy of
501-
the function or static in the caller, rather than generating code to
502-
call the function or access the static where it is defined.
498+
The `cold` and `inline` attributes give suggestions to the compiler to compile
499+
your code in a way that may be faster than what it would do without the hint.
500+
The attributes are only suggestions, and the compiler may choose to ignore it.
503501
504-
The compiler automatically inlines functions based on internal heuristics.
505-
Incorrectly inlining functions can actually make the program slower, so it
506-
should be used with care.
502+
#### `inline` Attribute
507503
508-
`#[inline]` and `#[inline(always)]` always cause the function to be serialized
509-
into the crate metadata to allow cross-crate inlining.
504+
The *`inline` attribute* suggests to the compiler that it should place a copy of
505+
the attributed function in the caller, rather than generating code to call the
506+
function where it is defined.
510507
511-
There are three different types of inline attributes:
508+
This attribute can be used on [functions] and function prototypes, although it
509+
does not do anything on function prototypes. When this attribute is applied to
510+
a function in a [trait], it applies only to that function when used as a default
511+
function for a trait implementation and not to all trait implementations.
512+
513+
> ***Note***: The compiler automatically inlines functions based on internal
514+
> heuristics. Incorrectly inlining functions can actually make the program
515+
> slower, so this attibute should be used with care.
516+
517+
There are three ways of using the inline attribute:
512518
513519
* `#[inline]` hints the compiler to perform an inline expansion.
514520
* `#[inline(always)]` asks the compiler to always perform an inline expansion.
515521
* `#[inline(never)]` asks the compiler to never perform an inline expansion.
516522
523+
#### `cold` Attribute
524+
525+
The *`cold` attribute* suggests to the compiler that the attributed function is
526+
unlikely to be called.
527+
528+
This attribute can be used on [functions] and function prototypes, although it
529+
does not do anything on function prototypes. When this attribute is applied to
530+
a function in a [trait], it applies only to that function when used as a default
531+
function for a trait implementation and not to all trait implementations.
532+
517533
### `derive`
518534
519535
The `derive` attribute allows certain traits to be automatically implemented
@@ -544,7 +560,7 @@ impl<T: PartialEq> PartialEq for Foo<T> {
544560
}
545561
```
546562

547-
You can implement `derive` for your own type through [procedural macros].
563+
You can implement `derive` for your own traits through [procedural macros].
548564

549565
[Doc comments]: comments.html#doc-comments
550566
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
@@ -570,4 +586,5 @@ You can implement `derive` for your own type through [procedural macros].
570586
[modules]: items/modules.html
571587
[statements]: statements.html
572588
[match expressions]: expressions/match-expr.html
573-
[external blocks]: items/external-blocks.html
589+
[external blocks]: items/external-blocks.html
590+
[trait]: items/traits.html

0 commit comments

Comments
 (0)