Browse documentation

Optimize production WebAssembly modules with Binaryen wasm-opt

Date: 2026-12-03 Area: build, apps Refines: ADR 0244, ADR 0255 Related: ADR 0235, ADR 0236

Context

FreePascal's wasm32 backend has no assembly peephole optimizer (compiler/wasm32/aoptcpu.pas is an empty subclass of TAsmOptimizer). It emits redundant stack/local sequences such as local.set X; local.get X instead of local.tee X, redundant blocks, and unmerged constants. Even after wasm-ld --strip-all (ADR 0244) and native WebAssembly exceptions (ADR 0255), the ICU-linked GocciaRunner production module was 16.05 MB raw (2.61 MB brotli-11; 16.92 MB raw / 2.65 MB brotli-11 prior to ADR 0255).

Binaryen's wasm-opt optimizes linked WebAssembly binaries directly, shrinking code size, compile time, and runtime cost. However, benchmarking naive wasm-opt -O3 against AWFY under Node.js WASI uncovered a severe regression: Binaryen's default --one-caller-inline-max-function-size=-1 (unlimited) inlined the engine's single-caller 63 KB interpreter FastLoop directly into its 146 KB caller Execute, creating a ~179 KB mega-function that degraded V8 TurboFan tier-up and made AWFY 1.83x slower than the unoptimized module. Capping single-caller inlining at --one-caller-inline-max-function-size=100 prevented that merge and turned -O3 into a consistent win across size, compile time, startup time, and AWFY throughput.

Separately, passing --compress-relocations and -O2 to wasm-ld compacts LEB128 relocation padding and shrinks raw size by ~605 KB (~17 KB brotli-11) at link time even when wasm-opt is not installed.

Decision

  • Pass --compress-relocations and -O2 to wasm-ld on --prod wasm32-wasip1 builds. Both flags are cheap linker-only wins that apply whether or not wasm-opt is present.
  • Run Binaryen wasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-features after linking on --prod wasm32-wasip1 builds.
    • When wasm-opt will run, build.pas also passes -k--keep-section=target_features to wasm-ld so Binaryen automatically reads the exact WebAssembly feature set emitted by FreePascal and ICU4C (such as bulk memory, sign-extension, non-trapping float-to-int, reference types, and native exnref exceptions) without hardcoding feature flags in build.pas, and --strip-target-features removes the custom section from the finished module.
    • GOCCIA_WASM_OPT controls execution: 0 disables wasm-opt, 1 requires it on PATH (failing the build if missing), a file path names an explicit binary, and leaving it unset uses wasm-opt from PATH when present or skips with a notice when absent.
  • Pin Binaryen (version_133) by SHA-256 in scripts/install-binaryen.sh and cache it in CI (pr.yml, ci.yml). CI builds set GOCCIA_WASM_OPT=1 so published release, nightly, and playground modules are guaranteed to be post-link optimized.
  • Keep -O3 without --converge or -Oz. -O3 with --one-caller-inline-max-function-size=100 runs in ~6 s (~2 s once FPC avoids giant state-machine functions) and beats -Os on AWFY throughput (~1.6% faster) for <15 KB brotli-11 difference; --converge took ~96 s for ~3 KB brotli-11.

Consequences

  • On the ICU-linked --prod GocciaRunner with native exnref exceptions (fpc-toolchain 3.3.1+13, Node.js v24.18.0 WASI, interleaved A/B):

    Metricwasm-ld only+ wasm-ld -O2 + wasm-optDelta
    GocciaRunner raw size16,048,697 B12,111,376 B-24.53% (-3.94 MB)
    GocciaRunner gzip-9 size3,944,572 B3,341,643 B-15.29% (-603 KB)
    GocciaRunner brotli-11 size2,607,601 B2,307,231 B-11.52% (-300 KB)
    GocciaTestRunner raw size15,928,192 B12,069,012 B-24.23% (-3.86 MB)
    GocciaTestRunner brotli-11 size2,578,644 B2,294,575 B-11.02% (-284 KB)
    Cold WebAssembly.compile (11 reps)7.02 ms5.31 ms-24.3%
    Cold _start empty script (11 reps)35.13 ms29.90 ms-14.9%
    Cold GocciaRunner --output=json total (11 reps)28.41 ms24.19 ms-14.8%
    Warm instantiate + _start empty script (21 reps)7.03 ms6.54 ms-6.9%
    AWFY 14-workload steady-state geomean (5 reps, Sieve warmed)1.000x1.077x+7.7% (all 14 faster; 13 non-Sieve +7.1%)

    Prior to ADR 0255 (fpc-toolchain 3.3.1+12, branchful exceptions), the same optimization cut raw size by 22.62% (16,923,259 B to 13,095,498 B), brotli-11 by 10.17% (2,652,121 B to 2,382,321 B), WebAssembly.compile by 21%, cold _start by 11.9%, and improved 7-benchmark AWFY geomean by 5.2%.

  • Stripping target_features after optimization preserves native exnref / try_table exceptions and reference types: the entire wasm32-wasip1 test suite (scripts/check-wasm-modules.js, scripts/run-wasm-test-suite.js, and the website playground real-module runner tests including try_table unwind checks) runs directly against the wasm-opt-optimized modules in CI.

  • Local contributors without wasm-opt installed can still build --prod modules (getting the wasm-ld flags and a clear notice), or run bash scripts/install-binaryen.sh build/binaryen to match CI byte-for-byte.