Skip to content

Commit

Permalink
Introduce a function barrier in Broadcast.copyto_nonleaf! to improve …
Browse files Browse the repository at this point in the history
…type stability

during type widening
  • Loading branch information
cstjean committed Feb 25, 2019
1 parent d2cd3f4 commit 412b356
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,13 @@ end
@noinline throwdm(axdest, axsrc) =
throw(DimensionMismatch("destination axes $axdest are not compatible with source axes $axsrc"))

function copyupto!(newdest, dest, iter)
# Function barrier that makes the copying to newdest type stable
for II in iter
newdest[II] = dest[II]
end
end

function copyto_nonleaf!(dest, bc::Broadcasted, iter, state, count)
T = eltype(dest)
while true
Expand All @@ -976,9 +983,7 @@ function copyto_nonleaf!(dest, bc::Broadcasted, iter, state, count)
# This element type doesn't fit in dest. Allocate a new dest with wider eltype,
# copy over old values, and continue
newdest = Base.similar(dest, promote_typejoin(T, typeof(val)))
for II in Iterators.take(iter, count)
newdest[II] = dest[II]
end
copyupto!(newdest, dest, Iterators.take(iter, count))
newdest[I] = val
return copyto_nonleaf!(newdest, bc, iter, state, count+1)
end
Expand Down

0 comments on commit 412b356

Please sign in to comment.