Skip to content

Commit

Permalink
abstractarray: fix append!(::AbstractVector, ...) interface
Browse files Browse the repository at this point in the history
#47154 mistakenly added `@_safeindex` macro on the
`_append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)` method,
although `@_safeindex` is only valid for builtin vectors i.e. `Vector`.

This commit adds `isa` check so that `@_safeindex` is only applied to
builtin vectors. The `isa` check should be removed at compile time, so
it should not affect the runtime performance.

closes #49748
  • Loading branch information
aviatesk committed May 11, 2023
1 parent 0b5ec1f commit caf552c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,8 @@ See [`sizehint!`](@ref) for notes about the performance model.
See also [`vcat`](@ref) for vectors, [`union!`](@ref) for sets,
and [`prepend!`](@ref) and [`pushfirst!`](@ref) for the opposite order.
"""
function append! end

function append!(a::Vector, items::AbstractVector)
itemindices = eachindex(items)
n = length(itemindices)
Expand All @@ -1180,18 +1182,21 @@ push!(a::AbstractVector, iter...) = append!(a, iter)

append!(a::AbstractVector, iter...) = foldl(append!, iter, init=a)

function _append!(a, ::Union{HasLength,HasShape}, iter)
function _append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)
@_terminates_locally_meta
n = length(a)
i = lastindex(a)
resize!(a, n+Int(length(iter))::Int)
@_safeindex for (i, item) in zip(i+1:lastindex(a), iter)
a[i] = item
for (i, item) in zip(i+1:lastindex(a), iter)
if isa(a, Vector) # give better effects for builtin vectors
@_safeindex a[i] = item
else
a[i] = item
end
end
a
end

function _append!(a, ::IteratorSize, iter)
function _append!(a::AbstractVector, ::IteratorSize, iter)
for item in iter
push!(a, item)
end
Expand Down Expand Up @@ -1246,7 +1251,7 @@ pushfirst!(a::Vector, iter...) = prepend!(a, iter)

prepend!(a::AbstractVector, iter...) = foldr((v, a) -> prepend!(a, v), iter, init=a)

function _prepend!(a, ::Union{HasLength,HasShape}, iter)
function _prepend!(a::Vector, ::Union{HasLength,HasShape}, iter)
@_terminates_locally_meta
require_one_based_indexing(a)
n = length(iter)
Expand All @@ -1257,7 +1262,7 @@ function _prepend!(a, ::Union{HasLength,HasShape}, iter)
end
a
end
function _prepend!(a, ::IteratorSize, iter)
function _prepend!(a::Vector, ::IteratorSize, iter)
n = 0
for item in iter
n += 1
Expand Down

0 comments on commit caf552c

Please sign in to comment.