[m-rev.] diff: Cache indirect imports.

Peter Wang novalazy at gmail.com
Fri Nov 25 15:36:44 AEDT 2022


This change reduces the average run time for a do-nothing build of
Prince on my machine from 3.1 s to 1.95 s, for a speedup of ~37%.

compiler/make.make_info.m:
    Add a field to make_info to hold the result of indirect_imports.

compiler/make.dependencies.m:
    Cache the result of indirect_imports in make_info.

diff --git a/compiler/make.dependencies.m b/compiler/make.dependencies.m
index 200b029ff..907f8e309 100644
--- a/compiler/make.dependencies.m
+++ b/compiler/make.dependencies.m
@@ -151,6 +151,9 @@
 :- type cached_direct_imports.
 :- func init_cached_direct_imports = cached_direct_imports.
 
+:- type cached_indirect_imports.
+:- func init_cached_indirect_imports = cached_indirect_imports.
+
 :- type cached_transitive_foreign_imports.
 :- func init_cached_transitive_foreign_imports =
     cached_transitive_foreign_imports.
@@ -607,8 +610,18 @@ non_intermod_direct_imports_2(Globals, ModuleIndex, Succeeded, Modules,
     make_info::in, make_info::out, io::di, io::uo) is det.
 
 indirect_imports(Globals, ModuleIndex, Succeeded, Modules, !Info, !IO) :-
-    indirect_imports_2(Globals, direct_imports, ModuleIndex,
-        Succeeded, Modules, !Info, !IO).
+    CachedIndirectImports0 = !.Info ^ mki_cached_indirect_imports,
+    ( if map.search(CachedIndirectImports0, ModuleIndex, CachedResult) then
+        CachedResult = deps_result(Succeeded, Modules)
+    else
+        indirect_imports_2(Globals, direct_imports, ModuleIndex,
+            Succeeded, Modules, !Info, !IO),
+        Result = deps_result(Succeeded, Modules),
+        CachedIndirectImports1 = !.Info ^ mki_cached_indirect_imports,
+        map.det_insert(ModuleIndex, Result,
+            CachedIndirectImports1, CachedIndirectImports),
+        !Info ^ mki_cached_indirect_imports := CachedIndirectImports
+    ).
 
     % Return the list of modules for which we should read `.int2' files,
     % ignoring those which need to be read as a result of importing modules
@@ -1370,6 +1383,10 @@ make_write_dependency_file_and_timestamp_list([Head | Tail], !IO) :-
 
 init_cached_direct_imports = map.init.
 
+:- type cached_indirect_imports == map(module_index, module_deps_result).
+
+init_cached_indirect_imports = map.init.
+
 :- type cached_transitive_foreign_imports
     == map(module_index, module_deps_result).
 
diff --git a/compiler/make.make_info.m b/compiler/make.make_info.m
index 433e8163e..8ae0b1378 100644
--- a/compiler/make.make_info.m
+++ b/compiler/make.make_info.m
@@ -91,6 +91,9 @@
                 mki_cached_non_intermod_direct_imports
                                         :: cached_direct_imports,
 
+                mki_cached_indirect_imports
+                                        :: cached_indirect_imports,
+
                 % The boolean is `yes' if the result is complete.
                 % XXX Use a better representation for the sets.
                 mki_cached_transitive_dependencies
diff --git a/compiler/make.top_level.m b/compiler/make.top_level.m
index 8dcdc77a5..ec6079b91 100644
--- a/compiler/make.top_level.m
+++ b/compiler/make.top_level.m
@@ -131,6 +131,7 @@ make_process_compiler_args(Globals, DetectedGradeFlags, Variables, OptionArgs,
             DepStatusMap,
             init_cached_direct_imports,
             init_cached_direct_imports,
+            init_cached_indirect_imports,
             init_cached_transitive_dependencies,
             init_cached_transitive_foreign_imports,
             ShouldRebuildModuleDeps,
-- 
2.38.0



More information about the reviews mailing list