From 2686b4dfe9cfc940de9bc4cb90874e6d82409147 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 1 Jul 2026 08:26:03 +0200 Subject: [PATCH] Memoize `split_indexed_var` and `get_stable_index` --- lib/ModelingToolkitBase/src/utils.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ModelingToolkitBase/src/utils.jl b/lib/ModelingToolkitBase/src/utils.jl index f159f04d77..7744d8ddf5 100644 --- a/lib/ModelingToolkitBase/src/utils.jl +++ b/lib/ModelingToolkitBase/src/utils.jl @@ -1549,7 +1549,11 @@ const DEFAULT_STABLE_INDEX = SU.StableIndex(Int[]) Given a symbolic variable `x`, check whether it is an indexed array symbolic. If it is, return the array and `true`. Otherwise, return `x, false`. """ -function split_indexed_var(x::SymbolicT) +SU.@cache limit = 500_000 function split_indexed_var(x::SymbolicT)::Tuple{SymbolicT, Bool} + return _split_indexed_var(x) +end + +function _split_indexed_var(x::SymbolicT) return Moshi.Match.@match x begin BSImpl.Term(; f, args) && if f === getindex end => (args[1], true) BSImpl.Term(; f, args) && if f isa Operator && length(args) == 1 end => begin @@ -1572,7 +1576,11 @@ end Given a symbolic variable `x`, assume `split_indexed_var(x)[2]` is `true`. Return the corresponding `SymbolicUtils.StableIndex`. """ -function get_stable_index(x::SymbolicT) +SU.@cache limit = 500_000 function get_stable_index(x::SymbolicT)::SU.StableIndex{Int} + return _get_stable_index(x) +end + +function _get_stable_index(x::SymbolicT) return Moshi.Match.@match x begin BSImpl.Term(; f, args) && if f === getindex end => return SU.StableIndex{Int}(x) BSImpl.Term(; f, args) && if f isa Operator end => return get_stable_index(args[1])