Skip to content

Commit 1016aec

Browse files
committed
use more type stable cat_t implementation
After JuliaLang/julia#45028, it will be more recommended to use this more type stable version of `cat_t`.
1 parent f8ffbb7 commit 1016aec

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/sparsevector.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,10 +1078,11 @@ const _SparseConcatGroup = Union{_DenseConcatGroup, _SparseConcatArrays, _Annota
10781078
_makesparse(x::Number) = x
10791079
_makesparse(x::AbstractArray) = SparseMatrixCSC(issparse(x) ? x : sparse(x))
10801080

1081-
function Base._cat(dims, Xin::_SparseConcatGroup...)
1081+
# `@constprop :aggressive` allows `dims` to be propagated as constant improving return type inference
1082+
Base.@constprop :aggressive function Base._cat(dims, Xin::_SparseConcatGroup...)
10821083
X = map(_makesparse, Xin)
10831084
T = promote_eltype(Xin...)
1084-
Base.cat_t(T, X...; dims=dims)
1085+
return Base._cat_t(dims, T, X...)
10851086
end
10861087
function hcat(Xin::_SparseConcatGroup...)
10871088
X = map(_makesparse, Xin)

test/sparsevector.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ end
561561
@test issparse(hvcat((2,), othervecormat, spvec))
562562
@test issparse(cat(spvec, othervecormat; dims=(1,2)))
563563
@test issparse(cat(othervecormat, spvec; dims=(1,2)))
564+
565+
# inferrability (https://github.com/JuliaSparse/SparseArrays.jl/pull/92)
566+
cat_with_constdims(args...) = cat(args...; dims=(1,2))
567+
@test issparse(@inferred cat_with_constdims(spvec, othervecormat))
568+
@test issparse(@inferred cat_with_constdims(othervecormat, spvec))
564569
end
565570
# The preceding tests should cover multi-way combinations of those types, but for good
566571
# measure test a few multi-way combinations involving those types
@@ -572,6 +577,11 @@ end
572577
@test issparse(hvcat((5,), spvec, densemat, diagmat, densevec, spmat))
573578
@test issparse(cat(densemat, diagmat, spmat, densevec, spvec; dims=(1,2)))
574579
@test issparse(cat(spvec, diagmat, densevec, spmat, densemat; dims=(1,2)))
580+
581+
# inferrability (https://github.com/JuliaSparse/SparseArrays.jl/pull/92)
582+
cat_with_constdims(args...) = cat(args...; dims=(1,2))
583+
@test issparse(@inferred cat_with_constdims(densemat, diagmat, spmat, densevec, spvec))
584+
@test issparse(@inferred cat_with_constdims(spvec, diagmat, densevec, spmat, densemat))
575585
end
576586
@testset "vertical concatenation of SparseVectors with different el- and ind-type (#22225)" begin
577587
spv6464 = SparseVector(0, Int64[], Int64[])

0 commit comments

Comments
 (0)