Running numbat with cargo flamegraph, I found that a significant portion of the startup time was spent in the DefineFunction branch of elaborate_statement in the typechecker just cloning TypeChecker (line 1313 of numbat/src/typechecker/mod.rs). I think this is only due to the need to save and restore the environment, value_namespace and type_namespace members of TypeChecker after we have finished checking the function definition to remove any local names before continuing.
To avoid this, I experimented with replacing the HashMaps in Environment and NameSpace with a stack of HashMaps (a MapStack for lack of a better name) that can be used to save the current state of the map (when we start checking a definition) and restore it (when we are done) but otherwise behaves like a hash map. This seems to give good results to reduce startup time with little impact on the typechecker's code itself. Running numbat on the files in the example/ directory with hyperfine, I observe a ~20-25% reduction in time. I've attached the results as Markdown files for master and this branch as well as the output of cargo flamegraph for master.
Running
numbat
withcargo flamegraph
, I found that a significant portion of the startup time was spent in theDefineFunction
branch ofelaborate_statement
in the typechecker just cloningTypeChecker
(line 1313 ofnumbat/src/typechecker/mod.rs
). I think this is only due to the need to save and restore theenvironment
,value_namespace
andtype_namespace
members ofTypeChecker
after we have finished checking the function definition to remove any local names before continuing.To avoid this, I experimented with replacing the
HashMap
s inEnvironment
andNameSpace
with a stack ofHashMap
s (aMapStack
for lack of a better name) that can be used to save the current state of the map (when we start checking a definition) and restore it (when we are done) but otherwise behaves like a hash map. This seems to give good results to reduce startup time with little impact on the typechecker's code itself. Runningnumbat
on the files in theexample/
directory withhyperfine
, I observe a ~20-25% reduction in time. I've attached the results as Markdown files formaster
and this branch as well as the output ofcargo flamegraph
for master.bench-master.md bench-avoid-cloning-the-typechecker.md
flamegraph.svg