From 5c20d67710dc6402f41a8beb350ac40c8debc28f Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 11:27:28 -0500 Subject: [PATCH 1/3] Short-circuit over components without recursion or allocations Add `_allcols` and `_anycols`, unrolled `&&`/`||` chains built with `Base.Cartesian.@nall`/`@nany`. Unlike `all`/`any` and recursion over `tail`, they stay inferrable and allocation-free for any number of components, including NamedTuples, and keep early stopping. Use them in `roweq` (grouping), `issparse` and `broadcast_unalias`. Co-authored-by: Kristoffer Carlsson --- ext/SparseArraysExt.jl | 2 +- src/sort.jl | 5 ++--- src/structarray.jl | 2 +- src/utils.jl | 6 ++++++ test/runtests.jl | 17 +++++++++++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/ext/SparseArraysExt.jl b/ext/SparseArraysExt.jl index fda77f62..36c0e0be 100644 --- a/ext/SparseArraysExt.jl +++ b/ext/SparseArraysExt.jl @@ -9,6 +9,6 @@ function sparse(S::StructArray{T}) where {T} return createinstance.(T, sparse_components...) end -issparse(S::StructArray) = all(issparse, components(S)) +issparse(S::StructArray) = StructArrays._allcols(issparse, components(S)) end diff --git a/src/sort.jl b/src/sort.jl index 64316d91..c7f2120e 100644 --- a/src/sort.jl +++ b/src/sort.jl @@ -35,9 +35,8 @@ Base.eltype(::Type{<:GroupPerm}) = UnitRange{Int} return eq end -roweq(t::Tuple{}, i, j) = true -roweq(t::Tuple, i, j) = roweq(t[1], i, j) ? roweq(tail(t), i, j) : false -roweq(s::StructArray, i, j) = roweq(Tuple(components(s)), i, j) +roweq(t::Tup, i, j) = _allcols(c -> roweq(c, i, j), t) +roweq(s::StructArray, i, j) = roweq(components(s), i, j) function uniquesorted(keys, perm=sortperm(keys)) (keys[perm[idxs[1]]] for idxs in GroupPerm(keys, perm)) diff --git a/src/structarray.jl b/src/structarray.jl index 2f047a3c..e6d0bb96 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -646,7 +646,7 @@ end # for aliasing analysis during broadcast function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray) - if dest === src || any(Base.Fix2(===, src), values(components(dest))) # values() works around Julia 1.13 allocation regression + if dest === src || _anycols(Base.Fix2(===, src), components(dest)) return src else return Base.unalias(dest, src) diff --git a/src/utils.jl b/src/utils.jl index d402004b..4211cfb9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -41,6 +41,12 @@ Unlike `map`, stays inferrable for any number of elements: `map` gives up on tup @inline @generated _mapcols(f::F, cols::Tup) where {F} = :($(strip_params(cols))(Base.Cartesian.@ntuple $(fieldcount(cols)) i -> f(cols[i]))) +# Short-circuiting `all(f, cols)` and `any(f, cols)` that stay allocation-free for any number of elements, including NamedTuples +@inline @generated _allcols(f::F, cols::Tup) where {F} = + :(Base.Cartesian.@nall $(fieldcount(cols)) i -> f(cols[i])) +@inline @generated _anycols(f::F, cols::Tup) where {F} = + :(Base.Cartesian.@nany $(fieldcount(cols)) i -> f(cols[i])) + buildfromschema(initializer::F, ::Type{T}) where {F, T} = buildfromschema(initializer, T, staticschema(T)) """ diff --git a/test/runtests.jl b/test/runtests.jl index bba00f62..3091bc5f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -830,6 +830,16 @@ end @test allocs(storerow!, out, t, 1) == 0 @test allocs(storepop!, out, pop!, copy(t)) == 0 @test allocs(storepop!, out, popfirst!, copy(t)) == 0 + # equal rows, so that every column is compared + @test StructArrays.roweq(t, 1, 1) + @test !StructArrays.roweq(t, 1, 2) + @test allocs(StructArrays.roweq, t, 1, 1) == 0 + # the last component, so that every component is checked + @test allocs(Base.Broadcast.broadcast_unalias, t, last(components(t))) == 0 + # sparse components, so that `issparse` is called on every one + sp = StructArray(map(sparse, components(t))) + @test issparse(sp) + @test allocs(issparse, sp) == 0 end s = StructArray{Wide64}(widecols(64, true)) @@ -1309,6 +1319,13 @@ end @test sp isa SparseMatrixCSC{eltype(sa)} @test sp == sa end + @testset "issparse" begin + s = StructArray(a = sparse([1, 0]), b = sparse([0, 1])) + @test issparse(s) + @test !issparse(StructArray(a = sparse([1, 0]), b = [0, 1])) + allocs(f::F, args...) where {F} = (f(args...); @allocated f(args...)) + @test allocs(issparse, s) == 0 + end end @testset "metadata" begin From 16d1a405d3e7460e94749ba4cd7f18502500b8ac Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 11:51:07 -0500 Subject: [PATCH 2/3] Short-circuit findconsistentvalue Co-authored-by: Kristoffer Carlsson --- src/utils.jl | 3 +-- test/runtests.jl | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 4211cfb9..876b7828 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -216,6 +216,5 @@ If not all values are equal, return `nothing`. Otherwise, return the unique valu """ function findconsistentvalue(f::F, cols::Tup) where F val = f(first(cols)) - isconsistent = all(_mapcols(isequal(val) ∘ f, cols)) - return ifelse(isconsistent, val, nothing) + return _allcols(c -> isequal(val, f(c)), cols) ? val : nothing end diff --git a/test/runtests.jl b/test/runtests.jl index 3091bc5f..8f9e884a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -127,6 +127,12 @@ end cols = (a=rand(2), b=rand(2), c=rand(3)) @test isnothing(StructArrays.findconsistentvalue(length, cols)) @test isnothing(StructArrays.findconsistentvalue(length, Tuple(cols))) + + # stops at the first inconsistent component + ncalls = Ref(0) + countlength(c) = (ncalls[] += 1; length(c)) + @test isnothing(StructArrays.findconsistentvalue(countlength, ([1], [1, 2], [1], [1]))) + @test ncalls[] == 3 end @testset "indexstyle" begin From eb923059d641fad1a8a76b7fb5a88401c3777113 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 12:00:25 -0500 Subject: [PATCH 3/3] Use _anycols in index_type Replaces the recursion over components. The special case for homogeneous component tuples (only check the first component) was added in #194 because the recursion was not inferred beyond a few elements; without recursion it is no longer needed, and mixed-type wide tuples now compile faster too. Co-authored-by: Kristoffer Carlsson --- src/structarray.jl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/structarray.jl b/src/structarray.jl index e6d0bb96..0e0944ad 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -22,15 +22,10 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N} end end -# compute optimal type to use for indexing as a function of components -index_type(components::NamedTuple) = index_type(values(components)) -index_type(::Tuple{}) = Int -function index_type(components::Tuple) - f, ls = first(components), tail(components) - return IndexStyle(f) isa IndexCartesian ? CartesianIndex{ndims(f)} : index_type(ls) -end -# Only check first component if the all the component types match -index_type(components::NTuple) = invoke(index_type, Tuple{Tuple}, (first(components),)) +# compute optimal type to use for indexing as a function of components, +# which all have the same `ndims` (guaranteed by the inner constructor) +index_type(components::Tup) = + _anycols(c -> IndexStyle(c) isa IndexCartesian, components) ? CartesianIndex{ndims(first(components))} : Int # Return the index type parameter as a function of the StructArray type or instance index_type(s::StructArray) = index_type(typeof(s)) index_type(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = I