Skip to content

Commit

Permalink
fix stack broadcast dims performance where possible (#767)
Browse files Browse the repository at this point in the history
* fix stack broadcast dims performance where possible

* method not needed

* manually throw error
  • Loading branch information
rafaqz authored Aug 17, 2024
1 parent 183d1d3 commit 75767d4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ end
function broadcast_dims(f, As::Union{AbstractDimStack,AbstractBasicDimArray}...)
st = _firststack(As...)
nts = _as_extended_nts(NamedTuple(st), As...)
layers = map(nts...) do as...
broadcast_dims(f, as...)
layers = map(keys(st)) do name
broadcast_dims(f, map(nt -> nt[name], nts)...)
end
rebuild_from_arrays(st, layers)
end
Expand Down Expand Up @@ -214,9 +214,14 @@ uniquekeys(nt::NamedTuple) = keys(nt)

_as_extended_nts(nt::NamedTuple{K}, A::AbstractDimArray, As...) where K =
(NamedTuple{K}(ntuple(x -> A, length(K))), _as_extended_nts(nt, As...)...)
function _as_extended_nts(nt::NamedTuple{K}, st::AbstractDimStack, As...) where K
function _as_extended_nts(nt::NamedTuple{K1}, st::AbstractDimStack{K2}, As...) where {K1,K2}
K1 == K2 || throw(ArgumentError("Keys of stack $K2 do not match the keys of the first stack $K1"))
extended_layers = map(layers(st)) do l
DimExtensionArray(l, dims(st))
if all(hasdim(l, dims(st)))
l
else
DimExtensionArray(l, dims(st))
end
end
return (extended_layers, _as_extended_nts(nt, As...)...)
end
Expand Down

0 comments on commit 75767d4

Please sign in to comment.