-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
improve docs for @inbounds
and Base.@propagate_inbounds
#50157
base: master
Are you sure you want to change the base?
Changes from all commits
b071872
fdb2f91
d0b9427
2fc8bbe
8f7ba3b
01df742
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -846,13 +846,14 @@ macro boundscheck(blk) | |
end | ||
|
||
""" | ||
@inbounds(blk) | ||
@inbounds block | ||
|
||
Eliminates array bounds checking within expressions. | ||
|
||
In the example below the in-range check for referencing | ||
element `i` of array `A` is skipped to improve performance. | ||
Eliminates bounds checking within the block. | ||
This macro can be used to improve performance by informing the compiler that accesses to | ||
array elements or object fields are assuredly within bounds. | ||
|
||
In the example below the in-range check for referencing element `i` of array `A` is skipped | ||
to improve performance. | ||
```julia | ||
function sum(A::AbstractArray) | ||
r = zero(eltype(A)) | ||
|
@@ -864,14 +865,44 @@ end | |
``` | ||
|
||
!!! warning | ||
|
||
Using `@inbounds` may return incorrect results/crashes/corruption | ||
for out-of-bounds indices. The user is responsible for checking it manually. | ||
Only use `@inbounds` when you are certain that all accesses are in bounds (as | ||
undefined behavior, e.g. crashes, might occur if this assertion is violated). For | ||
example, using `1:length(A)` instead of `eachindex(A)` in a function like | ||
the one above is _not_ safely inbounds because the first index of `A` may not | ||
be `1` for all user defined types that subtype `AbstractArray`. | ||
|
||
!!! note | ||
`@inbounds` eliminates bounds checks in methods called within a given block, but not | ||
those that are syntactically within the given block. | ||
However, keep in mind that the `@inbounds` context propagates only one function call | ||
layer deep. For example, if an `@inbounds` block includes a call to `f()`, which in turn | ||
calls `g()`, bounds checks that are syntactically within `f()` will be eliminated, | ||
while ones within `g()` will not. | ||
If you want to eliminates bounds checks within `g()` also, | ||
you need to annotate [`Base.@propagate_inbounds`](@ref) on `f()`. | ||
|
||
# Example | ||
|
||
```julia-repl | ||
julia> code_typed((Vector{Any},Int)) do a, i | ||
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. The 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. It is also not accurate anymore, since |
||
a[i] | ||
end |> only | ||
CodeInfo( | ||
1 ─ %1 = Base.arrayref(true, a, i)::Any | ||
└── return %1 | ||
) => Any | ||
|
||
julia> code_typed((Vector{Any},Int)) do a, i | ||
@inbounds a[i] # The bounds check for `arrayref` is turned off. | ||
end |> only | ||
CodeInfo( | ||
1 ─ %1 = Base.arrayref(false, a, i)::Any | ||
└── return %1 | ||
) => Any | ||
``` | ||
|
||
""" | ||
macro inbounds(blk) | ||
return Expr(:block, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.