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..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 @@ -646,7 +641,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..876b7828 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)) """ @@ -210,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 bba00f62..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 @@ -830,6 +836,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 +1325,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