From bd48452193bf20a0056f60593d70499b43353e71 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 12 Feb 2026 11:49:46 +0530 Subject: [PATCH] feat: cache `unhack_system` --- .../src/ModelingToolkitTearing.jl | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl b/lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl index e3a9043..7a01c4e 100644 --- a/lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl +++ b/lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl @@ -54,7 +54,16 @@ abstract type ReassembleAlgorithm end include("reassemble.jl") +struct UnhackSystemCacheKey end + function MTKBase.unhack_system(sys::System) + cache = getmetadata(sys, MTKBase.MutableCacheKey, nothing) + if cache isa MTKBase.MutableCacheKey + cached_sys = get(cache, UnhackSystemCacheKey, nothing) + if cached_sys isa System + return cached_sys + end + end # Observed are copied by the masking operation obseqs = observed(sys) eqs = copy(equations(sys)) @@ -147,11 +156,16 @@ function MTKBase.unhack_system(sys::System) dvs = [unknowns(sys); additional_vars] - @set! sys.observed = obseqs - @set! sys.eqs = eqs - @set! sys.unknowns = dvs - @set! sys.schedule = sched - return sys + newsys = @set sys.observed = obseqs + @set! newsys.eqs = eqs + @set! newsys.unknowns = dvs + @set! newsys.schedule = sched + + if cache isa MTKBase.MutableCacheT + cache[UnhackSystemCacheKey] = newsys + end + + return newsys end function populate_inline_scc_map!(