Skip to content

Commit 2f075b5

Browse files
authored
Avoid repeated work in RemoveUnusedModuleElements::addReferences() [NFC] (#7407)
Globals that refer to globals lead to more work. This work cannot be infinite, as globals only refer to previous ones, but this can end up as exponential time, so this is actually important to optimize here. For exponential time, it is enough to have a chain of these: (global $global$N+1 (ref $A) (struct.new $A (global.get $global$N) (global.get $global$N) )) That is, two references from each global to its predecessor, causing us to double the work each time we scan back. Fixes #7405 (where the above pattern appears)
1 parent ca5d9db commit 2f075b5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/passes/RemoveUnusedModuleElements.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,14 @@ struct Analyzer {
544544
finder.walk(curr);
545545

546546
for (auto element : finder.elements) {
547-
referenced.insert(element);
547+
// Avoid repeated work. Note that globals with multiple references to
548+
// previous globals can lead to exponential work, so this is important.
549+
// (If C refers twice to B, and B refers twice to A, then when we process
550+
// C we would, naively, scan B twice and A four times.)
551+
auto [_, inserted] = referenced.insert(element);
552+
if (!inserted) {
553+
continue;
554+
}
548555

549556
auto& [kind, value] = element;
550557
if (kind == ModuleElementKind::Global) {

0 commit comments

Comments
 (0)