Skip to content

Commit d54a455

Browse files
authored
Hoist boundscheck in vector setindex for arrays (#53149)
As noted by @N5N3 in #40962 (comment), the bounds-check on the elementwise `setindex!` prevents vectorization. Explicitly performing the bounds-check and marking the `setindex!` as `@inbounds` speeds up the operation. ```julia julia> A = zeros(1000); B = rand(1000); julia> @Btime $A[1:end] = @view $B[1:end]; 689.940 ns (0 allocations: 0 bytes) # master 97.629 ns (0 allocations: 0 bytes) # PR ```
1 parent 058b511 commit d54a455

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

base/array.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,13 +991,13 @@ __safe_setindex!(A::Vector{T}, x, i::Int) where {T} = (@inline;
991991
function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int})
992992
@_propagate_inbounds_meta
993993
@boundscheck setindex_shape_check(X, length(I))
994+
@boundscheck checkbounds(A, I)
994995
require_one_based_indexing(X)
995996
X′ = unalias(A, X)
996997
I′ = unalias(A, I)
997998
count = 1
998999
for i in I′
999-
@inbounds x = X′[count]
1000-
A[i] = x
1000+
@inbounds A[i] = X′[count]
10011001
count += 1
10021002
end
10031003
return A

0 commit comments

Comments
 (0)