perf: avoid O(N^2) exiting-branch checks in CodeFolding#8599
Open
Changqing-JING wants to merge 1 commit intoWebAssembly:mainfrom
Open
perf: avoid O(N^2) exiting-branch checks in CodeFolding#8599Changqing-JING wants to merge 1 commit intoWebAssembly:mainfrom
Changqing-JING wants to merge 1 commit intoWebAssembly:mainfrom
Conversation
1dae3f3 to
66dff99
Compare
kripken
reviewed
Apr 14, 2026
| } | ||
| // Pre-populate the cache once at the top level so all subsequent | ||
| // exitingBranchCache_ lookups are O(1). | ||
| if (num == 0) { |
Member
There was a problem hiding this comment.
- We are called more than once with
num == 0, so I think this is doing more work than needed? (there are three calls to this, two withnum == 0as the default value) - We may also not end up needing the cache at all, if other issues stop us earlier.
- We will also only need the cache for some expressions, not the entire function.
To fix those issues, how about making new line 702 call a function that checks for external break targets. That function would lazily populate a cache internally, that is, given a specific expression it would compute it and cache results for that expression and all children (avoiding walking children already in the cache).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up PR of #8586 to optimize CodeFolding
optimizeTerminatingTailscallsEffectAnalyzerper tail item, each walking the full subtree. On deeply nested blocks this is O(N^2).Replace the per-item walks with a single O(N) bottom-up
PostWalker(populateExitingBranchCache) that pre-computes exiting-branch results for every node, making subsequent lookups O(1).Example: AssemblyScript GC compiles
__visit_membersas abr_tabledispatch over all types, producing ~N nested blocks with ~N tails. The old code walks each tail's subtree separately -- O(N^2) total node visits. With this change, one bottom-up walk covers all nodes, then each tail lookup is O(1).benchmark data
The test module is from issue #7319
#7319 (comment)
In main head
time ./build/bin/wasm-opt -Oz --enable-bulk-memory --enable-multivalue --enable-reference-types --enable-gc --enable-tail-call --enable-exception-handling -o /dev/null ./test3.wasm real 9m16.111s user 35m33.985s sys 0m51.000sIn the PR