Skip to content

Improved performance of Broadcasting. #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,20 @@ end
end

@inline function Base.copyto!(dest::ArrayPartition,
bc::Broadcast.Broadcasted{ArrayPartitionStyle{Style}}) where {
Style,
}
bc::Broadcast.Broadcasted{ArrayPartitionStyle{Style}}) where {Style}
N = npartitions(dest, bc)
@inline function f(i)
copyto!(dest.x[i], unpack(bc, i))
# If dest is all the same underlying array type, use for-loop
if all(x isa typeof(first(dest.x)) for x in dest.x)
@inbounds for i in 1:N
copyto!(dest.x[i], unpack(bc, i))
end
else
# Fall back to original implementation for complex broadcasts
@inline function f(i)
copyto!(dest.x[i], unpack(bc, i))
end
ntuple(f, Val(N))
end
ntuple(f, Val(N))
dest
end

Expand Down Expand Up @@ -411,8 +417,8 @@ end
i) where {Style <: Broadcast.DefaultArrayStyle}
Broadcast.Broadcasted{Style}(bc.f, unpack_args(i, bc.args))
end
unpack(x, ::Any) = x
unpack(x::ArrayPartition, i) = x.x[i]
@inline unpack(x, ::Any) = x
@inline unpack(x::ArrayPartition, i) = x.x[i]

@inline function unpack_args(i, args::Tuple)
(unpack(args[1], i), unpack_args(i, Base.tail(args))...)
Expand Down
5 changes: 2 additions & 3 deletions src/named_array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ end
@inline function Base.copyto!(dest::NamedArrayPartition,
bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{NamedArrayPartition}})
N = npartitions(dest, bc)
@inline function f(i)
copyto!(ArrayPartition(dest).x[i], unpack(bc, i))
@inbounds for i in 1:N
copyto!(dest.x[i], unpack(bc, i))
end
ntuple(f, Val(N))
return dest
end

Expand Down