Skip to content

[NFC] cache repeated tree walks to avoid O(N^2) in optimizeTerminatingTails in CodeFolding#8602

Open
Changqing-JING wants to merge 1 commit intoWebAssembly:mainfrom
Changqing-JING:opt/compile-speed3
Open

[NFC] cache repeated tree walks to avoid O(N^2) in optimizeTerminatingTails in CodeFolding#8602
Changqing-JING wants to merge 1 commit intoWebAssembly:mainfrom
Changqing-JING:opt/compile-speed3

Conversation

@Changqing-JING
Copy link
Copy Markdown
Contributor

@Changqing-JING Changqing-JING commented Apr 14, 2026

In optimizeTerminatingTails, canMove repeatedly walked the entire function body to compute branch targets and find Try/TryTable nodes, causing O(N^2) behavior. This change caches those results lazily on first use via canMoveWithCachedBodyInfo, and resets the cache each pass iteration. No functional change — only a compile-time performance improvement for large functions.

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    7m53.093s
user    8m28.756s
sys     0m1.100s

This 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    4m25.416s
user    5m1.491s
sys     0m1.829s

@Changqing-JING Changqing-JING requested a review from a team as a code owner April 14, 2026 09:16
@Changqing-JING Changqing-JING requested review from tlively and removed request for a team April 14, 2026 09:16
@Changqing-JING Changqing-JING marked this pull request as draft April 14, 2026 09:16
@Changqing-JING Changqing-JING marked this pull request as ready for review April 15, 2026 14:02
@Changqing-JING Changqing-JING marked this pull request as draft April 15, 2026 14:23
@Changqing-JING Changqing-JING changed the title perf: cache repeated tree walks to avoid O(N^2) in optimizeTerminatingTails in CodeFolding [NFC] cache repeated tree walks to avoid O(N^2) in optimizeTerminatingTails in CodeFolding Apr 16, 2026
@Changqing-JING Changqing-JING marked this pull request as ready for review April 16, 2026 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant