Skip to content

Avoid inlining setindex!(::Array, ::Any, ::AbstractVector) #21401

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,30 +537,44 @@ setindex!{T}(A::Array{T}, x, i1::Int) = arrayset(A, convert(T,x)::T, i1)
setindex!{T}(A::Array{T}, x, i1::Int, i2::Int, I::Int...) = (@_inline_meta; arrayset(A, convert(T,x)::T, i1, i2, I...)) # TODO: REMOVE FOR #14770

# These are redundant with the abstract fallbacks but needed for bootstrap
function setindex!(A::Array, x, I::AbstractVector{Int})
@_propagate_inbounds_meta
A === I && (I = copy(I))
for i in I
A[i] = x
for (f, inbounds, inbounds_pop) in
((:_unsafe_setindex!, :($(Expr(:inbounds, true))), :($(Expr(:inbounds, :pop)))),
(:_safe_setindex!, :nothing, :nothing))
@eval begin
function $f(A::Array, x, I::AbstractVector{Int})
$inbounds
A === I && (I = copy(I))
for i in I
A[i] = x
end
return A
$inbounds_pop
end
function $f(A::Array, X::AbstractArray, I::AbstractVector{Int})
$inbounds
@boundscheck setindex_shape_check(X, length(I))
count = 1
if X === A
X = copy(X)
I===A && (I = X::typeof(I))
elseif I === A
I = copy(I)
end
for i in I
@inbounds x = X[count]
A[i] = x
count += 1
end
return A
$inbounds_pop
end
end
return A
end
function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int})
@_propagate_inbounds_meta
@boundscheck setindex_shape_check(X, length(I))
count = 1
if X === A
X = copy(X)
I===A && (I = X::typeof(I))
elseif I === A
I = copy(I)
end
for i in I
@inbounds x = X[count]
A[i] = x
count += 1
end
return A
# Wrapper needed to propagate @inbounds without necessarily inlining full definition
function setindex!(A::Array, X, I::AbstractVector{Int})
@_inline_meta
@boundscheck return _safe_setindex!(A, X, I)
return _unsafe_setindex!(A, X, I)
end

# Faster contiguous setindex! with copy!
Expand Down