Skip to content

perf: find legacy catch block types in parallel - #346

Open
rossng wants to merge 1 commit into
wasm-bindgen:mainfrom
rossng:parallel-legacy-catch-types
Open

rossng wants to merge 1 commit into
wasm-bindgen:mainfrom
rossng:parallel-legacy-catch-types

Conversation

@rossng

@rossng rossng commented Nov 18, 2026

Copy link
Copy Markdown

parse_local_functions decodes every function body a second time to find the implicit block types of legacy catch handlers. It does this on one thread, before it parses the bodies in parallel. The scan runs for any module with a tag that has params, or with a function type that has two or more results. Every Emscripten module built with C++ exceptions has such a tag, __cpp_exception(i32), even when no function uses a legacy try.

Change

  • register_legacy_catch_block_types is renamed to legacy_catch_block_types. It now takes &self and returns the types instead of adding them to ModuleTypes.
  • With the parallel feature, the scan runs over all bodies with par_iter.
  • The serial loop adds each function's types after its entry block type, as before, so type IDs are unchanged.

Performance

I ran wasm-bindgen on a 74 MB Emscripten module built with C++ exceptions, on a 12-core Linux VM. wasm-bindgen enables walrus's parallel feature. The wasm-bindgen build is main with wasm-bindgen/wasm-bindgen#5479, wasm-bindgen/wasm-bindgen#5480, wasm-bindgen/wasm-bindgen#5481 and wasm-bindgen/wasm-bindgen#5482 applied. Each figure is the median wall time of 7 runs.

walrus Wall time
main 6.45 s
this PR 5.63 s

wasm-bindgen's output for this module is byte-identical with both versions of walrus.

The existing tests in legacy-exceptions-multivalue-catch.rs cover the handler types this scan finds, with and without parallel.

🤖 Generated with Claude Code

parse_local_functions decodes every function body a second time to find the
implicit block types of legacy catch handlers, on one thread, before it
parses the bodies in parallel. The scan runs for any module with a tag that
has params, which includes every Emscripten module built with C++ exceptions
because of the `__cpp_exception(i32)` tag, or with a function type that has
two or more results.

legacy_catch_block_types now takes &self and returns the types instead of
adding them to ModuleTypes, so with the `parallel` feature the scan runs over
all bodies in parallel. The serial loop adds each function's types after its
entry block type, as before, so type IDs are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@wasm-bindgen-bot wasm-bindgen-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Parsing a module scans every function body for the implicit block types of legacy catch handlers. This PR moves that scan out of the serial per-function loop. The scan now returns the types per function and, with parallel, runs over the bodies with par_iter. The serial loop still adds each function's types right after its entry block type, so TypeId order is unchanged. It looks aimed at speeding up wasm-bindgen on Emscripten C++-exception modules, where the __cpp_exception(i32) tag always triggers the scan. The change looks correct to me and I have no concerns: rayon's ordered collect::<Result<Vec<_>>> keeps results in function order, Module, IndicesToIds and the bodies are already shared across threads in the main parse, and the existing tests in legacy-exceptions-multivalue-catch.rs cover the handler types both with and without parallel.

@wasm-bindgen-bot wasm-bindgen-bot Bot added the merge-ready The bot's review of its own PR found no concerns; ready for a human approval label Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready The bot's review of its own PR found no concerns; ready for a human approval

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant