Skip to content

Add a WebAssembly build with threads for pages that are cross-origin isolated - #226

Merged
nyosegawa merged 1 commit into
mainfrom
wasm-threads
Nov 20, 2026
Merged

nyosegawa merged 1 commit into
mainfrom
wasm-threads

Conversation

@nyosegawa

@nyosegawa nyosegawa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Builds on #225 (base wasm-build, built on f3a6280).

What changed

A second WebAssembly module, with threads, for pages that are cross-origin isolated.

  • emcmake cmake -B build-wasm-threads -DSPEECH_WASM_THREADS=ON builds libspeech-threads.js and libspeech-threads.wasm, every file compiled with -pthread. Its memory is a SharedArrayBuffer, which a browser gives only to a page served with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp.
  • The module starts a Worker for each of the browser's logical processors before it runs (PTHREAD_POOL_SIZE=navigator.hardwareConcurrency): ggml starts its threads for each graph and waits for them within the call, before a Worker asked for then could start. The library refuses more threads than that at speech_load_params_set_threads(), and with PTHREAD_POOL_SIZE_STRICT=2 a thread past them fails to start rather than waits.
  • 4 threads by default, or fewer logical processors: with all 10 of an Apple M5 the first request of Silero VAD on a minute of audio ran at RTFx 100, against 560 with 4.
  • Its memory grows to 1 GB at most: Safari on iOS counts a shared memory's maximum against the process when the memory is made, and has refused 2 GB (iOS 16.2, reported by others).
  • speech.mjs: openSpeech({ threads: true }) loads the module with threads and refuses a page that is not isolated. The page has a Threads field (and &threads=N in a link) and runs the module with threads for more than one. wasm/serve.py serves the folder on 127.0.0.1 with both headers.
  • The smoke takes --threads N, and then also checks that audio past the module's 1 GB is refused as out_of_memory before the module writes it (the review's finding on Build the C API for WebAssembly and run Silero VAD in a browser #225, which needs a module whose memory cannot grow), and that the next request answers.
  • Docs: docs/browser.md describes both modules, their limits and numbers; c-api.md and the header give the threads of each; install.md's build table. ADRs: a new record of the threads module's decisions; the platform and Worker records mention it.

Numbers

Silero VAD F32, a minute of audio, Apple M5 (10 logical processors), macOS 26.2, headless Chrome 158, Emscripten 7.0.0, 2026-11-21, medians of three runs:

libspeech threads, 2 threads, 4
.wasm and .js gzipped 360 KB, 18 KB 340 KB, 22 KB 340 KB, 22 KB
module start 6 ms 17 ms 19 ms
load time 9 ms 8 ms 9 ms
first request RTFx 480 RTFx 640 RTFx 560
fifth request RTFx 670 RTFx 1080 RTFx 1350
memory at most 29.9 MiB 29.9 MiB 35.9 MiB

speech vad natively: RTFx 660 with one thread, 1220 with four.

Checked

  • wasm_browser_smoke.mjs on seven WAVE files with the module without threads, with --threads 4, and from one folder holding both with --threads 2: every region equal to speech vad's, 11 threads refused on 10 logical processors, 1.08 GB of audio refused as out_of_memory. Without the allocation check in speech.mjs that last check fails with "offset is out of bounds".
  • The page in Safari on the iOS 26.5 simulator (iPhone 17) with 4 threads: cross-origin isolated, the 1 GB shared memory made, the same regions. Not checked on a real iPhone, whose memory the simulator does not limit.
  • A native build and speech-api-check detect with Silero VAD.

🤖 Generated with Claude Code

@nyosegawa

Copy link
Copy Markdown
Owner Author

Checked outside CI, on an Apple M5 (macOS 26.2, 10 logical processors) with Emscripten 7.0.0 and this branch at 0701a88:

  • wasm_browser_smoke.mjs with the module without threads, with --threads 4 on build-wasm-threads, and with --threads 2 from one folder holding both: every region equal to speech vad's, 11 threads refused, 1.08 GB of audio refused as out_of_memory, ok.
  • The page in Safari on the iOS 26.5 simulator with &threads=4: cross-origin isolated, the 1 GB shared memory made, the same regions. Not on a real iPhone.

…isolated

SPEECH_WASM_THREADS builds libspeech-threads.js and libspeech-threads.wasm,
every file compiled with -pthread, whose memory is a SharedArrayBuffer that a
browser gives only a page served with COOP and COEP. The module starts a
Worker for each of the browser's logical processors before it runs, since
ggml starts its threads for each graph and waits for them within the call,
before a Worker asked for then could start; the library refuses more threads
than that at speech_load_params_set_threads(), and a thread past them fails
to start rather than waits. It computes on 4 threads by default: Silero VAD
on a minute of audio ran its first request at RTFx 560 with 4 threads and 100
with all 10 of an Apple M5. Its memory grows to 1 GB at most, since Safari on
iOS counts a shared memory's most against the process as it makes it and has
refused one of 2 GB.

speech.mjs loads either module and refuses the one with threads in a page
that is not isolated; the page has a field of threads and runs the module
with threads for more than one; wasm/serve.py serves the folder with both
headers. The smoke takes --threads N and then checks that audio past the
module's 1 GB is refused as out_of_memory before the module writes it, and
that the module answers after it. A new record says why there are two
modules, why the Workers start first, and why 4 threads and 1 GB; the
platform record and docs/browser.md describe both, with their numbers.

Checked: wasm_browser_smoke.mjs on the seven WAVE files with the module
without threads, with --threads 4 and from one folder holding both with
--threads 2: every region equal to speech vad's, 11 threads refused on 10
logical processors, audio of 1.08 GB refused (the check fails with
"offset is out of bounds" when speech.mjs does not check malloc()). The
page in Safari on the iOS 26.5 simulator with 4 threads: cross-origin
isolated, the same regions. speech-api-check detect with Silero VAD and a
native build. Apple M5, macOS 26.2, Chrome 158, Emscripten 7.0.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nyosegawa
nyosegawa merged commit 6bc11fe into main Nov 20, 2026
7 checks passed
@nyosegawa
nyosegawa deleted the wasm-threads branch November 20, 2026 17:09
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