Skip to content

Commit 5db3da5

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`. We don't overloading `Size` and `similar_type` at present. as they are only used for `broadcast`. With this, we can move much less code to `StaticArraysCore`. The only downside is that SizedArray would be allocated twice. That's not idea, but we can't do any better if we don't depend on StaticArray or copy a lot of code from there.
1 parent b99776e commit 5db3da5

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/interface.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ function createinstance(::Type{T}, args...) where {T}
4949
isconcretetype(T) ? bypass_constructor(T, args) : T(args...)
5050
end
5151

52-
createinstance(::Type{T}, args...) where {T<:Tup} = T(args)
52+
createinstance(::Type{T}, args...) where {T<:Tup} = T(args)
53+
54+
createinstance(::Type{T}) where {T} = (x...) -> createinstance(T, x...)

src/staticarrays_support.jl

Lines changed: 48 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,50 @@ 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, similar_type
33+
StructStaticArrayStyle{N} = StructArrayStyle{StaticArrayStyle{N}, N}
34+
function Broadcast.instantiate(bc::Broadcasted{StructStaticArrayStyle{M}}) where {M}
35+
bc′ = Broadcast.instantiate(replace_structarray(bc))
36+
return convert(Broadcasted{StructStaticArrayStyle{M}}, bc′)
37+
end
38+
# This looks costy, but compiler should be able to optimize them away
39+
Broadcast._axes(bc::Broadcasted{<:StructStaticArrayStyle}, ::Nothing) = axes(replace_structarray(bc))
40+
41+
to_staticstyle(@nospecialize(x::Type)) = x
42+
to_staticstyle(::Type{StructStaticArrayStyle{N}}) where {N} = StaticArrayStyle{N}
43+
function replace_structarray(bc::Broadcasted{Style}) where {Style}
44+
args = replace_structarray_args(bc.args)
45+
return Broadcasted{to_staticstyle(Style)}(bc.f, args, nothing)
46+
end
47+
function replace_structarray(A::StructArray)
48+
f = createinstance(eltype(A))
49+
args = Tuple(components(A))
50+
return Broadcasted{StaticArrayStyle{ndims(A)}}(f, args, nothing)
51+
end
52+
replace_structarray(@nospecialize(A)) = A
53+
54+
replace_structarray_args(args::Tuple) = (replace_structarray(args[1]), replace_structarray_args(Base.tail(args))...)
55+
replace_structarray_args(::Tuple{}) = ()
56+
57+
# StaticArrayStyle has no similar defined.
58+
# Overload `Base.copy` instead.
59+
@inline function Base.copy(bc::Broadcasted{StructStaticArrayStyle{M}}) where {M}
60+
sa = copy(convert(Broadcasted{StaticArrayStyle{M}}, bc))
61+
ET = eltype(sa)
62+
isnonemptystructtype(ET) || return sa
63+
elements = Tuple(sa)
64+
arrs = ntuple(Val(fieldcount(ET))) do i
65+
similar_type(sa, fieldtype(ET, i))(_getfields(elements, i))
66+
end
67+
return StructArray{ET}(arrs)
68+
end
69+
70+
@inline function _getfields(x::Tuple, i::Int)
71+
if @generated
72+
return Expr(:tuple, (:(getfield(x[$j], i)) for j in 1:fieldcount(x))...)
73+
else
74+
return map(Base.Fix2(getfield, i), x)
75+
end
76+
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)