Skip to content
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

Slight refactor to collection widening #30076

Merged
merged 7 commits into from
Dec 1, 2018
Merged
Changes from 3 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
48 changes: 36 additions & 12 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,20 @@ function collect_to_with_first!(dest, v1, itr, st)
return grow_to!(dest, itr, st)
end

function setindex_widen!(dest::Array{T}, el, i) where T
bramtayl marked this conversation as resolved.
Show resolved Hide resolved
@_inline_meta
if el isa T || typeof(el) === T
bramtayl marked this conversation as resolved.
Show resolved Hide resolved
@inbounds dest[i] = el::T
dest
bramtayl marked this conversation as resolved.
Show resolved Hide resolved
else
R = promote_typejoin(T, typeof(el))
new = similar(dest, R)
copyto!(new,1, dest,1, i-1)
bramtayl marked this conversation as resolved.
Show resolved Hide resolved
@inbounds new[i] = el
new
end
end

function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
# collect to dest array, checking the type of each result. if a result does not
# match, widen the result type and re-dispatch.
Expand All @@ -668,10 +682,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
@inbounds dest[i] = el::T
i += 1
else
R = promote_typejoin(T, typeof(el))
new = similar(dest, R)
copyto!(new,1, dest,1, i-1)
@inbounds new[i] = el
new = setindex_widen!(dest, el, i)
bramtayl marked this conversation as resolved.
Show resolved Hide resolved
return collect_to!(new, itr, i+1, st)
end
end
Expand All @@ -686,6 +697,26 @@ function grow_to!(dest, itr)
grow_to!(dest2, itr, y[2])
end

function push_widen!(dest, el)
@_inline_meta
T = eltype(dest)
S = typeof(el)
if S === T || S <: T
push!(dest, el::T)
dest
else
new = sizehint!(empty(dest, promote_typejoin(T, S)), length(dest))
if new isa AbstractSet
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
union!(new, dest)
else
append!(new, dest)
end
push!(new, el)
new
end
end

function grow_to!(dest, itr, st)
T = eltype(dest)
y = iterate(itr, st)
Expand All @@ -695,14 +726,7 @@ function grow_to!(dest, itr, st)
if S === T || S <: T
push!(dest, el::T)
else
new = sizehint!(empty(dest, promote_typejoin(T, S)), length(dest))
if new isa AbstractSet
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
union!(new, dest)
else
append!(new, dest)
end
push!(new, el)
new = push_widen!(dest, el)
return grow_to!(new, itr, st)
end
y = iterate(itr, st)
Expand Down