From 7963dbe65698046e0f15f9ad9defe5de8ba379b8 Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Fri, 16 Oct 2026 03:52:58 +0000 Subject: [PATCH] Precompile `show`, `\`, products, broadcast and concatenation of sparse Float64 arrays Co-Authored-By: Claude Opus 5.5 --- src/SparseArrays.jl | 2 ++ src/precompile.jl | 24 ++++++++++++++++++++++++ test/solvers/threads.jl | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/precompile.jl diff --git a/src/SparseArrays.jl b/src/SparseArrays.jl index 5c8ce1dd..859a87b5 100644 --- a/src/SparseArrays.jl +++ b/src/SparseArrays.jl @@ -476,4 +476,6 @@ zero(a::AbstractSparseArray{Tv,Ti}) where {Tv,Ti} = spzeros(Tv, Ti, size(a)...) LinearAlgebra.diagzero(D::Diagonal{<:AbstractSparseMatrix{T}},i,j) where {T} = spzeros(T, size(D.diag[i], 1), size(D.diag[j], 2)) +include("precompile.jl") + end diff --git a/src/precompile.jl b/src/precompile.jl new file mode 100644 index 00000000..4e18e66e --- /dev/null +++ b/src/precompile.jl @@ -0,0 +1,24 @@ +# This file is a part of Julia. License is MIT: https://julialang.org/license + +# Runs the common operations on Float64 arrays while the package image is built, so that their +# native code is cached; `show` and the `\` polyalgorithm otherwise take seconds on first call. +if Base.generating_output() + let + A = sparse([1, 2, 3, 1, 3], [1, 2, 3, 3, 1], [4.0, 5.0, 6.0, 1.0, 1.0]) + x = [1.0, 2.0, 3.0] + v = sparsevec([1, 3], [1.0, 2.0], 3) + A[1, 2]; A[1:2, 2:3]; A[:, 2]; A[2, 2] = 5.0 + A * x; A * A; A' * x; A * v; A + A; A - A; 2 * A + A .* 2; A .+ A; A .* A + copy(A'); sum(A); findnz(A); dropzeros(A); diag(A); kron(A, A) + hcat(A, A); vcat(A, A); [A A; A A] + v + v; dot(v, v) + sprint(show, MIME"text/plain"(), A); sprint(show, A) + sprint(show, MIME"text/plain"(), v); sprint(show, v) + @static if Base.USE_GPL_LIBS + A \ x; lu(A) \ x; qr(A) \ x + H = Hermitian(A' * A + 10 * LinearAlgebra.I) + cholesky(H) \ x; ldlt(H) \ x + end + end +end diff --git a/test/solvers/threads.jl b/test/solvers/threads.jl index 0adc73f3..897ac07c 100644 --- a/test/solvers/threads.jl +++ b/test/solvers/threads.jl @@ -41,6 +41,24 @@ end end end +@static if COMPREHENSIVE +@testset "the package image holds native code for `show` and `\\`" begin + script = """ + using LinearAlgebra + function cached(f, types) + mi = Base.method_instance(f, types) + return mi !== nothing && isdefined(mi, :cache) && mi.cache.invoke != C_NULL + end + S = SparseMatrixCSC{Float64,Int} + if Base.JLOptions().use_pkgimages != 0 + @test cached(show, (typeof(IOBuffer()), MIME"text/plain", S)) + @test cached(\\, (S, Vector{Float64})) + end + """ + @test success(pipeline(testprocess(script); stdout, stderr)) +end +end + @testset "SuiteSparse library directory override (#250)" begin L = SparseArrays.LibSuiteSparse original_dir = L.libdir()