From 412b3569922da5ec4ebb75794de20456b4fa16b4 Mon Sep 17 00:00:00 2001 From: cstjean Date: Mon, 25 Feb 2019 15:12:20 -0500 Subject: [PATCH] Introduce a function barrier in Broadcast.copyto_nonleaf! to improve type stability during type widening --- base/broadcast.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index 24d0424a897273..711012b4b7a6e8 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -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 @@ -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