Skip to content

build: link emscripten programs at their compile's level of optimization - #18

Merged
pinhopro merged 1 commit into
mainfrom
tools/emscripten-release-link
Nov 15, 2026
Merged

pinhopro merged 1 commit into
mainfrom
tools/emscripten-release-link

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

b2 5.5.3's emscripten toolset compiles each translation unit at the level <optimization> names (-O3 in variant=release) but passes no -O to its link, which em++ then runs at -O0. Release programs were therefore linked without optimization: Binaryen's wasm-opt never ran on the whole module, and under Asyncify the instrumentation was applied to unoptimized code, saving whole frames at each suspension.

What changes

  • Jamroot maps <optimization> to em++'s link options with toolset.flags on emscripten.link (off -O0, speed -O3, space -Os, minimal -O1, debug -Og), in the region emscripten-link-optimization. They are the toolset's own options, before <linkflags>, so a program that asks for another level still gets it.
  • Debug builds link as before: their outputs are byte-identical.
  • tools/test/emscripten_test.py gains test_a_link_is_optimized_as_its_compile, which fails on main.
  • AGENTS.md (chapter 4 Targets, chapter 6), CODING-GUIDELINES.md and the 0.1.0 release notes describe it.

Measured (Emscripten 6.0.11, release)

  • asio timers: 735,400 -> 489,216 bytes of .wasm; timers_asyncify: 2,481,578 -> 988,047.
  • asio asyncify_stack, deepest unwind: 2,900 -> 388 bytes (1,900 -> 304 without exceptions), now identical to its optimized_link build; cobalt's: 1,092 -> 384.

Verification

  • Every emscripten lane of asio, beast, cobalt, config, document, idna, lexical, pratt and trystero, in release and debug, with each own lane's CI options.
  • All pass except two release-only test-harness assumptions that the optimized link reveals, to be fixed in their libraries (CI builds these lanes in debug, where they pass):
    • cobalt's test/no_exceptions/driver.py expects the ASSERTIONS-only abort text Aborted(native code called abort()).
    • asio's test/browser/worker_page.html requires Emscripten's doPollAsync, which the optimized link drops from browser_worker_move_failure because that program never polls.
  • Tools tests: emscripten, jamroot, harness, webcpp_jam, variants, drive, changelog, release and matrix all pass.

Follow-ups in the libraries: asio's asyncify-stack-size rationale and its optimized_link tests and docs, and cobalt's optimized_link tests, describe the old link.

🤖 Generated with Claude Code

b2 5.5.3's emscripten toolset compiles at the level <optimization> names
(-O3 in release, as gcc.jam maps it) and gives its link no level, which
em++ runs at -O0: a release program was linked without optimization, so
Binaryen's wasm-opt never ran on the whole module and Asyncify instrumented
it unoptimized, saving whole frames at each suspension.

The Jamroot now maps <optimization> to em++'s link options with
toolset.flags on emscripten.link: off -O0, speed -O3, space -Os, minimal
-O1, debug -Og. They are the toolset's own options, before <linkflags>, so
a program that asks for another level still gets it, and they reach every
program b2 links with em++, whatever project declares it. A debug build
links as before (its outputs are byte-identical).

Measured with Emscripten 6.0.11, asio's release timers test went from
735,400 bytes of .wasm to 489,216, and the deepest suspension of its
asyncify_stack test from 2,900 bytes to 388 (1,900 to 304 without
exceptions), the same as its optimized_link build.

tools/test/emscripten_test.py checks the level of each value against the
compile's, and that a program's own linkflags come last. AGENTS.md,
CODING-GUIDELINES.md and the 0.1.0 release notes say so.
@pinhopro
pinhopro merged commit 28e5e05 into main Nov 15, 2026
12 checks passed
pinhopro pushed a commit that referenced this pull request Nov 15, 2026
Chapter 15's trystero entry keeps run_case as the rule, which keeps a test's
cases out of main's frame whatever the link, and gives the measurements
under the link the Jamroot now gives a release program, -O3: the room,
app_impl and allocation tests' deepest suspensions write 564, 440 and 408
bytes in release and 2,344, 2,068 and 2,020 in debug, now the deepest
build, and a main with every case inlined fits at -O3. The release runs
stay as trystero's only runs under Asyncify in the CI's emscripten lane.
Chapter 19's "Emscripten's release link." is removed: the Jamroot's link
options fixed it (#18).
@pinhopro
pinhopro deleted the tools/emscripten-release-link branch November 17, 2026 04:05
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