Browse documentation

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, --prod GocciaRunner with ICU4C):

    • Without wasm-opt: the two largest functions (Execute and FastLoop) shrink from 58,200 lines (2 br_table) and 28,283 lines (1 br_table) to 40,352 lines (5 br_table) and 18,821 lines (5 br_table).
    • With wasm-opt: the two dispatch functions shrink by 46% and 50%, from 41,052 lines (2 br_table) and 20,326 lines (1 br_table) to 22,161 lines (5 br_table) and 10,078 lines (5 br_table).
  • Module size (bytes; --prod with ICU4C linked, gzip -9, brotli -q 11; qjs-wasi.wasm v0.18.0 without ICU shown for scale):

    Module3.3.1+133.3.1+14Delta
    GocciaRunner (wasm-opt) raw12,111,37612,023,487-0.73% (-87,889 B)
    GocciaRunner (wasm-opt) gzip-93,272,3333,239,256-1.01% (-33,077 B)
    GocciaRunner (wasm-opt) brotli-112,309,6472,285,488-1.05% (-24,159 B)
    GocciaRunner (GOCCIA_WASM_OPT=0) raw15,443,23615,347,155-0.62% (-96,081 B)
    GocciaRunner (GOCCIA_WASM_OPT=0) brotli-112,587,1272,558,316-1.11% (-28,811 B)
    GocciaTestRunner (wasm-opt) raw12,069,01211,981,123-0.73% (-87,889 B)
    GocciaTestRunner (wasm-opt) brotli-112,297,1402,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.0 WASI, medians and min–max):

    Metric3.3.1+13 (wasm-opt)3.3.1+14 (wasm-opt)Deltaqjs-wasi.wasm
    WebAssembly.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=json timing.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 from 183.14 ms (179.85–185.35) to 64.49 ms (63.38–66.54, -64.8%), and --output=json timing.total_ns drops from 28.37 ms to 22.44 ms (-20.9%).

  • AWFY execution (14 workloads, perf/awfy/manifest.json warm protocol, 1 warmup + 5 interleaved alternating reps, medians in us):

    Benchmark3.3.1+13 (wasm-opt)3.3.1+14 (wasm-opt)qjs-wasi.wasmb13 / b14b14 / qjsb14 / native
    Bounce1,835.2 (1,818–1,846)425.4 (425–431)680.5 (670–684)4.31x0.63x0.965x
    CD11,112.9 (11,068–11,275)2,826.4 (2,810–2,827)5,514.8 (5,454–5,550)3.93x0.51x0.906x
    DeltaBlue332.1 (331–335)92.1 (92–94)178.0 (174–179)3.61x0.52x0.897x
    Havlak4,599,775.8 (4.57–4.62M)1,380,893.8 (1.38–1.40M)2,239,771.4 (2.22–2.26M)3.33x0.62x0.849x
    Json20,845.8 (20,765–21,038)5,792.2 (5,774–5,850)7,411.9 (7,374–7,454)3.60x0.78x1.035x
    List1,759.5 (1,749–1,765)363.4 (360–368)670.0 (668–682)4.84x0.54x1.107x
    Mandelbrot4.476 (4.425–4.536)0.383 (0.378–0.385)0.820 (0.813–0.826)11.68x0.47x1.029x
    NBody20.2 (20.1–20.3)5.6 (5.6–5.7)9.5 (9.4–9.5)3.58x0.59x0.996x
    Permute3,087.1 (3,078–3,102)769.1 (754–786)1,104.5 (1,098–1,110)4.01x0.70x0.890x
    Queens2,200.9 (2,200–2,221)520.5 (514–529)747.3 (743–751)4.23x0.70x0.981x
    Richards84,262.9 (83,531–84,449)21,994.5 (21,763–22,302)30,467.1 (29,740–30,614)3.83x0.72x0.924x
    Sieve2,316.3 (2,295–2,333)169.3 (168–170)384.8 (384–387)13.68x0.44x0.823x
    Storage2,478.6 (2,471–2,496)938.4 (933–948)1,199.4 (1,192–1,204)2.64x0.78x0.988x
    Towers4,973.9 (4,945–5,001)1,155.3 (1,140–1,172)1,496.3 (1,489–1,508)4.31x0.77x0.958x
    Geomean (14)———4.52x0.62x0.951x
    Geomean (13 non-Sieve)———4.15x0.63x0.961x

    b14 / native comes from a separate interleaved run against the darwin/arm64 native interpreter (GOCCIA_JIT=0), where 3.3.1+13 was 4.310x (4.007x excluding Sieve) and 3.3.1+14 is 0.951x (0.961x excluding Sieve; 11 of 14 workloads faster in WebAssembly under V8 TurboFan than in the native FPC arm64 build). Without wasm-opt, 3.3.1+14 is 4.49x faster than 3.3.1+13 (4.17x excluding Sieve), moving GocciaScript-wasm / qjs-wasi.wasm from 2.98x to 0.66x.

  • V8 tier-up falsification checks (wasm-opt modules):

    • Under the default manifest's warmupIterations: 5, 3.3.1+14's smaller FastLoop tiers up to V8 TurboFan by iteration 1–2 while 3.3.1+13 stalls in Liftoff on Sieve and Mandelbrot and qjs-wasi.wasm finishes tiering up partway through short measured windows.
    • Warming all short workloads past TurboFan tier-up while preserving Liftoff PGO (warmupIterations: 30 on Bounce, DeltaBlue, List, Permute, Queens, Sieve, Storage, Towers; 3000 on Mandelbrot and NBody; manifest on CD, Havlak, Json, Richards) yields a 14-workload steady-state geomean speedup of 3.90x (3.8979x) and GocciaScript-wasm / qjs-wasi.wasm moving from 2.66x to 0.68x (1.47x faster than QuickJS-ng WASM, faster on all 14 workloads: 0.48x on Mandelbrot to 0.92x on Storage).
    • 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 a 3.37x (3.3731x) geomean speedup over 3.3.1+13 and 0.80x (1.24x faster) against qjs-wasi.wasm.
  • Playground --timeout path (--timeout=120000, 14 workloads, wasm-opt):

    • Because Execute (the instruction-counting slow dispatch loop used whenever --timeout is set) drops from 41,052 to 22,161 WAT lines with 5 br_tables, 3.3.1+14 speeds up the --timeout path by 8.50x geomean over 3.3.1+13 (6.20x on Storage to 10.81x on CD; Havlak 20.75 s -> 2.25 s), moving GocciaScript-wasm (--timeout) / qjs-wasi.wasm (no timeout) from 11.52x to 1.35x.
    • On 3.3.1+14 directly interleaved (--timeout=120000 vs no timeout), the --timeout slow path costs 2.20x geomean (1.59x on Json to 3.51x on Sieve).
  • Native neutrality (darwin/arm64, 14 AWFY workloads, interleaved 3.3.1+13 vs 3.3.1+14):

    • Geomean 3.3.1+14 / 3.3.1+13 is 1.0000x (default) and 0.9965x (GOCCIA_JIT=0), confirming zero native regression (patch 0155 touches only compiler/wasm32/).
  • Full test suites and Win32 SafeSEH audit:

    • Native JS suite (2,067 files): 35,640 passed, 0 failed, 12 skipped (both default and GOCCIA_JIT=0).
    • Full WebAssembly test suite (scripts/run-wasm-test-suite.js, --icu-data, 2,067 files): identical outcomes (2,032 passed, 35 failed as listed in tests/wasm-known-failures.txt, 0 unexpected, 0 stale), wall-clock time dropping from 75 s (3.3.1+13 wasm-opt) to 28 s (3.3.1+14 wasm-opt, -62.7% / 2.68x faster). scripts/check-wasm-modules.js and the website browser-runner tests pass.
    • Because the --timeout path 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) uses Atomics.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=50000000 cap first.
    • Win32 SafeSEH audit: all 142 audited i386-win32 objects (140 object + 2 required-object) in 3.3.1+14 match their 3.3.1+13 SHA-256 hashes; ./safeseh.pas --digests, FPCSafeSEH.Test, PESafeSEH.Test, FPCToolchain.Test, and local i386-win32 --prod builds pass.

Decision

  • Pin fpc-toolchain 3.3.1+14 (3.3.1-b14) in toolchain.lock, all CI workflows (615e0f1dade0e24941271a1f57115b6b2caad5e2), and config/win32-safeseh/fpc-3.3.1.manifest.
  • Keep -CTwasmexceptions (ADR 0255) and Binaryen wasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-features (ADR 0256) unchanged. Both compose cleanly with patch 0155.

Consequences

  • Shipped --prod WebAssembly modules run 3.90x faster in fully warmed steady state (4.52x under the default AWFY manifest protocol), beating qjs-wasi.wasm v0.18.0 across all 14 AWFY workloads (0.68x steady-state, 0.62x manifest) and reaching 0.95x of the native darwin/arm64 interpreter.
  • Playground runs with --timeout speed up 8.50x (11.52x -> 1.35x relative to untimed qjs-wasi.wasm), cold Node WASI subprocess startup drops 67.8% (187 ms -> 60 ms), the full 2,067-file WebAssembly test suite drops from 75 s to 28 s, and --prod brotli-11 module sizes shrink a further 1.05%–1.15% (~24–26 KB).
  • Native targets and Win32 SafeSEH object hashes are unchanged (3.3.1+10 object audit continues to hold).