Per-request compileStreaming vs pre-compiled Module shared to workers for tail latency
0 reputation · 01 Nov 2025, 15:49 UTC
0 reputation · 01 Nov 2025, 15:49 UTC
Goal is to keep tail latency stable for a WebAssembly service under concurrent requests. Single-request benchmarks hide contention that appears when multiple requests trigger compilation and instantiation at the same time.
Approach A uses per-request WebAssembly.compileStreaming and instantiateStreaming to compile while bytes arrive. Approach B compiles once with WebAssembly.compile and transfers the resulting WebAssembly.Module to workers, creating a new WebAssembly.Instance per request.
The WebAssembly specification defines no cross-isolate compilation cache and no performance guarantees. Whether a transferred Module avoids recompilation, and how instantiation contends under load, is implementation-defined and varies by engine.
Does transferring a WebAssembly.Module to workers guarantee compiled code reuse without recompilation under concurrent instantiation? How does instantiation contention behave under load for per-request compileStreaming versus a shared pre-compiled Module? What engine-specific guarantees exist for compiled code sharing across isolates?
No, transferring a WebAssembly.Module to workers does not guarantee that the compiled code is reused without recompilation; reuse depends on the engine and how it handles the transferred artifact.
When a Module is posted to a worker, the receiving engine can either deserialize the binary and reuse already‑generated native code (if it maintains an in‑process code cache) or it may treat the bytes as a fresh module and re‑compile or re‑optimize them. Because the spec does not require a cache, the outcome varies by engine (V8, SpiderMonkey, Wasmtime, etc.) and by whether the worker shares the same process/agent cluster as the main thread. Instantiation, however, always allocates per‑instance state (memories, tables, globals) and runs the start function, so even with perfect code reuse the per‑request latency can still be limited by instantiation work and any contention on shared resources such as the heap or atomics.
If you are unsure whether your workers share a process/agent cluster with the main thread, please confirm that detail, as it directly influences whether module transfer can reuse compiled code and thus affects the tail‑latency recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.