Compiler Flags for WASM Inference: What They Do to Pyodide Performance

Which Emscripten flags change Pyodide inference latency and which only change size or compile time, plus how to sweep them on the real target hardware.

Compiler Flags for WASM Inference: What They Do to Pyodide Performance
Written by TechnoLynx Published on 11 Jul 2026

Pyodide forbids -pthread, one of the Emscripten flags that could change inference speed: libraries built with it will not load. Published measurements show no single -O level wins across programs, so choose flags by testing them on your own target. Whether the Pyodide path is worth tuning in the first place is a separate decision, covered in how Pyodide works and when it fits. This article assumes you have already chosen Pyodide and now need to know which flags are worth sweeping.

Which Emscripten and Pyodide Flags Affect Inference Latency, and Which Only Size or Compile Time?

The table sorts the common flags by what they mostly act on. “Latency” means time per inference once the module is warm. “Cold-start” means download, compile and initialisation before the first inference.

Flag or setting Mainly affects What to expect
-O1, -O2, -O3 Latency, compile time Higher levels optimise more aggressively, at the cost of longer compiles. -O3 can also produce larger code.
-Os, -Oz Size, some latency Reduce code size at some cost to performance, with -Oz cutting more than -Os. In some benchmarks -Oz produced the fastest Wasm anyway.
-Ofast Latency (sometimes the wrong way), numerics Adds inexact-math passes on top of -O2. It has been measured running slower than -O1 and -Oz.
-msimd128 Latency on vectorisable loops Enables Wasm SIMD and LLVM autovectorisation.
-mrelaxed-simd Latency on multiply-add heavy code Lets 128-bit operations use relaxed-SIMD fused multiply-add instead of separate multiply and add.
-pthread, PTHREAD_POOL_SIZE Latency through parallelism Not usable for Pyodide libraries: they will not load if built with -pthread.
MALLOC=emmalloc / dlmalloc / mimalloc Size against allocation speed emmalloc is smaller; the docs recommend dlmalloc for many small objects and mimalloc for malloc contention.
MINIMAL_RUNTIME, FILESYSTEM=0 Size Strip runtime features and filesystem support to cut code size.
EVAL_CTORS Cold-start Runs constructors and main() at compile time and snapshots the state into the Wasm, which can either shrink or grow the binary.
ASYNCIFY Size and latency (as a cost) Carries a significant code-size and speed cost.
DISABLE_EXCEPTION_CATCHING Size and latency Catching is off by default from -O1 up because exceptions are costly in size and speed.

One structural point the table can hide: Emscripten settings apply at link time and have no effect during compilation unless the documentation says otherwise. A setting passed only at compile time therefore does nothing, so check the final link command before you trust a measurement.

No -O Level Wins Everywhere

The Emscripten documentation recommends -O2 for a well-optimised build and says -O3 or -Os can do better, and that trying the options against your own application is the way to find out. Across 41 C benchmarks compiled to Wasm, -Oz produced the fastest binary in 15 cases, -O1 in 8, -O2 in 8 and -Ofast in 10, and the authors concluded there is no silver-bullet flag. Their geometric means put -Oz at 0.86x the execution time of -O2 for Wasm, while -Ofast came out unexpectedly slower than both -O1 and -Oz. In one benchmark (ADPCM), -Ofast took 1.50x as long as -O2 because it missed a dead-code elimination that -O2 performed.

The authors’ explanation is that these optimisations were designed for x86 rather than for Wasm. The GCC-side semantics of these levels, and native flags such as -march that have no Emscripten equivalent, are covered in GCC optimization flags explained.

Emscripten describes -Os and -Oz as size-reducing, whereas the IMC study found code size within 2% of -O2 across -O1, -Ofast and -Oz for its benchmarks. Measure size on your own module rather than taking either figure as a forecast for it.

What -msimd128 and -mrelaxed-simd Buy Browser Inference

SIMD is the flag class most likely to move inference latency, because neural-network kernels are dominated by vectorisable multiply-adds. Passing -msimd128 at compile time enables Wasm SIMD and also turns on LLVM’s autovectorisation passes. If you want explicit intrinsics without the autovectoriser, add -fno-vectorize -fno-slp-vectorize. That split helps during a sweep: it tells you whether a gain came from your hand-written intrinsics or from the compiler rewriting your loops. Code can detect the target at build time through __wasm_simd128__ and __wasm_relaxed_simd__.

