Skip to content

Commit 4743976

Browse files
committed
Avoid overloading Size and similar_type.
We might want them in the future. But at present, they are only used for `broadcast`. With this, we can move much less code to `StaticArraysCore`
1 parent d8e302f commit 4743976

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/interface.jl

+3-1
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

+20-12
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,30 @@ StructArrays.component(s::FieldArray, i) = invoke(StructArrays.component, Tuple{
2929
StructArrays.createinstance(T::Type{<:FieldArray}, args...) = invoke(createinstance, Tuple{Type{<:Any}, Vararg}, T, args...)
3030

3131
# Broadcast overload
32-
using StaticArraysCore: StaticArrayStyle
33-
import StaticArraysCore: Size, is_staticarray_like, similar_type
32+
using StaticArraysCore: StaticArrayStyle, similar_type
3433
StructStaticArrayStyle{N} = StructArrayStyle{StaticArrayStyle{N}, N}
3534
function Broadcast.instantiate(bc::Broadcasted{StructStaticArrayStyle{M}}) where {M}
36-
bc′ = Broadcast.instantiate(convert(Broadcasted{StaticArrayStyle{M}}, bc))
35+
bc′ = Broadcast.instantiate(replace_structarray(bc))
3736
return convert(Broadcasted{StructStaticArrayStyle{M}}, bc′)
3837
end
39-
function Broadcast._axes(bc::Broadcasted{StructStaticArrayStyle{M}}, ::Nothing) where {M}
40-
return Broadcast._axes(convert(Broadcasted{StaticArrayStyle{M}}, bc), nothing)
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)
4151
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{}) = ()
4256

4357
# StaticArrayStyle has no similar defined.
4458
# Overload `Base.copy` instead.
@@ -48,7 +62,7 @@ end
4862
isnonemptystructtype(ET) || return sa
4963
elements = Tuple(sa)
5064
arrs = ntuple(Val(fieldcount(ET))) do i
51-
similar_type(sa, fieldtype(ET, i), Size(sa))(_getfields(elements, i))
65+
similar_type(sa, fieldtype(ET, i))(_getfields(elements, i))
5266
end
5367
return StructArray{ET}(arrs)
5468
end
@@ -60,9 +74,3 @@ end
6074
return map(Base.Fix2(getfield, i), x)
6175
end
6276
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

0 commit comments

Comments
 (0)