[m-rev.] diff: Optimise get_dependencies_from_graph.

Peter Wang novalazy at gmail.com
Wed Feb 1 11:54:26 AEDT 2023


This reduces the time to make dependencies in the compiler directory
from 5.85 seconds to 3.60 seconds on my machine.

compiler/write_deps_file.m:
    Use set.list_to_set instead of calling set.insert in a loop.

diff --git a/compiler/write_deps_file.m b/compiler/write_deps_file.m
index a56a05fbc..30885a5c0 100644
--- a/compiler/write_deps_file.m
+++ b/compiler/write_deps_file.m
@@ -1388,15 +1388,19 @@ generate_dependencies_write_d_file(Globals, Dep,
 :- pred get_dependencies_from_graph(deps_graph::in, module_name::in,
     set(module_name)::out) is det.
 
-get_dependencies_from_graph(DepsGraph0, ModuleName, Dependencies) :-
-    digraph.add_vertex(ModuleName, ModuleKey, DepsGraph0, DepsGraph),
+get_dependencies_from_graph(DepsGraph, ModuleName, Dependencies) :-
+    ( if digraph.search_key(DepsGraph, ModuleName, ModuleKey) then
         digraph.lookup_key_set_from(DepsGraph, ModuleKey, DepsKeysSet),
         AddKeyDep =
             ( pred(Key::in, Deps0::in, Deps::out) is det :-
                 digraph.lookup_vertex(DepsGraph, Key, Dep),
-            set.insert(Dep, Deps0, Deps)
+                Deps = [Dep | Deps0]
             ),
-    sparse_bitset.foldl(AddKeyDep, DepsKeysSet, set.init, Dependencies).
+        sparse_bitset.foldr(AddKeyDep, DepsKeysSet, [], DependenciesList),
+        set.list_to_set(DependenciesList, Dependencies)
+    else
+        set.init(Dependencies)
+    ).
 
 %---------------------------------------------------------------------------%
 
-- 
2.39.0



More information about the reviews mailing list