julia> x = [1]
1-element Vector{Int64}:
1
julia> let a = x
@inbounds b = a[2]
end
ERROR: BoundsError: attempt to access 1-element Vector{Int64} at index [2]
Stacktrace:
[1] getindex(A::Vector{Int64}, i1::Int64)
@ Base ./array.jl:861
[2] top-level scope
@ REPL[8]:2
To be specific, @inbounds only works inside type-stable compiled contexts with inlining enabled. If you’re not compiled or type-stable, then the overhead from bounds checking is the least of your worries.
It’s worth pointing out that this overhead is pretty negligible in general. The only time it’s likely to make much of a difference is when it prevents SIMD.