Playground and measurement tools
Experiments on Run in the browser and on the benchmark tools.
This page records the experiments of the last days on the playground (Run of edited programs in the browser) and on the benchmark tools. Each section gives what was tried, why, the measurement, the verdict and the next step. Times are wall clock. "Cold" is a fresh browser profile. "Reload" is a new visit with the HTTP cache warm. "Same page" is a second edit in an open page.
Run edited programs in the browser
What. The playground builds an edited program in the page, with no server. The front end (marmot-play.wasm) makes the program's MLIR. The backend makes an object and links it with a prebuilt standard library (std-dev.a) and the runtime. It is compiled to wasm with Emscripten, together with wasm-ld in one module of about 25 MB. Binaryen.js then adds goroutine switching. The program runs in a Web Worker with a WASI host.
Why. Before this, Run worked only on the built-in examples, which are built ahead.
Measurement. Node first, then Chrome and Firefox (headless). The backend objects built in wasm are byte-identical to the native backend's objects. The standard library is built by separate compilation (deploy/play/stdobj), with -trimpath. The build fails if a published file holds a directory of the build machine. In the page, goroutine stacks are 16 KiB (MARMOT_STACKSIZE), so 100000 goroutines fit in the 4 GiB of wasm memory. With the default 64 KiB they do not.
Verdict. Shipped.
Next step. None. The sections below make it faster.
JSPI for goroutine switches in the page
What. Some browsers have JavaScript Promise Integration (JSPI). In them, the page links the JSPI runtime and runs binaryen's spill-pointers pass, instead of the Asyncify runtime and the Asyncify pass. The page tests for WebAssembly.Suspending and WebAssembly.promising. Other browsers get Asyncify. A JSPI build that does not start is built again with Asyncify, and the Output drawer says so.
Why. The Asyncify pass was the largest step of a Run.
Measurement. Binaryen.js on json+sort (a 3.2 MB linked module), node: the Asyncify pass took 7.1 to 9.8 s (output 8.9 MB). Spill-pointers took 0.94 to 1.2 s (output 3.9 MB). On the live site, 10 Runs per path, click to output, Run 3 s after the edit:
| Path | Cold, median | Reload, median |
|---|---|---|
| Chrome, JSPI | 2.9 s | 1.6 s |
| Firefox 160, JSPI | 2.1 s | 2.3 s |
| Chrome, Asyncify (forced) | 8.7 s | 5.5 s |
A second edit in the same page takes 0.9 s with JSPI and about 4 s with Asyncify (Chrome). Firefox 160 has JSPI on by default. Older Firefox and Safari use Asyncify: 13 to 25 s in a Firefox build without JSPI. With the engine stack pool of the JSPI runtime, 100000 goroutines run in 344 ms. Without the pool they took 3.2 to 5.4 s. 25 programs (the examples, the GC tests, a panic and an os.Exit on a goroutine other than main) give the same output with both runtimes.
Verdict. Shipped, on by default where the browser has JSPI.
Next step. None for now.
Make the Asyncify pass smaller
What. Four attempts to make Asyncify cheaper for browsers without JSPI. They are a remove-list of runtime functions, asyncify-ignore-indirect, an only-list from a call graph, and a standard library transformed ahead of time.
Why. Asyncify is still 5 to 25 s in browsers without JSPI.
Measurement. The remove-list gave 3% on json+sort. With asyncify-ignore-indirect the pass took as long as without it (6.3 against 6.6 s). The reason: on wasm, the collector gets every frame's locals into memory by an unwind (rt_spill_regs), from gc_collect and from the allocation slow path. So every function that can allocate reaches a switch through direct calls. ignore-indirect is not safe for Go in any case: goroutine entry, closures, defers and interface calls are indirect calls into code that parks. An only-list from the call graph, with indirect calls resolved by signature, kept 3782 of 4452 functions instrumented, against 3812 by default. A standard library transformed ahead of time is not possible: Asyncify works on the linked module only.
Verdict. Dropped.
Next step. A call graph from the compiler with Go type information (closure and method targets per call site) could give a safe remove-list. The upper bound is about 700 of 4450 functions. The real gain is unknown.
Warm binaryen.js before the first Run in Firefox
What. Run an Asyncify pass on a small module while the user types, so that Firefox compiles binaryen.js to optimized code before the real pass.
Why. An early measurement showed a second pass twice as fast as the first.
Measurement. In a Firefox build without JSPI, three passes on one module took 36, 27 and 39 s without a warm-up. After a warm-up they took 22, 33 and 23 s. That is noise. The early "twice as fast" compared two programs of different size.
Verdict. Dropped.
Next step. None.
A multithreaded binaryen.js
What. Build binaryen.js with threads. The page is cross-origin isolated, so SharedArrayBuffer works, and Asyncify runs function by function.
Why. It could make the Asyncify pass faster in browsers without JSPI.
Measurement. Not built.
Verdict. Not done. With JSPI in current Firefox, it would help only older Firefox and Safari.
Next step. Do it only if Safari speed becomes a goal.
Load the build tools on the first edit
What. The first edit tells the compiler's Web Worker to load the build tools. The worker loads the front end, compiles the backend module and imports play-tools.mjs and binaryen.js. It also fetches the standard library and the runtime. A Run then waits only for what is not loaded yet. A visit without an edit downloads none of it.
Why. A first Run waited for about 25 MB of downloads and a 25 MB wasm compile.
Measurement. Local server behind a 20 Mbit/s proxy, Chrome with JSPI, Run 10 s after the first edit. Click to output takes 3.9 s, against 10.0 s without the preload. On a first visit the Run is limited by the download. The last file (the backend, 9.4 MB compressed) arrives after about 8 s at 20 Mbit/s.
Verdict. Shipped.
Next step. None.
Keep the build tools warm between steps
What. Keep one instance of the build tools between the codegen and the link step, instead of a fresh instance per step.
Why. It looked like a cost per step.
Measurement. A fresh instance starts in 7 to 21 ms.
Verdict. Dropped. There is nothing to gain.
Next step. None.
Smaller downloads
What. The standard library archive, the runtime archive and the runtime bitcode go gzipped (gzip -9). The page unpacks them with DecompressionStream.
Why. The server compresses only known text and wasm types. std-dev.a went over the wire uncompressed.
Measurement. std-dev.a 15.7 MB to 5.4 MB, librtcore.a 269 KB to 75 KB, rtcore.bc 229 KB to 145 KB. A first visit is about 25 MB instead of 35 MB. Brotli at level 11 would make the wasm and JavaScript files smaller than the server's zstd. The backend goes from 9.16 to 6.6 MB, the front end 5.04 to 3.7 MB, binaryen.js 2.24 to 1.4 MB.
Verdict. Gzip shipped. Brotli is not done.
Next step. Test whether precompressed files with a Content-Encoding header work on the static host. If they do, about 8 MB more can go.
Cache the build tools
What. Cache headers for the build tools: a year for the content-hashed files, revalidation for their manifest.
Why. The build tools had no cache rule, so the browser revalidated all 54 MB on each visit.
Measurement. The headers apply on the local server and on the live site.
Verdict. Shipped.
Next step. None.
A build tool that does not start
What. Sometimes the browser cannot make a new instance of the build tools, for example when no memory is left. The build then stops with "the build tools did not start" and the reason.
Why. Before, Emscripten's start promise never settled after a failed instantiate. Run waited at "Generating code" for ever, with no message.
Measurement. Reproduced in Chrome and Firefox by using up the wasm memory reservations of the Web Worker just before the tools start. Without the fix the build waited for ever. With it, the Output drawer shows the error.
Verdict. Shipped.
Next step. None.
A module import that does not settle
What. The page downloads play-tools.mjs and binaryen.js with fetch and a stall timer, then imports the bytes from a blob URL. The timer restarts on each chunk. Only 10 s with no bytes aborts the download and starts it again, up to three times. A slow download is never cut.
Why. In 5 of about 45 runs on a local server, the dynamic import of play-tools.mjs in the Web Worker never settled. Firefox had 4 of them and Chrome 1. The server logged no completed request. A first fix gave each import 10 s in total, which would have broken slow connections: binaryen.js takes 13 s at 1.6 Mbit/s.
Measurement. Behind a throttling proxy: at 1.6 Mbit/s (binaryen.js 13 s) and 800 kbit/s (26 s) there was no retry. Two faults were tried again after 10 s: a binaryen.js body that stops after 64 KB, and no answer to play-tools.mjs. The Run then gave the right output (Chrome and Firefox). On the live site, 40 of 40 Runs passed.
Verdict. Shipped. The cause of the stall (the browser or the local server) is not known.
Next step. None, unless the stall shows on the live site.
CPU isolation for benchmark runs
What. While a timed comparison runs, the benchmark gets CPUs 0-3 and 8-11 (four cores with their second threads). Other compiler jobs get the rest. perfcmp -cpus pins the timed programs to CPUs 0-3. The opt-in is AT_CPUSPLIT=1.
Why. On the benchmark machine the load average is often above 8 (half its 16 CPUs), so perfcmp's load rule reports INCONCLUSIVE. A load log every 30 s during benchmarks showed 11 to 13 cores busy with no benchmark. 8 were compiler jobs (their cap), and 3 to 5 were other work on the machine.
Measurement. A-against-A comparisons (one commit on both sides). Three with the split on: noise 1.026 to 1.035 in dev and 1.007 to 1.022 in release. Rows on identical binaries were marked WORSE, up to 1.138. Other work used 9% to 47% of one benchmark CPU. All three were INCONCLUSIVE. One with the split off: noise 1.011 to 1.012, verdict ok.
Verdict. Opt-in only. Not used for gates. The noise comes from other work on the benchmark CPUs, not from compiler jobs. The split cannot move that work.
Next step. A rule for the load check is still to be decided. The data above is the input.
perfcmp builds first, then waits for a quiet machine
What. perfcmp builds both sides before it times anything. Before the timed runs, it waits until the load is below a quarter of the CPUs (at most 5 minutes). Each timed run uses a fresh copy of the binary, so the page cache does not favor one side in RSS.
Why. Its own builds made the first timed runs noisy.
Measurement. Not measured as a number. The change takes perfcmp's own builds out of the timed window.
Verdict. Shipped.
Next step. None.