From e60b7ce8bdc0ff9d8b5f70ae249b131c21abf002 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 23 Nov 2023 09:38:42 -0500 Subject: [PATCH] Avoid defensive copy for matching `Array{T}` when broadcasting `Base.mightalias` considers two arrays to be aliased iff they have the same base pointer in memory, which is exactly the case where it is safe to not make a defensive copy due to the traversal mentioned above --- base/broadcast.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/base/broadcast.jl b/base/broadcast.jl index d6e5513889cee..d1e221d2c7124 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -945,6 +945,11 @@ end broadcast_unalias(dest, src) = dest === src ? src : unalias(dest, src) broadcast_unalias(::Nothing, src) = src +# `Base.mightalias` considers two arrays to be aliased iff they have the same +# base pointer in memory, which is exactly the case where it is safe to not +# make a defensive copy due to the traversal mentioned above. +broadcast_unalias(dest::Array{T}, src::Array{T}) where T = src + # Preprocessing a `Broadcasted` does two things: # * unaliases any arguments from `dest` # * "extrudes" the arguments where it is advantageous to pre-compute the broadcasted indices