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 27, 2019
1 parent d2cd3f4 commit a86ca21
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,15 @@ end
@noinline throwdm(axdest, axsrc) =
throw(DimensionMismatch("destination axes $axdest are not compatible with source axes $axsrc"))

function restart_copyto_nonleaf!(newdest, dest, bc, val, I, iter, state, count)
# Function barrier that makes the copying to newdest type stable
for II in Iterators.take(iter, count)
newdest[II] = dest[II]
end
newdest[I] = val
return copyto_nonleaf!(newdest, bc, iter, state, count+1)
end

function copyto_nonleaf!(dest, bc::Broadcasted, iter, state, count)
T = eltype(dest)
while true
Expand All @@ -976,11 +985,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
newdest[I] = val
return copyto_nonleaf!(newdest, bc, iter, state, count+1)
return restart_copyto_nonleaf!(newdest, dest, bc, val, I, iter, state, count)
end
count += 1
end
Expand Down

0 comments on commit a86ca21

Please sign in to comment.