Skip to content

Commit 3fb1de7

Browse files
committed
Fix performance issue with Structure.is_merge_node
1 parent 47b8477 commit 3fb1de7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

compiler/lib/structure.ml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,20 @@ let is_forward g pc pc' = Hashtbl.find g.block_order pc < Hashtbl.find g.block_o
4242
let is_merge_node' block_order preds pc =
4343
let s = try Hashtbl.find preds pc with Not_found -> Addr.Set.empty in
4444
let o = Hashtbl.find block_order pc in
45-
let n =
46-
Addr.Set.fold (fun pc' n -> if Hashtbl.find block_order pc' < o then n + 1 else n) s 0
47-
in
48-
n > 1
45+
try
46+
ignore
47+
(Addr.Set.fold
48+
(fun pc' found_first ->
49+
if Hashtbl.find block_order pc' < o
50+
then
51+
if found_first
52+
then (* Exit early to avoid quadratic behavior *) raise Exit
53+
else true
54+
else found_first)
55+
s
56+
false);
57+
false
58+
with Exit -> true
4959

5060
let empty_body body =
5161
List.for_all

0 commit comments

Comments
 (0)