Measured gains vary by workload. An HPC study found SIMD gave 1.36x better throughput on one benchmark compared with the same Wasm built without SIMD. The TensorFlow.js team reported in 2020 that SIMD brought 1.7 to 4.5x over plain Wasm on its inference models in Google Chrome, with XNNPACK kernels underneath. XNNPACK is a heavily optimised library of neural-network operators. Generic code that the autovectoriser happens to reach should not expect the same.

Three constraints keep SIMD from closing the gap to native:

  • Width. Wasm SIMD covers only 128-bit vectors, against 512-bit instructions on modern Intel processors. The 256-bit AVX instructions are emulated as two 128-bit operations. What that width means for achievable throughput is the subject of CPU GFLOPS for WASM inference.
  • Emulation. Emscripten warns that semantic differences between host hardware and Wasm “sometimes will lead to poor performance”. Float min and max cost 7 to 10 x86 instructions because of NaN propagation rules, where pmin/pmax cost one. The SSE test intrinsics are scalarised entirely.
  • FMA. Fused multiply-add needs -mrelaxed-simd. With -msimd128 alone, those operations compile to separate multiply and add.

Threading Flags, and Why They Stop at Pyodide’s Door

Outside Pyodide, threading is a further lever on inference speed. Emscripten threads need -pthread at both compile and link time, and -sPTHREAD_POOL_SIZE creates web workers before main() so the first parallel call does not wait on worker start-up. TensorFlow.js measured multithreading at a further 1.8 to 2.9x on top of SIMD.

None of that reaches a Pyodide extension. The Pyodide ABI states that -pthread must not be used at compile or link time, and that libraries built with it will not load. Pyodide also does not support threading or multiprocessing, so packages that use them need a patch to disable it. If your risk model’s native code expects a thread pool, it needs a patch to run single-threaded inside Pyodide.

Threads are available outside Pyodide, for example in ONNX Runtime Web called from JavaScript. That route carries a deployment constraint of its own. ONNX Runtime Web’s multithreading only works when the page is crossOriginIsolated, and Chrome restricted SharedArrayBuffer on sites that are not cross-origin isolated from Chrome 92. If the page has to embed third-party content that cannot meet COEP, threading is off the table whatever the build does. When you do build threaded, inspect the output with wasm-objdump for atomic opcodes and shared memory: if none are there, the module is single-threaded however many workers you start. TensorFlow.js handles mixed browser support by detecting SIMD and threads at runtime and serving a different binary for each case.

Why -O3 Rarely Fixes a Compute-Bound CPython-on-WASM Pipeline

The flags you control apply to your own compiled extensions. They do not apply to Pyodide’s WebAssembly CPython interpreter, which ships prebuilt inside pyodide.asm.wasm as direct Emscripten output. If a profile shows most of the time going to Python-level code (the interpreter loop, object allocation, crossing between Python and numpy), raising your extension’s -O level optimises the part that was not slow.

There is also a gap between WebAssembly ML inference and native code. Jangda and colleagues (USENIX ATC 2019) found applications compiled to Wasm ran 45% (Firefox) to 55% (Chrome) slower than native across SPEC CPU, and attributed part of the gap to properties inherent to the platform. The IMC 2021 study found that browser JIT optimisation gave no substantial speed-up for Wasm, unlike JavaScript.

Profile first and only then pick the flag class: SIMD and -O sweeps when a native kernel is hot, and changes outside the flags when the interpreter dominates.

Cold-Start Is Mostly Download and Initialisation, Not Codegen

The Pyodide roadmap gives the baseline: a first load needs a 6.4 MB download and 4 to 5 seconds of environment initialisation, with later loads faster because the browser caches the assets. The same page names optimised compilation parameters as one lever, alongside minifying the standard library and packages and reducing exported symbols. Pyodide 0.23 added pyodide py-compile for wheels and, to reduce download size, unvendored further standard-library modules.

Engine tiering also shapes first-call speed. In V8, Liftoff generates code quickly for fast start-up, while TurboFan generates faster code for peak performance, and dynamic tiering only sends repeatedly executed functions to TurboFan. Measure first-call latency and warm latency separately, or a flag that helps one will look like noise in the other.

