WebAssembly builds use fpc-toolchain 3.3.1+14 br_table dispatch
Date: 2026-12-04
Area: build, apps, website
Refines: ADR 0255,
ADR 0256
Related: ADR 0235,
ADR 0244,
ADR 0245
Context
In FreePascal's wasm32 backend before fpc-toolchain 3.3.1+14, any procedure containing a label / goto fell back to turning the entire procedure into a single state-machine switch loop and disabling br_table jump-table emission for its case statements. Both of GocciaScript's bytecode VM dispatch procedures (FastLoop and Execute in Goccia.VM.pas) use labels, so every opcode dispatch lowered to nested state-machine branches inside two oversized WebAssembly functions (41,052 and 20,326 WAT lines even after Binaryen wasm-opt). That caused two compounding costs under V8: expensive per-opcode dispatch and slow background TurboFan tier-up (including stalling short benchmarks such as Sieve and Mandelbrot in Liftoff and holding cold Node WASI subprocesses open for ~187 ms).
fpc-toolchain 3.3.1+14 (release 3.3.1-b14, commit 615e0f1dade0e24941271a1f57115b6b2caad5e2, PR frostney/fpc-toolchain#48) adds patch 0155, which touches only compiler/wasm32/ (aasmcpu.pas, cpupi.pas, nwasmset.pas). It keeps br_table jump tables in procedures that contain labels and lowers goto selectively instead of rewriting the whole procedure as a state machine.
We measured 3.3.1+13 against 3.3.1+14 interleaved on darwin/arm64 (Node.js v24.18.0 WASI, ICU4C 79.1 linked --prod modules, Binaryen version_133 wasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-features, alongside QuickJS-ng v0.18.0 qjs-wasi.wasm and the native darwin/arm64 interpreter):
-
Disassembly (
wasm-tools print,--prodGocciaRunnerwith ICU4C):- Without
wasm-opt: the two largest functions (ExecuteandFastLoop) shrink from58,200lines (2br_table) and28,283lines (1br_table) to40,352lines (5br_table) and18,821lines (5br_table). - With
wasm-opt: the two dispatch functions shrink by46%and50%, from41,052lines (2br_table) and20,326lines (1br_table) to22,161lines (5br_table) and10,078lines (5br_table).
- Without
-
Module size (bytes;
--prodwith ICU4C linked, gzip-9, brotli-q 11;qjs-wasi.wasmv0.18.0 without ICU shown for scale):Module 3.3.1+133.3.1+14Delta GocciaRunner(wasm-opt) raw12,111,376 12,023,487 -0.73% (-87,889 B) GocciaRunner(wasm-opt) gzip-93,272,333 3,239,256 -1.01% (-33,077 B) GocciaRunner(wasm-opt) brotli-112,309,647 2,285,488 -1.05% (-24,159 B) GocciaRunner(GOCCIA_WASM_OPT=0) raw15,443,236 15,347,155 -0.62% (-96,081 B) GocciaRunner(GOCCIA_WASM_OPT=0) brotli-112,587,127 2,558,316 -1.11% (-28,811 B) GocciaTestRunner(wasm-opt) raw12,069,012 11,981,123 -0.73% (-87,889 B) GocciaTestRunner(wasm-opt) brotli-112,297,140 2,270,745 -1.15% (-26,395 B) qjs-wasi.wasm(v0.18.0, no ICU)1,571,399 raw / 544,525 gz-9 / 435,766 br-11 — — -
Startup (empty script, interleaved under
Node.js v24.18.0WASI, medians and min–max):Metric 3.3.1+13(wasm-opt)3.3.1+14(wasm-opt)Delta qjs-wasi.wasmWebAssembly.compile(11 reps)4.59 ms (4.51–4.69) 4.52 ms (4.39–4.58) -1.5% 0.87 ms (0.82–0.88) Cold Node WASI subprocess (11 reps) 187.01 ms (184.80–188.27) 60.28 ms (59.39–61.69) -67.8% (3.10x faster) 28.73 ms (27.24–30.40) Cold --output=jsontiming.total_ns(11 reps)24.30 ms (24.00–24.45) 18.39 ms (18.13–18.80) -24.3% — Warm instantiate + _start(21 reps)6.44 ms (6.10–7.23) 5.80 ms (5.30–6.71) -10.0% 0.78 ms (0.60–0.97) Without
wasm-opt, cold Node WASI subprocess startup similarly drops from183.14 ms(179.85–185.35) to64.49 ms(63.38–66.54,-64.8%), and--output=jsontiming.total_nsdrops from28.37 msto22.44 ms(-20.9%). -
AWFY execution (14 workloads,
perf/awfy/manifest.jsonwarm protocol, 1 warmup + 5 interleaved alternating reps, medians in us):Benchmark 3.3.1+13(wasm-opt)3.3.1+14(wasm-opt)qjs-wasi.wasmb13 / b14b14 / qjsb14 / nativeBounce 1,835.2 (1,818–1,846) 425.4 (425–431) 680.5 (670–684) 4.31x 0.63x 0.965x CD 11,112.9 (11,068–11,275) 2,826.4 (2,810–2,827) 5,514.8 (5,454–5,550) 3.93x 0.51x 0.906x DeltaBlue 332.1 (331–335) 92.1 (92–94) 178.0 (174–179) 3.61x 0.52x 0.897x Havlak 4,599,775.8 (4.57–4.62M) 1,380,893.8 (1.38–1.40M) 2,239,771.4 (2.22–2.26M) 3.33x 0.62x 0.849x Json 20,845.8 (20,765–21,038) 5,792.2 (5,774–5,850) 7,411.9 (7,374–7,454) 3.60x 0.78x 1.035x List 1,759.5 (1,749–1,765) 363.4 (360–368) 670.0 (668–682) 4.84x 0.54x 1.107x Mandelbrot 4.476 (4.425–4.536) 0.383 (0.378–0.385) 0.820 (0.813–0.826) 11.68x 0.47x 1.029x NBody 20.2 (20.1–20.3) 5.6 (5.6–5.7) 9.5 (9.4–9.5) 3.58x 0.59x 0.996x Permute 3,087.1 (3,078–3,102) 769.1 (754–786) 1,104.5 (1,098–1,110) 4.01x 0.70x 0.890x Queens 2,200.9 (2,200–2,221) 520.5 (514–529) 747.3 (743–751) 4.23x 0.70x 0.981x Richards 84,262.9 (83,531–84,449) 21,994.5 (21,763–22,302) 30,467.1 (29,740–30,614) 3.83x 0.72x 0.924x Sieve 2,316.3 (2,295–2,333) 169.3 (168–170) 384.8 (384–387) 13.68x 0.44x 0.823x Storage 2,478.6 (2,471–2,496) 938.4 (933–948) 1,199.4 (1,192–1,204) 2.64x 0.78x 0.988x Towers 4,973.9 (4,945–5,001) 1,155.3 (1,140–1,172) 1,496.3 (1,489–1,508) 4.31x 0.77x 0.958x Geomean (14) — — — 4.52x 0.62x 0.951x Geomean (13 non-Sieve) — — — 4.15x 0.63x 0.961x b14 / nativecomes from a separate interleaved run against thedarwin/arm64native interpreter (GOCCIA_JIT=0), where3.3.1+13was4.310x(4.007xexcludingSieve) and3.3.1+14is0.951x(0.961xexcludingSieve; 11 of 14 workloads faster in WebAssembly under V8 TurboFan than in the native FPC arm64 build). Withoutwasm-opt,3.3.1+14is4.49xfaster than3.3.1+13(4.17xexcludingSieve), movingGocciaScript-wasm / qjs-wasi.wasmfrom2.98xto0.66x. -
V8 tier-up falsification checks (
wasm-optmodules):- Under the default manifest's
warmupIterations: 5,3.3.1+14's smallerFastLooptiers up to V8 TurboFan by iteration 1–2 while3.3.1+13stalls in Liftoff onSieveandMandelbrotandqjs-wasi.wasmfinishes tiering up partway through short measured windows. - Warming all short workloads past TurboFan tier-up while preserving Liftoff PGO (
warmupIterations: 30onBounce,DeltaBlue,List,Permute,Queens,Sieve,Storage,Towers;3000onMandelbrotandNBody; manifest onCD,Havlak,Json,Richards) yields a 14-workload steady-state geomean speedup of3.90x(3.8979x) andGocciaScript-wasm / qjs-wasi.wasmmoving from2.66xto0.68x(1.47xfaster than QuickJS-ng WASM, faster on all 14 workloads:0.48xonMandelbrotto0.92xonStorage). - Disabling Liftoff entirely (
--no-liftoff, 1 warmup + 3 reps, which removes both tier-up differences and V8's Liftoff call-count inlining feedback) still gives a3.37x(3.3731x) geomean speedup over3.3.1+13and0.80x(1.24xfaster) againstqjs-wasi.wasm.
- Under the default manifest's
-
Playground
--timeoutpath (--timeout=120000, 14 workloads,wasm-opt):- Because
Execute(the instruction-counting slow dispatch loop used whenever--timeoutis set) drops from41,052to22,161WAT lines with5br_tables,3.3.1+14speeds up the--timeoutpath by8.50xgeomean over3.3.1+13(6.20xonStorageto10.81xonCD;Havlak20.75 s->2.25 s), movingGocciaScript-wasm (--timeout) / qjs-wasi.wasm (no timeout)from11.52xto1.35x. - On
3.3.1+14directly interleaved (--timeout=120000vs no timeout), the--timeoutslow path costs2.20xgeomean (1.59xonJsonto3.51xonSieve).
- Because
-
Native neutrality (
darwin/arm64, 14 AWFY workloads, interleaved3.3.1+13vs3.3.1+14):- Geomean
3.3.1+14 / 3.3.1+13is1.0000x(default) and0.9965x(GOCCIA_JIT=0), confirming zero native regression (patch0155touches onlycompiler/wasm32/).
- Geomean
-
Full test suites and Win32 SafeSEH audit:
- Native JS suite (
2,067files):35,640passed,0failed,12skipped (both default andGOCCIA_JIT=0). - Full WebAssembly test suite (
scripts/run-wasm-test-suite.js,--icu-data,2,067files): identical outcomes (2,032passed,35failed as listed intests/wasm-known-failures.txt,0unexpected,0stale), wall-clock time dropping from75 s(3.3.1+13wasm-opt) to28 s(3.3.1+14wasm-opt,-62.7%/2.68xfaster).scripts/check-wasm-modules.jsand the website browser-runner tests pass. - Because the
--timeoutpath now executes 50,000,000 bytecode instructions in ~348 ms on fast Apple Silicon, the website browser-worker wall-clock kill test (website/src/__tests__/wasm-runner.test.ts) usesAtomics.wait(...)instead of a tight JS loop so it exercises the 1,000 ms worker kill timer rather than hitting the playground's--max-instructions=50000000cap first. - Win32 SafeSEH audit: all
142auditedi386-win32objects (140object+2required-object) in3.3.1+14match their3.3.1+13SHA-256 hashes;./safeseh.pas --digests,FPCSafeSEH.Test,PESafeSEH.Test,FPCToolchain.Test, and locali386-win32 --prodbuilds pass.
- Native JS suite (
Decision
- Pin
fpc-toolchain3.3.1+14(3.3.1-b14) intoolchain.lock, all CI workflows (615e0f1dade0e24941271a1f57115b6b2caad5e2), andconfig/win32-safeseh/fpc-3.3.1.manifest. - Keep
-CTwasmexceptions(ADR 0255) and Binaryenwasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-features(ADR 0256) unchanged. Both compose cleanly with patch0155.
Consequences
- Shipped
--prodWebAssembly modules run3.90xfaster in fully warmed steady state (4.52xunder the default AWFY manifest protocol), beatingqjs-wasi.wasmv0.18.0 across all 14 AWFY workloads (0.68xsteady-state,0.62xmanifest) and reaching0.95xof the nativedarwin/arm64interpreter. - Playground runs with
--timeoutspeed up8.50x(11.52x->1.35xrelative to untimedqjs-wasi.wasm), cold Node WASI subprocess startup drops67.8%(187 ms->60 ms), the full 2,067-file WebAssembly test suite drops from75 sto28 s, and--prodbrotli-11 module sizes shrink a further1.05%–1.15%(~24–26 KB). - Native targets and Win32 SafeSEH object hashes are unchanged (
3.3.1+10object audit continues to hold).