Skip to content

Commit

Permalink
Hoist boundscheck in vector setindex for arrays (#53149)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
jishnub authored Feb 2, 2024
1 parent 058b511 commit d54a455
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,13 @@ __safe_setindex!(A::Vector{T}, x, i::Int) where {T} = (@inline;
function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int})
@_propagate_inbounds_meta
@boundscheck setindex_shape_check(X, length(I))
@boundscheck checkbounds(A, I)
require_one_based_indexing(X)
X′ = unalias(A, X)
I′ = unalias(A, I)
count = 1
for i in I′
@inbounds x = X′[count]
A[i] = x
@inbounds A[i] = X′[count]
count += 1
end
return A
Expand Down

0 comments on commit d54a455

Please sign in to comment.