From 7a408eb6228f451eec0c49d5f9986d9a5e281b64 Mon Sep 17 00:00:00 2001 From: bramtayl Date: Sun, 18 Nov 2018 14:17:57 -0500 Subject: [PATCH 1/7] Slight refactor to collection widening --- base/array.jl | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/base/array.jl b/base/array.jl index 9e8da6c384ba0..bd4d2cae517ae 100644 --- a/base/array.jl +++ b/base/array.jl @@ -656,6 +656,19 @@ 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 + if el isa T || typeof(el) === T + @inbounds dest[i] = el::T + dest + else + R = promote_typejoin(T, typeof(el)) + new = similar(dest, R) + copyto!(new,1, dest,1, i-1) + @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. @@ -668,10 +681,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) return collect_to!(new, itr, i+1, st) end end @@ -686,6 +696,25 @@ function grow_to!(dest, itr) grow_to!(dest2, itr, y[2]) end +function push_widen!(dest, el) + 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) @@ -695,14 +724,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) From 8ef4bb20973329a742b9a890b2c1a495ea7310cf Mon Sep 17 00:00:00 2001 From: bramtayl Date: Sun, 18 Nov 2018 16:30:16 -0500 Subject: [PATCH 2/7] Update array.jl --- base/array.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/array.jl b/base/array.jl index bd4d2cae517ae..f95b7099caead 100644 --- a/base/array.jl +++ b/base/array.jl @@ -656,7 +656,7 @@ 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 +@inline function setindex_widen!(dest::Array{T}, el, i) where T if el isa T || typeof(el) === T @inbounds dest[i] = el::T dest @@ -696,7 +696,7 @@ function grow_to!(dest, itr) grow_to!(dest2, itr, y[2]) end -function push_widen!(dest, el) +@inline function push_widen!(dest, el) T = eltype(dest) S = typeof(el) if S === T || S <: T From ec3ef78e5c3c689a2cbd013a37459f518730b64b Mon Sep 17 00:00:00 2001 From: bramtayl Date: Mon, 19 Nov 2018 10:33:09 -0500 Subject: [PATCH 3/7] Update array.jl --- base/array.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/array.jl b/base/array.jl index f95b7099caead..5400db8a06bbc 100644 --- a/base/array.jl +++ b/base/array.jl @@ -656,7 +656,8 @@ function collect_to_with_first!(dest, v1, itr, st) return grow_to!(dest, itr, st) end -@inline function setindex_widen!(dest::Array{T}, el, i) where T +function setindex_widen!(dest::Array{T}, el, i) where T + @_inline_meta if el isa T || typeof(el) === T @inbounds dest[i] = el::T dest @@ -696,7 +697,8 @@ function grow_to!(dest, itr) grow_to!(dest2, itr, y[2]) end -@inline function push_widen!(dest, el) +function push_widen!(dest, el) + @_inline_meta T = eltype(dest) S = typeof(el) if S === T || S <: T From 3371caf31f31705bef426feee1588d9a85d2ed47 Mon Sep 17 00:00:00 2001 From: bramtayl Date: Tue, 20 Nov 2018 08:09:56 -0500 Subject: [PATCH 4/7] added returns, removed redundant code --- base/array.jl | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/base/array.jl b/base/array.jl index 5400db8a06bbc..c10261e0b3a17 100644 --- a/base/array.jl +++ b/base/array.jl @@ -658,16 +658,11 @@ end function setindex_widen!(dest::Array{T}, el, i) where T @_inline_meta - if el isa T || typeof(el) === T - @inbounds dest[i] = el::T - dest - else - R = promote_typejoin(T, typeof(el)) - new = similar(dest, R) - copyto!(new,1, dest,1, i-1) - @inbounds new[i] = el - new - end + R = promote_typejoin(T, typeof(el)) + new = similar(dest, R) + copyto!(new,1, dest,1, i-1) + @inbounds new[i] = el + return new end function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T @@ -678,7 +673,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T y = iterate(itr, st) y === nothing && break el, st = y - if el isa T || typeof(el) === T + if el isa T @inbounds dest[i] = el::T i += 1 else @@ -701,20 +696,15 @@ function push_widen!(dest, el) @_inline_meta T = eltype(dest) S = typeof(el) - if S === T || S <: T - push!(dest, el::T) - dest + 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 - 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 + append!(new, dest) end + push!(new, el) + return new end function grow_to!(dest, itr, st) From a43a5363ab74ec78312958e73bf422343a7aa315 Mon Sep 17 00:00:00 2001 From: bramtayl Date: Wed, 21 Nov 2018 11:49:10 -0500 Subject: [PATCH 5/7] Added back || typeof(el) === T --- base/array.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/array.jl b/base/array.jl index c10261e0b3a17..c45d4fb294b12 100644 --- a/base/array.jl +++ b/base/array.jl @@ -673,7 +673,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T y = iterate(itr, st) y === nothing && break el, st = y - if el isa T + if el isa T || typeof(el) === T @inbounds dest[i] = el::T i += 1 else From 5ea225835ce99976e3c7099d4fa5d1516cec7578 Mon Sep 17 00:00:00 2001 From: bramtayl Date: Thu, 22 Nov 2018 17:39:40 -0500 Subject: [PATCH 6/7] Renaming, abstractarrays --- base/array.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/base/array.jl b/base/array.jl index c45d4fb294b12..482aba82d7203 100644 --- a/base/array.jl +++ b/base/array.jl @@ -656,11 +656,11 @@ 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 +function setindex_widen_up_to(dest::AbstractArray{T}, el, i) where T @_inline_meta R = promote_typejoin(T, typeof(el)) new = similar(dest, R) - copyto!(new,1, dest,1, i-1) + copyto!(new, firstindex(new), dest, firstindex(dest), i-1) @inbounds new[i] = el return new end @@ -677,7 +677,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T @inbounds dest[i] = el::T i += 1 else - new = setindex_widen!(dest, el, i) + new = setindex_widen_up_to(dest, el, i) return collect_to!(new, itr, i+1, st) end end @@ -692,7 +692,7 @@ function grow_to!(dest, itr) grow_to!(dest2, itr, y[2]) end -function push_widen!(dest, el) +function push_widen(dest, el) @_inline_meta T = eltype(dest) S = typeof(el) @@ -716,7 +716,7 @@ function grow_to!(dest, itr, st) if S === T || S <: T push!(dest, el::T) else - new = push_widen!(dest, el) + new = push_widen(dest, el) return grow_to!(new, itr, st) end y = iterate(itr, st) From cd447cf3ceb699e2b4ae5209b4e8bab26e36d057 Mon Sep 17 00:00:00 2001 From: bramtayl Date: Fri, 23 Nov 2018 14:35:56 -0500 Subject: [PATCH 7/7] Slight code simplifications --- base/array.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/base/array.jl b/base/array.jl index 482aba82d7203..f126c9984e520 100644 --- a/base/array.jl +++ b/base/array.jl @@ -658,8 +658,7 @@ end function setindex_widen_up_to(dest::AbstractArray{T}, el, i) where T @_inline_meta - R = promote_typejoin(T, typeof(el)) - new = similar(dest, R) + new = similar(dest, promote_typejoin(T, typeof(el))) copyto!(new, firstindex(new), dest, firstindex(dest), i-1) @inbounds new[i] = el return new @@ -694,9 +693,7 @@ end function push_widen(dest, el) @_inline_meta - T = eltype(dest) - S = typeof(el) - new = sizehint!(empty(dest, promote_typejoin(T, S)), length(dest)) + new = sizehint!(empty(dest, promote_typejoin(eltype(dest), typeof(el))), length(dest)) if new isa AbstractSet # TODO: merge back these two branches when copy! is re-enabled for sets/vectors union!(new, dest)