From a19168a9d1836bf6fb0a4c4b2f43e73c21af0a61 Mon Sep 17 00:00:00 2001 From: Daniel Thom Date: Wed, 30 Oct 2024 15:55:42 -0600 Subject: [PATCH] Cache modules for faster deserialization --- src/utils/utils.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/utils/utils.jl b/src/utils/utils.jl index e82d8a83..ddd11ba9 100644 --- a/src/utils/utils.jl +++ b/src/utils/utils.jl @@ -425,14 +425,25 @@ function get_initial_timestamp(data::TimeSeries.TimeArray) return TimeSeries.timestamp(data)[1] end -function get_module(module_name) +# Looking up modules with Base.root_module is slow; cache them. +const g_cached_modules = Dict{String, Module}() + +function get_module(module_name::AbstractString) + cached_module = get(g_cached_modules, module_name, nothing) + if !isnothing(cached_module) + return cached_module + end + # root_module cannot find InfrastructureSystems if it hasn't been installed by the # user (but has been installed as a dependency to another package). - return if module_name == "InfrastructureSystems" + mod = if module_name == "InfrastructureSystems" InfrastructureSystems else Base.root_module(Base.__toplevel__, Symbol(module_name)) end + + g_cached_modules[module_name] = mod + return mod end get_type_from_strings(module_name, type) =