From c70baabd789f2c57229e3f61c6004e1f7f16ea7f Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 21 Aug 2019 13:48:05 +0200 Subject: [PATCH 1/2] fix and test that a ceratin broadcast pattern doesn't allocate --- base/broadcast.jl | 2 +- test/broadcast.jl | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index d1200d7c563b7..0afbe961aaad6 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -503,7 +503,7 @@ function check_broadcast_shape(shp, Ashp::Tuple) _bcsm(shp[1], Ashp[1]) || throw(DimensionMismatch("array could not be broadcast to match destination")) check_broadcast_shape(tail(shp), tail(Ashp)) end -check_broadcast_axes(shp, A) = check_broadcast_shape(shp, axes(A)) +@inline check_broadcast_axes(shp, A) = check_broadcast_shape(shp, axes(A)) # comparing many inputs @inline function check_broadcast_axes(shp, A, As...) check_broadcast_axes(shp, A) diff --git a/test/broadcast.jl b/test/broadcast.jl index a2289d684735f..089f68090994a 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -835,3 +835,10 @@ end # treat Pair as scalar: @test replace.(split("The quick brown fox jumps over the lazy dog"), r"[aeiou]"i => "_") == ["Th_", "q__ck", "br_wn", "f_x", "j_mps", "_v_r", "th_", "l_zy", "d_g"] + +# Test assigning into matrix doesn't allocate +perf_op_bcast!(R, x, y) = R .= 3 .* x .- 4 .* y.^2 .+ x .* y .- x .^ 3 +let x = rand(10^3), y = rand(10^3), R = Matrix{Float64}(undef, length(x), length(y)) + perf_op_bcast!(R, x, y) + @test @allocated(perf_op_bcast!(R, x, y)) == 0 +end \ No newline at end of file From c8ef66f9449577f1d89ea2ca3c18b28d655cb984 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 21 Aug 2019 15:09:48 +0200 Subject: [PATCH 2/2] remove the test --- test/broadcast.jl | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/broadcast.jl b/test/broadcast.jl index 089f68090994a..a2289d684735f 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -835,10 +835,3 @@ end # treat Pair as scalar: @test replace.(split("The quick brown fox jumps over the lazy dog"), r"[aeiou]"i => "_") == ["Th_", "q__ck", "br_wn", "f_x", "j_mps", "_v_r", "th_", "l_zy", "d_g"] - -# Test assigning into matrix doesn't allocate -perf_op_bcast!(R, x, y) = R .= 3 .* x .- 4 .* y.^2 .+ x .* y .- x .^ 3 -let x = rand(10^3), y = rand(10^3), R = Matrix{Float64}(undef, length(x), length(y)) - perf_op_bcast!(R, x, y) - @test @allocated(perf_op_bcast!(R, x, y)) == 0 -end \ No newline at end of file