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-relocationsand-O2towasm-ldon--prodwasm32-wasip1builds. Both flags are cheap linker-only wins that apply whether or notwasm-optis present. - Run Binaryen
wasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-featuresafter linking on--prodwasm32-wasip1builds.- When
wasm-optwill run,build.pasalso passes-k--keep-section=target_featurestowasm-ldso 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 nativeexnrefexceptions) without hardcoding feature flags inbuild.pas, and--strip-target-featuresremoves the custom section from the finished module. GOCCIA_WASM_OPTcontrols execution:0disableswasm-opt,1requires it onPATH(failing the build if missing), a file path names an explicit binary, and leaving it unset useswasm-optfromPATHwhen present or skips with a notice when absent.
- When
- Pin Binaryen (
version_133) by SHA-256 inscripts/install-binaryen.shand cache it in CI (pr.yml,ci.yml). CI builds setGOCCIA_WASM_OPT=1so published release, nightly, and playground modules are guaranteed to be post-link optimized. - Keep
-O3without--convergeor-Oz.-O3with--one-caller-inline-max-function-size=100runs in ~6 s (~2 s once FPC avoids giant state-machine functions) and beats-Oson AWFY throughput (~1.6% faster) for <15 KB brotli-11 difference;--convergetook ~96 s for ~3 KB brotli-11.
Consequences
-
On the ICU-linked
--prodGocciaRunnerwith nativeexnrefexceptions (fpc-toolchain3.3.1+13, Node.js v24.18.0 WASI, interleaved A/B):Metric wasm-ldonly+ wasm-ld -O2 + wasm-optDelta GocciaRunnerraw size16,048,697 B 12,111,376 B -24.53% (-3.94 MB) GocciaRunnergzip-9 size3,944,572 B 3,341,643 B -15.29% (-603 KB) GocciaRunnerbrotli-11 size2,607,601 B 2,307,231 B -11.52% (-300 KB) GocciaTestRunnerraw size15,928,192 B 12,069,012 B -24.23% (-3.86 MB) GocciaTestRunnerbrotli-11 size2,578,644 B 2,294,575 B -11.02% (-284 KB) Cold WebAssembly.compile(11 reps)7.02 ms 5.31 ms -24.3% Cold _startempty script (11 reps)35.13 ms 29.90 ms -14.9% Cold GocciaRunner --output=jsontotal (11 reps)28.41 ms 24.19 ms -14.8% Warm instantiate + _startempty script (21 reps)7.03 ms 6.54 ms -6.9% AWFY 14-workload steady-state geomean (5 reps, Sievewarmed)1.000x 1.077x +7.7% (all 14 faster; 13 non- Sieve+7.1%)Prior to ADR 0255 (
fpc-toolchain3.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.compileby 21%, cold_startby 11.9%, and improved 7-benchmark AWFY geomean by 5.2%. -
Stripping
target_featuresafter optimization preserves nativeexnref/try_tableexceptions and reference types: the entirewasm32-wasip1test suite (scripts/check-wasm-modules.js,scripts/run-wasm-test-suite.js, and the website playground real-module runner tests includingtry_tableunwind checks) runs directly against thewasm-opt-optimized modules in CI. -
Local contributors without
wasm-optinstalled can still build--prodmodules (getting thewasm-ldflags and a clear notice), or runbash scripts/install-binaryen.sh build/binaryento match CI byte-for-byte.