JuliaArrays / JuliaArrays/StructArrays.jl

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

Open
#262 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
402
Forks
49
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository files or tests; begin by reproducing the Julia example with StructVector and running @code_typed on first_prod. Investigate how the declared StructVector field type and component access produce the shown Any inferences; done means establishing a type-stable declaration or documenting why one is not available.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.