Skip to content

Commit

Permalink
Merge pull request #31174 from cstjean/faster-broadcast-widening
Browse files Browse the repository at this point in the history
Make broadcast eltype widening type-stable
  • Loading branch information
mbauman authored Feb 27, 2019
2 parents 87b6b00 + a86ca21 commit 6b0c5c2
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 6b0c5c2

Please sign in to comment.