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..1cad14de44c5f 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