Skip to content

How should I declare StructArray field type in type-stable manner? #262

@sairus7

Description

@sairus7

There are a lot of Any inferred types in this example:

struct Foo
    x::StructVector{NamedTuple{(:a, :b), Tuple{Int64, Float64}}}
end

f = Foo(StructVector(a=[1,2,3], b=[0.1, 0.2, 0.3]))

function first_prod(f::Foo)
    f.x.a[1] * f.x.b[1]
end

@code_typed first_prod(f)

outputs:

CodeInfo(
1%1 = Base.getfield(f, :x)::StructVector{NamedTuple{(:a, :b), Tuple{Int64, Float64}}}%2 = StructArrays.getfield(%1, :components)::Union{Tuple, NamedTuple}%3 = StructArrays.getfield(%2, :a)::Any%4 = Base.getindex(%3, 1)::Any%5 = Base.getfield(f, :x)::StructVector{NamedTuple{(:a, :b), Tuple{Int64, Float64}}}%6 = StructArrays.getfield(%5, :components)::Union{Tuple, NamedTuple}%7 = StructArrays.getfield(%6, :b)::Any%8 = Base.getindex(%7, 1)::Any%9 = (%4 * %8)::Any
└──      return %9
) => Any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions