Skip to content

ariadne_wasm: JSPI measured against Asyncify for browser guests (G2, last part) - #1272

Merged
transfix merged 1 commit into
masterfrom
feat/wasm-jspi-vs-asyncify
Nov 16, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/wasm-jspi-vs-asyncify

Conversation

@transfix

@transfix transfix commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

G2's last part (ARIADNE_SCRIPTING_HOSTS.md §8.5, §9 item 13): JSPI measured against Asyncify for WebAssembly guests in the browser. One commit on master, after #1245: a bench and the roadmap's write-up. No shipped code changes.

What it found

Asyncify stays the browser's way to pause a guest. JSPI would make guests smaller and faster, but today's ariglx can only wait for a JSPI guest through its own Asyncify, at about 11 µs a slice. A page on JSPI throughout would win everywhere, but it needs every wasm package on wasm exceptions. That is left open in §9, item 13.

Measured in headless Chrome 157 on sandipaws. Chrome's processes ran at High priority on a busy box; each number is the median of 9 rounds, each round timing every variant once.

  • Size. Asyncify more than doubles a metered guest:

    • the bench's guest grows from 7.5 to 20.9 KiB;
    • ari_wasm_meter.wasm, metered, grows from 0.4 to 1.0 MiB;
    • ariadne_wasm_browser_test.wasm (9.1 MiB as built) grows from 14.3 MiB metered to 30.3 MiB (3.9 to 7.8 MiB gzipped).

    Asyncify took 594 s on that last one, and metering took 0.3 s. A JSPI guest is just the metered one.

  • Speed, unsliced, against the metered guest.

    • Asyncify: fib 30.1 ms against 14.1, qsort 1495 against 530, SHA-256 80 against 62, the interpreter 370 against 339, a loop 52 against 46.
    • JSPI: the metered speed.
  • A pause.

    • Asyncify: 0.5 µs at the top of the stack, 3.7 µs 48 frames down.
    • JSPI: about 1.0 µs at any depth.
  • The host's wait.

    • ariglx's stack at a slice. I probed the smoke's guest.ari page: 34 frames are below the guest, and 13 of those calls go through invoke_*. That is Emscripten's JS exception handling, which libcvc builds with.

    • A JSPI host built like that doesn't run at all. JSPI can't suspend a stack with JS frames on it. Under -sJSPI, Emscripten also makes every invoke_* a suspending import, so nested calls fail with "SuspendError: trying to suspend JS frames" even when nothing waits.

    • So ariglx would wait through its own Asyncify. A poll at 1,000-gas slices costs:

      host guest 1 frame 34 frames 128 frames
      Asyncify, JS exceptions (as ariglx) Asyncify 0.6 µs 0.7 µs 0.5 µs
      Asyncify, JS exceptions (as ariglx) JSPI 3.6 µs 11.4 µs 24.8 µs
      JSPI, wasm exceptions JSPI 0.6 µs 0.6 µs 0.6 µs
  • So, per default slice (64,000 gas) in today's ariglx: a JSPI guest pays about 11 µs to save Asyncify's cost to its code.

    • For fib and qsort that cost is 36 and 41 µs, so JSPI wins.
    • For the loop, SHA-256 and the interpreter it is 1 to 3.5 µs, so JSPI loses.
  • A page on JSPI throughout would win everywhere. The host's wait drops to 0.6 µs, the guests skip Asyncify, and the host's own Asyncify cost from W1 goes too: CPython at half speed, and 8% of the module. What it needs:

    • -fwasm-exceptions across libcvc and every wasm package. Rust's Emscripten target needs the same move to link into cvcdbg's wasm build.
    • JSPI in the browser: Chrome since 137 and Safari from 27, with Firefox planned for 153.
    • The Asyncify build kept for browsers without JSPI.

    G3's WAMR needs the Asyncify build anyway.

The bench

src/ariadne_wasm/bench/. It isn't built by CMake or run in CI.

  • jspi_guest.c: the five workloads.
    • a loop;
    • the same loop 48 frames down;
    • fib;
    • libc's qsort;
    • SHA-256;
    • an interpreter dispatching through a table.
  • jspi_host.cpp: the host. It recurses N frames, each holding an object with a destructor, then polls the guest. The driver builds it five ways: Asyncify or JSPI, with no exceptions, JS exceptions (-fexceptions, as libcvc) or wasm exceptions.
  • jspi_bench.js and jspi_bench.html: the page. Three sections: the workloads, the pause cost, and the hosts.
    • An Asyncify guest is driven as browser_runtime.cpp drives one.
    • A JSPI guest has ari.gas as a WebAssembly.Suspending and its exports through WebAssembly.promising.
    • Each case's variants are timed in interleaved rounds, and every result and gas count is checked against the others.
  • jspi_bench.mjs: the driver. It builds everything with emsdk's emcc and wasm-opt and ari_wasm_meter, taking CVC_WASM_GUEST_FEATURES from ariadne_wasm.cmake. Then it serves the page cross-origin isolated and runs it in headless Chrome over CDP. --sizes also transforms other modules for sizes. --high-priority (Windows) raises Chrome's processes.
node src/ariadne_wasm/bench/jspi_bench.mjs --meter <ari_wasm_meter> --chrome <chrome> \
     [--query 'reps=9'] [--sizes A.wasm ...] [--high-priority] [--json out.json]

Checked

  • The bench's own checks: every variant's result and gas matched in every case. That includes gas under both pause mechanisms at 64,000-, 8,000- and 1,000-gas slices.
  • clang-format 18.1.8 on jspi_guest.c and jspi_host.cpp: clean.
  • node --check on both scripts.
  • The probe of ariglx's stack ran on ariglx-web: WebAssembly guests in the page (G2, third part) #1245's ariglx-web build. That page passed its guest.ari case with the probe in it.

Not covered

  • Browsers other than Chrome. Safari 27 and Firefox weren't run here.
  • wasmtime natively running the metered guest without Asyncify. It would drop the Asyncify code it carries now, but that wasn't measured.
  • The page on JSPI throughout. Only the bench host was built that way, not ariglx.

Base automatically changed from feat/ariglx-web-wasm-guests to master November 15, 2026 08:38
…last part)

src/ariadne_wasm/bench/jspi_bench.mjs builds five C workloads as guests
(jspi_guest.c), meters them, and runs them in headless Chrome four ways:
plain, metered, metered then Asyncify (today's install) and metered under
JSPI, unsliced and sliced. A host (jspi_host.cpp) built under Asyncify or
JSPI, with no exceptions, JS exceptions or wasm exceptions, polls a guest
from N frames down. Every variant's result and gas is checked against the
others'.

What it found (Chrome 157, sandipaws):
- Asyncify more than doubles a metered guest (14.3 to 30.3 MiB for a 9.1 MiB
  C++ module) and runs call-heavy code at half speed or less. Under JSPI a
  guest runs at the metered speed.
- A pause: Asyncify 0.5 us at the top of the stack and 3.7 us 48 frames
  down; JSPI about 1.0 us at any depth.
- ariglx's stack at a slice is 34 frames, 13 of them through invoke_*
  (libcvc's JS exceptions). JSPI can't suspend JS frames, and a JSPI host
  built with JS exceptions doesn't run at all. So ariglx would wait for a
  JSPI guest through its own Asyncify: 11.4 us a slice, against 0.7 for an
  Asyncify guest. A JSPI host with wasm exceptions waits 0.6 us at any depth.

So Asyncify stays the browser's way to pause a guest. A page on JSPI
throughout would win everywhere, but needs every wasm package on wasm
exceptions. That is open question 13 in the roadmap.

docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md: §8.5's as-measured section, §5.3's
third fallback, open question 13, and the G2 row (done).
@transfix
transfix force-pushed the feat/wasm-jspi-vs-asyncify branch from 5dffd1f to 8772230 Compare November 15, 2026 21:20
@transfix
transfix enabled auto-merge (squash) November 15, 2026 21:20
@transfix
transfix merged commit 4e2b0d6 into master Nov 16, 2026
29 of 30 checks passed
@transfix
transfix deleted the feat/wasm-jspi-vs-asyncify branch November 16, 2026 06:04
transfix added a commit that referenced this pull request Nov 16, 2026
Conflicts (includes in ariadne.h, ariadne.cpp and ariadne_runtime_test.cpp; three hunks of
#1241's menubar tray in ariadne.cpp) keep both sides. #1241's code meets this branch's groups:

- An action's context keeps master's scope (a quit application's actions stop) and is built by
  this branch's finish_context.
- The notify verbs bind by kind with the backend's (install_backend_verbs is now
  install_host_verbs): host_proxied in an `any` group, as cvc::notify mirrors the feed into
  the app's notify.* subtree raw on the caller's thread.
- What they capture (scope, source, a copy of mount_names) is taken on the host thread when a
  context is made (NotifyScope); a spawned child, made on the thread that pumps its group, binds
  from its parent's copy through its spawn context instead of reading mount_names there.
- quit_app kills the application's actions on current_sched() (the document's group), not
  only on group 0 when this Runtime used it.
- drain_begin runs builtin_event before the handler seam, through this branch's `m`.

Tests: AriadneIsolatedDocs.TheNotifyVerbsRunOnTheFramesThread (the feed is written on the
frame's thread while a worker pumps the program; a spawned child posts as its parent's scope)
and AriadneIsolatedDocs.QuitKillsALoadedDocumentsActionsInItsGroup.
STATE_EXEC_THREADED.md: the notify verbs' row, Quit among the kills that go to the group, and
the spawned child's notify scope.
transfix added a commit that referenced this pull request Nov 17, 2026
#1308)

* docs: JSPI and wasm exceptions across the browser stack (JX1-JX4 plan)

Joe decided on 2026-11-16 to move every wasm host from Asyncify to JSPI, and
libcvc and every cvcpkg wasm package from JS exceptions and longjmp to wasm's
own. That settles ARIADNE_SCRIPTING_HOSTS.md section 9, item 13.

WASM_JSPI_EXCEPTIONS.md is the plan. It covers:
- why (#1272's numbers);
- a mixed-link table from emsdk 5.0.7, which shows the break runs both ways;
- which archives in ariglx-web's closure use JS longjmp or JS exceptions;
- the four phases: JX1 cvcpkg, JX2 libcvc, JX3 guests, JX4 the switch.

Section 8.5 now gives the current browser support (Firefox 153 and Safari 27
have shipped JSPI). It also says there is no Asyncify fallback, since Asyncify
and wasm exceptions don't compose.

* docs: JX1 as built: EMCC_CFLAGS, the catalog scan's 13 recipes, and the PR open until JX4

JX1 started in cy-pca/cvcpkg (feat/wasm-exceptions):
- The flag goes through EMCC_CFLAGS, which only emcc reads, not CFLAGS, since several recipes
  build native host tools beside it.
- The pack gate reads wasm import sections itself and checks LTO bitcode's personality.
- A scan of all 126 latest wasm and wasm-mt bundles on cvcpkg.org found 13 cvcpkg recipes with
  JS-exception or JS-longjmp objects, plus libcvc's own three. Only those 13 are bumped.
- The cvcpkg PR stays open until JX4: on master, the flag would put bundles today's libcvc can't
  link onto prod.
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