Skip to content

Commit cc0a964

Browse files
committed
Convert to StaticArrayStyle
We first call broadcast from `StaticArrays` then split the output. This should has no extra runtime overhead. But some type info might missing because the eltype change. I think there's no better ways as we don't want to depend on the full `StaticArrays`.
1 parent b99776e commit cc0a964

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/staticarrays_support.jl

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import StaticArraysCore: StaticArray, FieldArray, tuple_prod
1+
using StaticArraysCore: StaticArray, FieldArray, tuple_prod
22

33
"""
44
StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
@@ -27,3 +27,42 @@ StructArrays.component(s::StaticArray, i) = getindex(s, i)
2727
end
2828
StructArrays.component(s::FieldArray, i) = invoke(StructArrays.component, Tuple{Any, Any}, s, i)
2929
StructArrays.createinstance(T::Type{<:FieldArray}, args...) = invoke(createinstance, Tuple{Type{<:Any}, Vararg}, T, args...)
30+
31+
# Broadcast overload
32+
using StaticArraysCore: StaticArrayStyle
33+
import StaticArraysCore: Size, is_staticarray_like, similar_type
34+
StructStaticArrayStyle{N} = StructArrayStyle{StaticArrayStyle{N}, N}
35+
function Broadcast.instantiate(bc::Broadcasted{StructStaticArrayStyle{M}}) where {M}
36+
bc′ = Broadcast.instantiate(convert(Broadcasted{StaticArrayStyle{M}}, bc))
37+
return convert(Broadcasted{StructStaticArrayStyle{M}}, bc′)
38+
end
39+
function Broadcast._axes(bc::Broadcasted{StructStaticArrayStyle{M}}, ::Nothing) where {M}
40+
return Broadcast._axes(convert(Broadcasted{StaticArrayStyle{M}}, bc), nothing)
41+
end
42+
43+
# StaticArrayStyle has no similar defined.
44+
# Overload `Base.copy` instead.
45+
@inline function Base.copy(bc::Broadcasted{StructStaticArrayStyle{M}}) where {M}
46+
sa = copy(convert(Broadcasted{StaticArrayStyle{M}}, bc))
47+
ET = eltype(sa)
48+
isnonemptystructtype(ET) || return sa
49+
elements = Tuple(sa)
50+
arrs = ntuple(Val(fieldcount(ET))) do i
51+
similar_type(sa, fieldtype(ET, i), Size(sa))(_getfields(elements, i))
52+
end
53+
return StructArray{ET}(arrs)
54+
end
55+
56+
@inline function _getfields(x::Tuple, i::Int)
57+
if @generated
58+
return Expr(:tuple, (:(getfield(x[$j], i)) for j in 1:fieldcount(x))...)
59+
else
60+
return map(Base.Fix2(getfield, i), x)
61+
end
62+
end
63+
64+
Size(::Type{SA}) where {SA<:StructArray} = Size(fieldtype(array_types(SA), 1))
65+
is_staticarray_like(x::StructArray) = any(is_staticarray_like, components(x))
66+
function similar_type(::Type{SA}, ::Type{T}, s::Size{S}) where {SA<:StructArray, T, S}
67+
return similar_type(fieldtype(array_types(SA), 1), T, s)
68+
end

test/runtests.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,12 +1160,24 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS
11601160
end
11611161
testset = Any[StructArray([1;2+im]),
11621162
1:2,
1163-
(1,2),
1163+
(1,2),
1164+
StructArray(@SArray [1 1+2im]),
1165+
(@SArray [1 2])
11641166
]
11651167
for aa in testset, bb in testset, cc in testset
11661168
_test(aa, bb, cc)
11671169
end
11681170
end
1171+
1172+
@testset "StructStaticArray" begin
1173+
bclog(s) = log.(s)
1174+
test_allocated(f, s) = @test (@allocated f(s)) == 0
1175+
a = @SMatrix [float(i) for i in 1:10, j in 1:10]
1176+
b = @SMatrix [0. for i in 1:10, j in 1:10]
1177+
s = StructArray{ComplexF64}((a , b))
1178+
@test (@inferred bclog(s)) isa typeof(s)
1179+
test_allocated(bclog, s)
1180+
end
11691181
end
11701182

11711183
@testset "map" begin

0 commit comments

Comments
 (0)