@@ -811,6 +811,14 @@ function observed_dependency_graph(eqs::Vector{Equation})
811
811
return DiCMOBiGraph {false} (graph, matching)
812
812
end
813
813
814
+ abstract type ObservedGraphCacheKey end
815
+
816
+ struct ObservedGraphCache
817
+ graph:: DiCMOBiGraph {false , Int, BipartiteGraph{Int, Nothing},
818
+ Matching{Unassigned, Vector{Union{Unassigned, Int}}}}
819
+ obsvar_to_idx:: Dict{Any, Int}
820
+ end
821
+
814
822
"""
815
823
$(TYPEDSIGNATURES)
816
824
@@ -831,8 +839,19 @@ Keyword arguments:
831
839
"""
832
840
function observed_equations_used_by (sys:: AbstractSystem , exprs;
833
841
involved_vars = vars (exprs; op = Union{Shift, Differential, Initial}), obs = observed (sys), available_vars = [])
834
- obsvars = getproperty .(obs, :lhs )
835
- graph = observed_dependency_graph (obs)
842
+ if iscomplete (sys) && obs == observed (sys)
843
+ cache = getmetadata (sys, MutableCacheKey, nothing )
844
+ obs_graph_cache = get! (cache, ObservedGraphCacheKey) do
845
+ obsvar_to_idx = Dict {Any, Int} ([eq. lhs => i for (i, eq) in enumerate (obs)])
846
+ graph = observed_dependency_graph (obs)
847
+ return ObservedGraphCache (graph, obsvar_to_idx)
848
+ end
849
+ @unpack obsvar_to_idx, graph = obs_graph_cache
850
+ else
851
+ obsvar_to_idx = Dict ([eq. lhs => i for (i, eq) in enumerate (obs)])
852
+ graph = observed_dependency_graph (obs)
853
+ end
854
+
836
855
if ! (available_vars isa Set)
837
856
available_vars = Set (available_vars)
838
857
end
@@ -841,7 +860,9 @@ function observed_equations_used_by(sys::AbstractSystem, exprs;
841
860
for sym in involved_vars
842
861
sym in available_vars && continue
843
862
arrsym = iscall (sym) && operation (sym) === getindex ? arguments (sym)[1 ] : nothing
844
- idx = findfirst (v -> isequal (v, sym) || isequal (v, arrsym), obsvars)
863
+ idx = @something (get (obsvar_to_idx, sym, nothing ),
864
+ get (obsvar_to_idx, arrsym, nothing ),
865
+ Some (nothing ))
845
866
idx === nothing && continue
846
867
idx in obsidxs && continue
847
868
parents = dfs_parents (graph, idx)
0 commit comments