I have a complicated nested NamedTuple, where profiling shows that getindex
on the NamedTuple is a significant source of slowness. @code_warntype
shows a type instability from getindex
on that NamedTuple in the slow call. Here is a tiny MWE that I think shows the same issue:
julia> nt = (a=[1,2],b=["a","b"])
(a = [1, 2], b = ["a", "b"])
julia> @code_warntype nt.b
MethodInstance for getproperty(::NamedTuple{(:a, :b), Tuple{Vector{Int64}, Vector{String}}}, ::Symbol)
from getproperty(x, f::Symbol) in Base at Base.jl:42
Arguments
#self#::Core.Const(getproperty)
x::NamedTuple{(:a, :b), Tuple{Vector{Int64}, Vector{String}}}
f::Symbol
Body::Union{Vector{Int64}, Vector{String}}
1 ─ nothing
│ %2 = Base.getfield(x, f)::Union{Vector{Int64}, Vector{String}}
└── return %2
Are there any common patterns to make getting fields from named tuples type stable?