From aaa9b3fb11e66a234b9300f66f7ff705fbbbbd95 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 09:51:41 -0500 Subject: [PATCH] Make StructArray dataids inferrable for many columns mapreduce over components stopped inferring and started allocating above 32 columns. --- src/structarray.jl | 4 +++- test/runtests.jl | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/structarray.jl b/src/structarray.jl index 2b07a517..e7d76963 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -653,7 +653,9 @@ function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray) end end -Base.dataids(u::StructArray) = mapreduce(Base.dataids, (a, b) -> (a..., b...), values(components(u)), init=()) +Base.dataids(u::StructArray) = dataids_cols(components(u)) +# generated to stay inferrable and non-allocating for any number of columns +@generated dataids_cols(cols::Tup) = :(Base.Cartesian.@ntuple $(fieldcount(cols)) i -> Base.dataids(cols[i])...) # Since all the components have the same axes, we choose the type of the first one to # define IteratorSize for a StructArray diff --git a/test/runtests.jl b/test/runtests.jl index bad13d78..e48287f0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1360,6 +1360,18 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS # used inside of broadcast but we also test it here explicitly @test isa(@inferred(Base.dataids(s)), NTuple{N, UInt} where {N}) + @test Base.dataids(s) == (UInt(pointer(s.re)), UInt(pointer(s.im))) + + # more columns than Base tuple functions handle inferrably + x, idx = rand(3), [3, 2, 1] + cols = (ntuple(_ -> rand(3), 31)..., 1:3, view(x, idx)) + ids = (map(c -> UInt(pointer(c)), cols[1:31])..., UInt(pointer(x)), UInt(pointer(idx))) + sw = StructArray(NamedTuple{ntuple(i -> Symbol(:c, i), 33)}(cols)) + @test @inferred(Base.dataids(sw)) == ids + @test @inferred(Base.dataids(StructArray(cols))) == ids + dataids_allocs(s) = @allocated Base.dataids(s) + dataids_allocs(sw) + @test dataids_allocs(sw) == 0 @testset "style conflict check" begin