From 89b9dca2ed2f17b80e6139c8127edcdb1d864e02 Mon Sep 17 00:00:00 2001 From: Jash Ambaliya Date: Sun, 18 Oct 2026 13:11:03 +0530 Subject: [PATCH] perf: don't allocate in the inline linear SCC cache lookup --- lib/ModelingToolkitTearing/src/reassemble.jl | 32 ++++++++++++-------- lib/ModelingToolkitTearing/test/runtests.jl | 6 +++- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/ModelingToolkitTearing/src/reassemble.jl b/lib/ModelingToolkitTearing/src/reassemble.jl index 2ca279ea..eedd5e7b 100644 --- a/lib/ModelingToolkitTearing/src/reassemble.jl +++ b/lib/ModelingToolkitTearing/src/reassemble.jl @@ -755,17 +755,22 @@ function solve_into!(cache, A::AbstractMatrix, b::AbstractVector) end function get_inline_linsolve_cache(A::AbstractMatrix, b::AbstractVector) - tls = task_local_storage() - key = (INLINE_LINSOLVE_CACHE, typeof(A), size(A)) - cache = get(tls, key, nothing) - if cache === nothing - # `A` and `b` are views into the diffcache buffers of whichever problem happens - # to call first, and the cache keeps what it is handed, so it needs copies it owns. - # `copy` rather than `Matrix`/`Vector` so the cache keeps the array type it was - # given, which matters for anything living off the CPU. - cache = CommonSolve.init(LinearProblem(copy(A), copy(b))) - tls[key] = cache - end + # one typed `Dict` under a `Symbol` key, since a tuple key into task local storage + # (an `IdDict{Any, Any}`) would be boxed on every call + caches = get!(InlineLinsolveCachesT, task_local_storage(), + INLINE_LINSOLVE_CACHE)::InlineLinsolveCachesT + cache = get(caches, (typeof(A), size(A)), nothing) + return cache === nothing ? init_inline_linsolve_cache!(caches, A, b) : cache +end + +# out of line, since inlined it slows every inline solve by ~25 ns on Julia 1.13 +@noinline function init_inline_linsolve_cache!(caches, A::AbstractMatrix, b::AbstractVector) + # `A` and `b` are views into the diffcache buffers of whichever problem happens + # to call first, and the cache keeps what it is handed, so it needs copies it owns. + # `copy` rather than `Matrix`/`Vector` so the cache keeps the array type it was + # given, which matters for anything living off the CPU. + cache = CommonSolve.init(LinearProblem(copy(A), copy(b))) + caches[(typeof(A), size(A))] = cache return cache end @@ -787,10 +792,13 @@ end const INLINE_LINEAR_SCC_OP = safe_ldiv """ -Tag distinguishing this package's entries in task local storage. See [`numeric_ldiv!`](@ref). +Key of this package's entry in task local storage, which holds the `LinearCache`s of +[`numeric_ldiv!`](@ref) by array type and size. """ const INLINE_LINSOLVE_CACHE = :ModelingToolkitTearing_inline_linsolve_cache +const InlineLinsolveCachesT = Dict{Tuple{DataType, Dims{2}}, Any} + """ $TYPEDSIGNATURES diff --git a/lib/ModelingToolkitTearing/test/runtests.jl b/lib/ModelingToolkitTearing/test/runtests.jl index 6e488a33..dc8ed69a 100644 --- a/lib/ModelingToolkitTearing/test/runtests.jl +++ b/lib/ModelingToolkitTearing/test/runtests.jl @@ -166,7 +166,11 @@ end f!(du, prob.u0, prob.p, 0.0) # the solve used to build a fresh `LinearProblem` and `LinearCache` per call, which # cost ~1.4 kB and 23 allocations regardless of the size of the system - @test @allocated(f!(du, prob.u0, prob.p, 0.0)) < 512 + @test @allocated(f!(du, prob.u0, prob.p, 0.0)) < 256 + # and the cache lookup used to box its key on every call + A, b = rand(2, 2), rand(2) + MTKTearing.get_inline_linsolve_cache(A, b) + @test @allocated(MTKTearing.get_inline_linsolve_cache(A, b)) == 0 # the same system without inlining is the reference for the values @mtkcompile refsys = System(eqs, t)