-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
supports @inline
/@noinline
annotations within a function body
#41312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,19 +188,32 @@ Give a hint to the compiler that this function is worth inlining. | |
Small functions typically do not need the `@inline` annotation, | ||
as the compiler does it automatically. By using `@inline` on bigger functions, | ||
an extra nudge can be given to the compiler to inline it. | ||
This is shown in the following example: | ||
|
||
`@inline` can be applied immediately before the definition or in its function body. | ||
|
||
```julia | ||
@inline function bigfunction(x) | ||
#= | ||
Function Definition | ||
=# | ||
# annotate long-form definition | ||
@inline function longdef(x) | ||
... | ||
end | ||
|
||
# annotate short-form definition | ||
@inline shortdef(x) = ... | ||
|
||
# annotate anonymous function that a `do` block creates | ||
f() do | ||
@inline | ||
... | ||
end | ||
``` | ||
|
||
!!! compat "Julia 1.7" | ||
The usage within a function body requires at least Julia 1.7. | ||
aviatesk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
macro inline(ex) | ||
esc(isa(ex, Expr) ? pushmeta!(ex, :inline) : ex) | ||
end | ||
macro inline() Expr(:meta, :inline) end | ||
|
||
""" | ||
@noinline | ||
|
@@ -209,22 +222,36 @@ Give a hint to the compiler that it should not inline a function. | |
|
||
Small functions are typically inlined automatically. | ||
By using `@noinline` on small functions, auto-inlining can be | ||
prevented. This is shown in the following example: | ||
prevented. | ||
|
||
`@noinline` can be applied immediately before the definition or in its function body. | ||
|
||
```julia | ||
@noinline function smallfunction(x) | ||
#= | ||
Function Definition | ||
=# | ||
# annotate long-form definition | ||
@noinline function longdef(x) | ||
... | ||
end | ||
|
||
# annotate short-form definition | ||
@noinline shortdef(x) = ... | ||
|
||
# annotate anonymous function that a `do` block creates | ||
f() do | ||
@noinline | ||
... | ||
end | ||
``` | ||
|
||
!!! compat "Julia 1.7" | ||
The usage within a function body requires at least Julia 1.7. | ||
aviatesk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
!!! note | ||
If the function is trivial (for example returning a constant) it might get inlined anyway. | ||
""" | ||
macro noinline(ex) | ||
esc(isa(ex, Expr) ? pushmeta!(ex, :noinline) : ex) | ||
end | ||
macro noinline() Expr(:meta, :noinline) end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we can delete There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think we can unify them. Will do that in a follow up PR though. |
||
|
||
""" | ||
@pure ex | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.