[NFC] cache repeated tree walks to avoid O(N^2) in optimizeTerminatingTails in CodeFolding - #8602
Merged
Merged
Conversation
Changqing-JING
marked this pull request as draft
April 14, 2026 09:16
Changqing-JING
force-pushed
the
opt/compile-speed3
branch
from
April 15, 2026 14:00
7454548 to
a1924cf
Compare
Changqing-JING
marked this pull request as ready for review
April 15, 2026 14:02
Changqing-JING
marked this pull request as draft
April 15, 2026 14:23
Changqing-JING
force-pushed
the
opt/compile-speed3
branch
2 times, most recently
from
April 16, 2026 03:07
c2c710f to
82d92bb
Compare
Changqing-JING
marked this pull request as ready for review
April 16, 2026 05:01
Changqing-JING
force-pushed
the
opt/compile-speed3
branch
from
May 7, 2026 02:11
82d92bb to
672efc7
Compare
Changqing-JING
marked this pull request as draft
May 7, 2026 02:42
Changqing-JING
marked this pull request as ready for review
May 7, 2026 09:51
Contributor
Author
|
@kripken |
tlively
reviewed
May 7, 2026
tlively
left a comment
Member
There was a problem hiding this comment.
LGMT with some extra comments.
tlively
approved these changes
May 8, 2026
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.
Cache the result of getBranchTargets(getFunction()->body) in optimizeTerminatingTails so that recursive calls share the same computed set rather than each re-walking the entire function body. This avoids O(N²) behavior where N is the size of the function body, since the recursive calls previously each performed an O(N) tree walk. The cached targets are computed lazily on first need and passed through to the canMove overload that accepts pre-computed branch targets.
Benmark data
For the test case in #7319 (comment)
Main head:
time ./build/bin/wasm-opt --code-folding --enable-bulk-memory --enable-multivalue --enable-reference-types --enable-gc --enable-tail-call --enable-exception-handling -o /dev/null ./test3.wasm real 5m45.996s user 6m6.267s sys 0m3.798sThis PR:
time ./build/bin/wasm-opt --code-folding --enable-bulk-memory --enable-multivalue --enable-reference-types --enable-gc --enable-tail-call --enable-exception-handling -o /dev/null ./test3.wasm real 2m2.380s user 2m25.700s sys 0m2.449sBenchmark regression test
Test case: https://jetbrains.github.io/kotlinconf-app/73cbe24d7cf5a54d37ad.wasm
On main
On current PR