Where your flags go depends on how you build:

  • Recipe builds. In a Pyodide recipe, build/cflags, build/cxxflags and build/ldflags in meta.yaml pass extra compiler and linker arguments.
  • Out-of-tree builds. pyodide build intercepts gcc, g++ and ld, redirects them to emcc, and translates flags for Wasm compatibility, so your existing setup.py, CMakeLists.txt or meson.build is used as-is. Check what the translated command actually contains.
  • ABI flags that are not tuning options. Shared libraries must link with -sWASM_BIGINT. C++ exceptions are off by default, and throw aborts unless -fexceptions is passed at both compile and link.

Has Anyone Published a Pyodide Cold-Start Benchmark for Chromebooks?

We know of no published benchmark of Pyodide cold-start times across common Chromebook models that could stand in for your own measurement, and no source we reviewed measures cold-start per Emscripten flag on that class of hardware. The dependable reference is a flag-sweep harness run on a sample of the actual classroom devices; TechnoLynx builds and runs that harness on representative classroom Chromebooks and delivers validated flag settings with the measured results before you ship to every district.

For the hardware side of the question, LynxBenchAI’s published results show how each device runs the public PyTorch stack. They do not run or measure your Pyodide application, so they complement the on-device sweep and do not replace it.

Three caveats limit any such validation. Wasm performance varies by browser and platform, so re-run the sweep when the fleet’s browser or image changes. A cold-start setting such as EVAL_CTORS can increase binary size as well as reduce it. A small device sample may not cover the spread of hardware across districts, so pick the sample from the slowest models in service, not the newest.

Measuring the Per-Flag Delta Against a Fixed Target

The sweep is only useful if every run is judged against the same targets on the same device. Run it in this order:

  1. Fix the targets first. Write down the per-inference latency, module or bundle size, and cold-start limits the pipeline has to meet on the slowest target device.
  2. Record the baseline. Build at -O2, which the IMC 2021 study used as its baseline for balancing run time, size and compile time.
  3. Change one flag class at a time. Sweep -O1, -O2, -O3, -Oz and -Ofast, then add -msimd128, then -mrelaxed-simd, then allocator and runtime settings, keeping the input fixed.
  4. Time with performance.now() and repeat. The IMC study bracketed each program with performance.now() calls and averaged five runs. Timer resolution is limited to 100 microseconds or more without cross-origin isolation and can reach 5 microseconds with it, so time batches when a single inference is short.
  5. Separate first call from warm calls. Report cold-start (fetch, compile, loadPyodide, package load), first inference and warm median separately, for the tiering reasons above.
  6. Measure size on the shipped artefacts. Record the .wasm and wheel sizes as served, compressed, because that is what the device downloads.
  7. Repeat per browser. IMC 2021 found Firefox took 0.61x Chrome’s time on desktop Wasm but 1.48x on mobile, so a flag set ranked in one browser may rank differently in another.

The output is a table of deltas per flag against the three targets, with a note on which flags moved the bottleneck and which only added compile time. Flag changes that alter module size or cold-start are also release gates, and re-checking them after tuning belongs in a release-readiness decision framework. LynxBenchAI benchmarks AI performance across hardware more generally, with stock PyTorch. The point here is narrower: compare deltas against your own fixed target rather than against someone else’s chart.

When Flags Justify a Pyodide Path, and When They Only Move the Cost

Flags justify staying on Pyodide when the profile puts the time in a native kernel that SIMD and the right -O level can reach, and the sweep brings latency, size and cold-start inside the targets on the slowest device. They only move the cost when the gain comes from shifting work into compile time, or into a bigger download that hurts cold-start, while the interpreter-bound part of the pipeline stays where it was.

The reason to run client-side should be a named trigger: a performance ceiling, a deployment-target constraint or a lock-in cost. Portability work is most valuable when it is scoped against a measurable trigger like that, rather than undertaken for its own sake. If the sweep cannot meet the targets, one option is a different runtime such as ONNX Runtime Web, whose multithreading needs cross-origin isolation. Teams weighing that choice for production SaaS can see how we approach AI for AI infrastructure and production SaaS.