Skip to content

Commit

Permalink
Cache modules for faster deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-thom committed Oct 30, 2024
1 parent ac2b29d commit a19168a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/utils/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down

0 comments on commit a19168a

Please sign in to comment.