From 9fde56985ae91765d87f644bfe60bcb102007609 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Mon, 4 Oct 2021 15:44:44 -0300 Subject: [PATCH 1/2] Fix broadcasting allocating --- base/broadcast.jl | 4 ++-- test/broadcast.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index d5daab992390c..971727768a4be 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -941,8 +941,8 @@ broadcast_unalias(::Nothing, src) = src preprocess(dest, x) = extrude(broadcast_unalias(dest, x)) @inline preprocess_args(dest, args::Tuple) = (preprocess(dest, args[1]), preprocess_args(dest, tail(args))...) -preprocess_args(dest, args::Tuple{Any}) = (preprocess(dest, args[1]),) -preprocess_args(dest, args::Tuple{}) = () +@inline preprocess_args(dest, args::Tuple{Any}) = (preprocess(dest, args[1]),) +@inline preprocess_args(dest, args::Tuple{}) = () # Specialize this method if all you want to do is specialize on typeof(dest) @inline function copyto!(dest::AbstractArray, bc::Broadcasted{Nothing}) diff --git a/test/broadcast.jl b/test/broadcast.jl index 38950dd10b5b0..4e7220abcba02 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -1058,3 +1058,13 @@ end @test println.(buf, [1,2,3]) == [nothing, nothing, nothing] @test String(take!(buf)) == "1\n2\n3\n" end + +@testset "Memory allocation inconsistency in broadcasting #41565" begin + function test(y) + y .= 0 .- y ./ (y.^2) # extra allocation + return y + end + arr = rand(1000) + @allocated test(arr) + @test (@allocated test(arr)) == 0 +end \ No newline at end of file From 3086ecb5b6dcf0aa47d23d50cb0f37445b8a244e Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Mon, 4 Oct 2021 15:47:15 -0300 Subject: [PATCH 2/2] add newline --- test/broadcast.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/broadcast.jl b/test/broadcast.jl index 4e7220abcba02..1cad14de44c5f 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -1067,4 +1067,4 @@ end arr = rand(1000) @allocated test(arr) @test (@allocated test(arr)) == 0 -end \ No newline at end of file +end