From ee583c3d75d6e72493c8546783de433b23440fa4 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 11 Apr 2023 11:56:32 -0400 Subject: [PATCH] improve cat design / performance This used to make a lot of references to design issues with the SparseArrays package (https://github.com/JuliaLang/julia/issues/2326 / https://github.com/JuliaLang/julia/pull/20815), which result in a non-sensical dispatch arrangement, and contribute to a slow loading experience do to the nonsense Unions that must be checked by subtyping. --- base/abstractarray.jl | 8 ++---- base/array.jl | 12 --------- .../md5 | 1 - .../sha512 | 1 - .../md5 | 1 + .../sha512 | 1 + stdlib/LinearAlgebra/src/special.jl | 26 ++++--------------- stdlib/LinearAlgebra/src/uniformscaling.jl | 13 +++++----- stdlib/SparseArrays.version | 4 +-- 9 files changed, 17 insertions(+), 50 deletions(-) delete mode 100644 deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/md5 delete mode 100644 deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/sha512 create mode 100644 deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/md5 create mode 100644 deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/sha512 diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 1417987847ec4..58da11c232635 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1984,16 +1984,14 @@ julia> cat(1, [2], [3;;]; dims=Val(2)) # The specializations for 1 and 2 inputs are important # especially when running with --inline=no, see #11158 -# The specializations for Union{AbstractVecOrMat,Number} are necessary -# to have more specialized methods here than in LinearAlgebra/uniformscaling.jl vcat(A::AbstractArray) = cat(A; dims=Val(1)) vcat(A::AbstractArray, B::AbstractArray) = cat(A, B; dims=Val(1)) vcat(A::AbstractArray...) = cat(A...; dims=Val(1)) -vcat(A::Union{AbstractVecOrMat,Number}...) = cat(A...; dims=Val(1)) +vcat(A::Union{AbstractArray,Number}...) = cat(A...; dims=Val(1)) hcat(A::AbstractArray) = cat(A; dims=Val(2)) hcat(A::AbstractArray, B::AbstractArray) = cat(A, B; dims=Val(2)) hcat(A::AbstractArray...) = cat(A...; dims=Val(2)) -hcat(A::Union{AbstractVecOrMat,Number}...) = cat(A...; dims=Val(2)) +hcat(A::Union{AbstractArray,Number}...) = cat(A...; dims=Val(2)) typed_vcat(T::Type, A::AbstractArray) = _cat_t(Val(1), T, A) typed_vcat(T::Type, A::AbstractArray, B::AbstractArray) = _cat_t(Val(1), T, A, B) @@ -2143,8 +2141,6 @@ end hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = typed_hvcat(promote_typeof(xs...), rows, xs...) hvcat(rows::Tuple{Vararg{Int}}, xs...) = typed_hvcat(promote_eltypeof(xs...), rows, xs...) -# the following method is needed to provide a more specific one compared to LinearAlgebra/uniformscaling.jl -hvcat(rows::Tuple{Vararg{Int}}, xs::Union{AbstractVecOrMat,Number}...) = typed_hvcat(promote_eltypeof(xs...), rows, xs...) function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T nr = length(rows) diff --git a/base/array.jl b/base/array.jl index b99ec7ee2b015..d3d4750743a91 100644 --- a/base/array.jl +++ b/base/array.jl @@ -2041,18 +2041,6 @@ function vcat(arrays::Vector{T}...) where T end vcat(A::Vector...) = cat(A...; dims=Val(1)) # more special than SparseArrays's vcat -# disambiguation with LinAlg/special.jl -# Union{Number,Vector,Matrix} is for LinearAlgebra._DenseConcatGroup -# VecOrMat{T} is for LinearAlgebra._TypedDenseConcatGroup -hcat(A::Union{Number,Vector,Matrix}...) = cat(A...; dims=Val(2)) -hcat(A::VecOrMat{T}...) where {T} = typed_hcat(T, A...) -vcat(A::Union{Number,Vector,Matrix}...) = cat(A...; dims=Val(1)) -vcat(A::VecOrMat{T}...) where {T} = typed_vcat(T, A...) -hvcat(rows::Tuple{Vararg{Int}}, xs::Union{Number,Vector,Matrix}...) = - typed_hvcat(promote_eltypeof(xs...), rows, xs...) -hvcat(rows::Tuple{Vararg{Int}}, xs::VecOrMat{T}...) where {T} = - typed_hvcat(T, rows, xs...) - _cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(Returns(1), n-1)..., length(x))) ## find ## diff --git a/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/md5 b/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/md5 deleted file mode 100644 index 21131a800d4af..0000000000000 --- a/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/md5 +++ /dev/null @@ -1 +0,0 @@ -0369c2679e8b834009526dd4f402cfd8 diff --git a/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/sha512 b/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/sha512 deleted file mode 100644 index df3b4cc3736e1..0000000000000 --- a/deps/checksums/SparseArrays-2c7f4d6d839e9a97027454a037bfa004c1eb34b0.tar.gz/sha512 +++ /dev/null @@ -1 +0,0 @@ -b2573398a8e8f5bcd8195cc3aa68103b2b5ea66c1835d4bc4d808fb1e0d3fbd46cc2caa04a4f49813a4a4a7f88d2f95789fa080d1d940c0291b09416be0fd1e6 diff --git a/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/md5 b/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/md5 new file mode 100644 index 0000000000000..4fe3b1a92398e --- /dev/null +++ b/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/md5 @@ -0,0 +1 @@ +ecdc624b32458735f7a123f62d9a611a diff --git a/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/sha512 b/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/sha512 new file mode 100644 index 0000000000000..774543ebf6910 --- /dev/null +++ b/deps/checksums/SparseArrays-c93589c73f86ecead456b0eab292e861afd9b2e5.tar.gz/sha512 @@ -0,0 +1 @@ +62fd1c7e34ce0c3fa4e60b311f9a0379f59f231f641514c832b0b80ffac88c96d3792ef394076caea260a2120c001d7779c4a940ee86692360d28b08ecaf3e5b diff --git a/stdlib/LinearAlgebra/src/special.jl b/stdlib/LinearAlgebra/src/special.jl index 22567c2a8ef96..885f29fa1417b 100644 --- a/stdlib/LinearAlgebra/src/special.jl +++ b/stdlib/LinearAlgebra/src/special.jl @@ -330,27 +330,11 @@ end ==(A::Bidiagonal, B::SymTridiagonal) = iszero(_evview(B)) && iszero(A.ev) && A.dv == B.dv ==(B::SymTridiagonal, A::Bidiagonal) = A == B -# concatenation -const _SpecialArrays = Union{Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal} -const _Symmetric_DenseArrays{T,A<:Matrix} = Symmetric{T,A} -const _Hermitian_DenseArrays{T,A<:Matrix} = Hermitian{T,A} -const _Triangular_DenseArrays{T,A<:Matrix} = UpperOrLowerTriangular{T,A} -const _Annotated_DenseArrays = Union{_SpecialArrays, _Triangular_DenseArrays, _Symmetric_DenseArrays, _Hermitian_DenseArrays} -const _Annotated_Typed_DenseArrays{T} = Union{_Triangular_DenseArrays{T}, _Symmetric_DenseArrays{T}, _Hermitian_DenseArrays{T}} -const _DenseConcatGroup = Union{Number, Vector, Adjoint{<:Any,<:Vector}, Transpose{<:Any,<:Vector}, Matrix, _Annotated_DenseArrays} -const _TypedDenseConcatGroup{T} = Union{Vector{T}, Adjoint{T,Vector{T}}, Transpose{T,Vector{T}}, Matrix{T}, _Annotated_Typed_DenseArrays{T}} - -promote_to_array_type(::Tuple{Vararg{Union{_DenseConcatGroup,UniformScaling}}}) = Matrix - -Base._cat(dims, xs::_DenseConcatGroup...) = Base._cat_t(dims, promote_eltype(xs...), xs...) -vcat(A::_DenseConcatGroup...) = Base.typed_vcat(promote_eltype(A...), A...) -hcat(A::_DenseConcatGroup...) = Base.typed_hcat(promote_eltype(A...), A...) -hvcat(rows::Tuple{Vararg{Int}}, xs::_DenseConcatGroup...) = Base.typed_hvcat(promote_eltype(xs...), rows, xs...) -# For performance, specially handle the case where the matrices/vectors have homogeneous eltype -Base._cat(dims, xs::_TypedDenseConcatGroup{T}...) where {T} = Base._cat_t(dims, T, xs...) -vcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_vcat(T, A...) -hcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hcat(T, A...) -hvcat(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hvcat(T, rows, xs...) +# TODO: remove these deprecations (used by SparseArrays in the past) +const _DenseConcatGroup = Union{} +const _SpecialArrays = Union{} + +promote_to_array_type(::Tuple) = Matrix # factorizations function cholesky(S::RealHermSymComplexHerm{<:Real,<:SymTridiagonal}, ::NoPivot = NoPivot(); check::Bool = true) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index 21ae8a1bb913a..e6167688c686c 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -417,17 +417,16 @@ promote_to_arrays(n,k, ::Type{T}, A, B, C) where {T} = (promote_to_arrays_(n[k], T, A), promote_to_arrays_(n[k+1], T, B), promote_to_arrays_(n[k+2], T, C)) promote_to_arrays(n,k, ::Type{T}, A, B, Cs...) where {T} = (promote_to_arrays_(n[k], T, A), promote_to_arrays_(n[k+1], T, B), promote_to_arrays(n,k+2, T, Cs...)...) -promote_to_array_type(A::Tuple{Vararg{Union{AbstractVecOrMat,UniformScaling,Number}}}) = Matrix _us2number(A) = A _us2number(J::UniformScaling) = J.λ for (f, _f, dim, name) in ((:hcat, :_hcat, 1, "rows"), (:vcat, :_vcat, 2, "cols")) @eval begin - @inline $f(A::Union{AbstractVecOrMat,UniformScaling}...) = $_f(A...) + @inline $f(A::Union{AbstractArray,UniformScaling}...) = $_f(A...) # if there's a Number present, J::UniformScaling must be 1x1-dimensional - @inline $f(A::Union{AbstractVecOrMat,UniformScaling,Number}...) = $f(map(_us2number, A)...) - function $_f(A::Union{AbstractVecOrMat,UniformScaling,Number}...; array_type = promote_to_array_type(A)) + @inline $f(A::Union{AbstractArray,UniformScaling,Number}...) = $f(map(_us2number, A)...) + function $_f(A::Union{AbstractArray,UniformScaling,Number}...; array_type = promote_to_array_type(A)) n = -1 for a in A if !isa(a, UniformScaling) @@ -445,9 +444,9 @@ for (f, _f, dim, name) in ((:hcat, :_hcat, 1, "rows"), (:vcat, :_vcat, 2, "cols" end end -hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractVecOrMat,UniformScaling}...) = _hvcat(rows, A...) -hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractVecOrMat,UniformScaling,Number}...) = _hvcat(rows, A...) -function _hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractVecOrMat,UniformScaling,Number}...; array_type = promote_to_array_type(A)) +hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractArray,UniformScaling}...) = _hvcat(rows, A...) +hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractArray,UniformScaling,Number}...) = _hvcat(rows, A...) +function _hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractArray,UniformScaling,Number}...; array_type = promote_to_array_type(A)) require_one_based_indexing(A...) nr = length(rows) sum(rows) == length(A) || throw(ArgumentError("mismatch between row sizes and number of arguments")) diff --git a/stdlib/SparseArrays.version b/stdlib/SparseArrays.version index b29ce9b0306e1..7fd1d71a6843b 100644 --- a/stdlib/SparseArrays.version +++ b/stdlib/SparseArrays.version @@ -1,4 +1,4 @@ -SPARSEARRAYS_BRANCH = main -SPARSEARRAYS_SHA1 = 2c7f4d6d839e9a97027454a037bfa004c1eb34b0 +SPARSEARRAYS_BRANCH = master +SPARSEARRAYS_SHA1 = c93589c73f86ecead456b0eab292e861afd9b2e5 SPARSEARRAYS_GIT_URL := https://github.com/JuliaSparse/SparseArrays.jl.git SPARSEARRAYS_TAR_URL = https://api.github.com/repos/JuliaSparse/SparseArrays.jl/tarball/$1