From 5a9b252335ec9434287ea6ba4f63e9c8b02d6190 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 10:51:42 -0500 Subject: [PATCH] Build Tuple, NamedTuple and StaticArrays rows without splatting Splatting more than 32 values into `createinstance` allocates. For row types whose `createinstance` belongs to StructArrays or StaticArrays, build the row from the tuple of field values directly. Applies to scalar `getindex`, `pop!` and `popfirst!`. Co-authored-by: Kristoffer Carlsson --- ext/StaticArraysExt.jl | 3 ++- src/interface.jl | 5 +++++ src/structarray.jl | 4 ++-- test/runtests.jl | 11 +++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ext/StaticArraysExt.jl b/ext/StaticArraysExt.jl index 52a947fd..cf9696f2 100644 --- a/ext/StaticArraysExt.jl +++ b/ext/StaticArraysExt.jl @@ -1,7 +1,7 @@ module StaticArraysExt using StructArrays -using StaticArrays: StaticArray, FieldArray, tuple_prod, SVector, MVector, SOneTo, HeterogeneousShapeTuple +using StaticArrays: StaticArray, FieldArray, tuple_prod, SVector, MVector, SOneTo, HeterogeneousShapeTuple, SArray, MArray, SizedArray """ StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T} @@ -22,6 +22,7 @@ which subtypes `FieldArray`. end end StructArrays.createinstance(::Type{T}, args...) where {T<:StaticArray} = T(args) +StructArrays._createinstance(::Type{T}, row) where {T<:Union{SArray, MArray, SizedArray}} = T(row) StructArrays.component(s::StaticArray, i::Integer) = getindex(s, i) function StructArrays.component(s::StructArray{<:Union{SVector,MVector}}, key::Symbol) diff --git a/src/interface.jl b/src/interface.jl index 51dee9ee..f2cb46c3 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -59,6 +59,11 @@ end createinstance(::Type{T}, args...) where {T<:Tup} = T(args) +# Build a `T` from a tuple of field values. Splatting more than 32 values allocates, so types whose +# `createinstance` belongs to this package (no user can overload it) are built from the tuple directly. +_createinstance(::Type{T}, row) where {T} = createinstance(T, row...) +_createinstance(::Type{T}, row) where {T<:Tup} = T(row) + struct Instantiator{T} end Instantiator(::Type{T}) where {T} = Instantiator{T}() diff --git a/src/structarray.jl b/src/structarray.jl index 40422686..2f047a3c 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -349,7 +349,7 @@ Base.@propagate_inbounds Base.getindex(x::StructArray, I...) = _getindex(x, to_i Base.@propagate_inbounds function _getindex(x::StructArray{T}, I::Vararg{Int}) where {T} cols = components(x) @boundscheck checkbounds(x, I...) - return createinstance(T, get_ith(cols, I...)...) + return _createinstance(T, get_ith(cols, I...)) end @inline function _getindex(s::StructArray{T}, I...) where {T} @@ -408,7 +408,7 @@ end for f in (:pop!, :popfirst!) @eval function Base.$f(s::StructVector{T}) where T t = _mapcols($f, components(s)) - return createinstance(T, t...) + return _createinstance(T, t) end end diff --git a/test/runtests.jl b/test/runtests.jl index 8e58ade1..bba00f62 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -798,6 +798,10 @@ end # Base.map loses inference on tuples with 32 or more elements, # so componentwise operations must not rely on it widecols(n, het) = ntuple(i -> het && iseven(i) ? [1.0, 2.0] : [1, 2], n) + # rows are stored rather than returned + allocs(f::F, args...) where {F} = (f(args...); @allocated f(args...)) + storerow!(out, t, i) = (out[1] = t[i]; nothing) + storepop!(out, f::F, t) where {F} = (out[1] = f(t); nothing) @testset "$n columns, $(het ? "mixed" : "same") types, $(named ? "NamedTuple" : "Tuple")" for n in (32, 64), het in (false, true), named in (false, true) cols = named ? NamedTuple{ntuple(i -> Symbol(:x, i), n)}(widecols(n, het)) : widecols(n, het) t = StructArray(cols) @@ -822,10 +826,17 @@ end @test (@inferred StructArrays.replace_storage(identity, t)) == t @test (@inferred StructArrays.colmetadata(t)) == map(_ -> nothing, Tables.columns(t)) @test (@inferred reshape(t, (SOneTo(2),))) == t + out = Vector{eltype(t)}(undef, 1) + @test allocs(storerow!, out, t, 1) == 0 + @test allocs(storepop!, out, pop!, copy(t)) == 0 + @test allocs(storepop!, out, popfirst!, copy(t)) == 0 end s = StructArray{Wide64}(widecols(64, true)) @test (@inferred constructorof(typeof(s))(components(s))) == s + + sv = StructArray{SVector{64, Float64}}(ntuple(_ -> [1.0, 2.0], 64)) + @test allocs(storerow!, Vector{eltype(sv)}(undef, 1), sv, 1) == 0 end @testset "propertynames" begin