From 1401566374b8f23fa575c43350cfef901d0b0ce4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 25 Mar 2020 13:58:30 -0400 Subject: [PATCH 1/4] Inline and use a non-splatting combine_axes in broadcast DifferentialEquations.jl has been carrying around a patch https://github.com/SciML/DiffEqBase.jl/blob/master/src/diffeqfastbc.jl#L18 for awhile which stops allocations like https://discourse.julialang.org/t/strange-allocations-during-broadcasting/26605/5 due to lack of inlining and due to splatting. It would probably be good to upstream this? --- base/broadcast.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index e68253c03ec24..140f1db180125 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -471,7 +471,8 @@ julia> Broadcast.combine_axes(1, 1, 1) ``` """ @inline combine_axes(A, B...) = broadcast_shape(axes(A), combine_axes(B...)) -combine_axes(A) = axes(A) +@inline combine_axes(A, B) = broadcast_shape(broadcast_axes(A), broadcast_axes(B)) +@inline combine_axes(A) = axes(A) # shape (i.e., tuple-of-indices) inputs broadcast_shape(shape::Tuple) = shape From fd879c683ad79094a6f935b826007fe739f8b18b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 25 Mar 2020 14:12:28 -0400 Subject: [PATCH 2/4] Update base/broadcast.jl Co-Authored-By: Kristoffer Carlsson --- base/broadcast.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index 140f1db180125..26168e3199743 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -471,7 +471,7 @@ julia> Broadcast.combine_axes(1, 1, 1) ``` """ @inline combine_axes(A, B...) = broadcast_shape(axes(A), combine_axes(B...)) -@inline combine_axes(A, B) = broadcast_shape(broadcast_axes(A), broadcast_axes(B)) +@inline combine_axes(A, B) = broadcast_shape(axes(A), axes(B)) @inline combine_axes(A) = axes(A) # shape (i.e., tuple-of-indices) inputs From ea07017ffb4d4bdae1b5f3226e0eee8b0fbb3958 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 25 Mar 2020 14:17:13 -0400 Subject: [PATCH 3/4] Update broadcast.jl --- base/broadcast.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index 26168e3199743..4c6178b5fb84d 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -472,7 +472,7 @@ julia> Broadcast.combine_axes(1, 1, 1) """ @inline combine_axes(A, B...) = broadcast_shape(axes(A), combine_axes(B...)) @inline combine_axes(A, B) = broadcast_shape(axes(A), axes(B)) -@inline combine_axes(A) = axes(A) +combine_axes(A) = axes(A) # shape (i.e., tuple-of-indices) inputs broadcast_shape(shape::Tuple) = shape From 3e110e542e9706d4c542637f7efbdaf2ea701994 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 25 Mar 2020 14:25:51 -0400 Subject: [PATCH 4/4] test to ensure no allocations --- test/broadcast.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/broadcast.jl b/test/broadcast.jl index 5edc623222dbf..acaafbe20d0ae 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -857,3 +857,13 @@ end # 28680 @test 1 .+ 1 .+ (1, 2) == (3, 4) + +# PR #35260 no allocations in simple broadcasts +u = rand(100) +k1 = similar(u) +k2 = similar(u) +k3 = similar(u) +k4 = similar(u) +f(a,b,c,d,e) = @. a = a + 1*(b+c+d+e) +@allocated f(u,k1,k2,k3,k4) +@test (@allocated f(u,k1,k2,k3,k4)) == 0