diff --git a/quest/m2/README.md b/quest/m2/README.md index 5d89d936f..4d66e141c 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -27,7 +27,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md); ## Required - [Byte slices without objects](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` allocates nothing, neither a chunk view nor a `ByteSlice` -- [An A/A control that measures noise](/quest/m2/ts-bench-aa.md) - `just ts bench`'s A/A measures only noise, in every engine - [Time Timestamp's checked_add and Ord](/quest/m2/ts-bench-timestamp.md) - the `u128` paths #89 sped up get a Chrome timing - [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - every value that drops in moq-net and kio drops where Rust drops it - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures diff --git a/quest/m2/ts-bench-aa.md b/quest/m2/ts-bench-aa.md deleted file mode 100644 index 0ba45206e..000000000 --- a/quest/m2/ts-bench-aa.md +++ /dev/null @@ -1,23 +0,0 @@ -# [S] An A/A control that measures noise - -## Goal - -`just ts bench`'s `A/A` route times the same code as `translated` with nothing else -different, so its spread is the run's noise on every engine, Chrome, Firefox, and Bun. - -## Plan - -Found by [#87](https://github.com/kixelated/rust2/pull/87) and -[#97](https://github.com/kixelated/rust2/pull/97) on 2026-10-27: - -- In JavaScriptCore, the `A/A` bundle and a route's bundle with identical text appear - to share type profiles, so a call site polymorphic in one is polymorphic in both. A - route that differs from `A/A` in text is then not directly comparable with it. -- In Firefox, the handoff's `A/A` runs 6-13% faster than `translated` on main, because - `translated` shares its module with `spawned` and `A/A` does not. - -Give every route the same module layout and run each in its own process or realm, so -neither shares state with another. Show the `A/A` spread before and after on each -engine. - -Public API: none. diff --git a/ts/README.md b/ts/README.md index c3b5b804a..cdfd211af 100644 --- a/ts/README.md +++ b/ts/README.md @@ -395,8 +395,9 @@ helpers in `bench/iter.ts`, a sorted array in `bench/sorted.ts`, and `bigint` in native Rust's first. The JS routes run minified, the same bundles whose size it reports, in Node, Bun, Chrome, and Firefox, with the WASM route's copies across the boundary timed apart from its compute, and an A/A copy of the -translation to show the noise. Each round runs the JS routes and workloads in a new -random order, so no route keeps a slot that runs it faster or slower than identical code +translation to show the noise. Each route runs alone in a worker of its own, since routes +in one realm share type profiles and run slower loaded second. Each round runs the JS +routes and workloads in a new random order, so no route keeps a slot that runs it faster or slower than identical code in another; the report records the seed. It also records the host and engine versions, and each route's bundle size raw and gzipped. It sets no threshold; the nightly workflow runs it and posts the report to its summary. Each browser's page reports each step it diff --git a/ts/bench/entry.ts b/ts/bench/entry.ts index e77dc1fbb..d443b1c60 100644 --- a/ts/bench/entry.ts +++ b/ts/bench/entry.ts @@ -1,8 +1,9 @@ // One engine's run of the benchmark, bundled by `main.ts` beside the files it loads: in -// Node and Bun it prints the result, and in a browser it posts it back to `main.ts`. +// Node and Bun it prints the result, and in a browser it posts it back to `main.ts`. Each +// route runs in a worker of its own, which loads this bundle again to serve `measure`. import { Wasm } from "../tests/wasm.ts"; -import { measure, type Route } from "./time.ts"; +import { type Link, type Load, measure, type Reply, type Subject, serve } from "./time.ts"; import type { Workload } from "./workloads.ts"; type Workloads = { name: string; op: Workload["op"]; items: number; input: string }[]; @@ -18,14 +19,16 @@ export interface Config { route?: string; } -const browser = typeof window !== "undefined"; +// A page has a `window` and its workers a `WorkerGlobalScope`; Node and Bun have neither. +const browser = typeof window !== "undefined" || typeof WorkerGlobalScope !== "undefined"; +const threads = browser ? undefined : await import("node:worker_threads"); +const worker = browser ? typeof window === "undefined" : !threads?.isMainThread; // Tells a browser's `main.ts`, which fails the run when it stops hearing, what runs next. A // beacon leaves no response for the page to handle between samples. const progress = browser ? (step: string) => void navigator.sendBeacon("/progress", step) : () => {}; async function load(name: string): Promise { - progress(`loading ${name}`); const url = new URL(name, import.meta.url); if (url.protocol === "file:") { const fs = await import("node:fs/promises"); @@ -34,11 +37,61 @@ async function load(name: string): Promise { return new Uint8Array(await (await fetch(url)).arrayBuffer()); } -/** A bundled route, whose `run` (or `exported`) returns its output as `view` reads it. */ -async function route(name: string, file: string, view: (out: T) => Uint8Array, exported = "run"): Promise { - progress(`loading ${file}`); - const run = (await import(new URL(file, import.meta.url).href))[exported]; - return { name, run, output: async (input) => view(await run(input)) }; +/** A worker running this bundle, which serves `measure`. */ +function spawn(): Link { + const arrived: Reply[] = []; + const waiting: PromiseWithResolvers[] = []; + let failed: Error | undefined; + const settle = () => { + while (waiting.length > 0 && (failed || arrived.length > 0)) { + const next = waiting.shift() as PromiseWithResolvers; + const reply = arrived.shift(); + if (!reply) next.reject(failed); + else if ("error" in reply) next.reject(new Error(reply.error)); + else next.resolve(reply.value); + } + }; + const receive = (reply: Reply) => { + arrived.push(reply); + settle(); + }; + const fail = (err: Error) => { + failed = err; + settle(); + }; + if (threads) { + const w = new threads.Worker(new URL(import.meta.url)); + w.on("message", receive); + w.on("error", fail); + return { post: (load) => w.postMessage(load), reply: () => next(), close: () => void w.terminate() }; + } + const w = new Worker(import.meta.url, { type: "module" }); + w.onmessage = (e: MessageEvent) => receive(e.data); + w.onerror = (e) => fail(new Error(`a worker failed: ${e.message}`)); + return { post: (load) => w.postMessage(load), reply: () => next(), close: () => w.terminate() }; + + function next(): Promise { + const next = Promise.withResolvers(); + waiting.push(next); + settle(); + return next.promise; + } +} + +/** A worker's side: it loads one route, then times it as `measure` asks. */ +function work() { + // Without isolation, a browser coarsens performance.now() to as much as a millisecond. + if (browser && !crossOriginIsolated) throw new Error("the worker is not cross-origin isolated"); + const reply = (reply: Reply) => (threads ? threads.parentPort?.postMessage(reply) : postMessage(reply)); + const start = (request: Load) => + serve( + request, + reply, + (file) => import(new URL(file, import.meta.url).href), + async (file) => new Wasm(await WebAssembly.compile(await load(file))), + ); + if (threads) threads.parentPort?.once("message", start); + else onmessage = (e: MessageEvent) => start(e.data); } function engine(): string { @@ -55,36 +108,41 @@ function parse(workloads: Workloads): Workload[] { async function main() { // Without isolation, a browser coarsens performance.now() to as much as a millisecond. if (browser && !crossOriginIsolated) throw new Error("the page is not cross-origin isolated"); + progress("loading workloads.json"); const config: Config = JSON.parse(new TextDecoder().decode(await load("workloads.json"))); - const workloads = parse(config.workloads); + const options = { ...config, progress }; + // Each route is a file of its own, so no cache keyed by URL serves one route's code to + // another, and runs in a worker of its own. The A/A route is the translation again: its + // spread from the first is the measurement's noise. + const route = (route: string, file: string, exported = "run", slice = false): Subject => ({ route, file, exported, slice }); const wasm = new Wasm(await WebAssembly.compile(await load("bench.wasm"))); - // The A/A route is the translation again, loaded as its own module: its spread from the - // first is the measurement's noise. - const translated = (out: { data: Uint8Array; length: number }) => out.data.subarray(0, out.length); const routes = [ - await route("hand-written", "moq.js", (out: Uint8Array) => out), - await route("translated", "translated.js", translated), - await route("A/A", "translated-aa.js", translated), + { wasm: "bench.wasm" }, + route("hand-written", "moq.js"), + route("translated", "translated.js", "run", true), + route("A/A", "translated-aa.js", "run", true), ]; - if (config.route) routes.push(await route(config.route, "route.js", translated)); - const timings = await measure(wasm, routes, workloads, { ...config, progress }); + if (config.route) routes.push(route(config.route, "route.js", "run", true)); + const timings = await measure(wasm, routes, parse(config.workloads), spawn, options); // The async handoff: the translation polled at once by a one-task executor, as the // native and WASM routes are, and on the runtime's microtask executor. - const bytes = (out: Uint8Array) => out; const handoffRoutes = [ - await route("hand-written", "task-hand.js", bytes, "handWritten"), - await route("translated", "task-translated.js", bytes), - await route("spawned", "task-translated.js", bytes, "spawn"), - await route("A/A", "task-translated-aa.js", bytes), + { wasm: "task.wasm" }, + route("hand-written", "task-hand.js", "handWritten"), + route("translated", "task-translated.js"), + route("spawned", "task-spawned.js", "spawn"), + route("A/A", "task-translated-aa.js"), ]; - if (config.route) handoffRoutes.push(await route(config.route, "task-route.js", bytes)); + if (config.route) handoffRoutes.push(route(config.route, "task-route.js")); const taskWasm = new Wasm(await WebAssembly.compile(await load("task.wasm"))); - timings.push(...(await measure(taskWasm, handoffRoutes, parse(config.handoffs), { ...config, progress }))); + timings.push(...(await measure(taskWasm, handoffRoutes, parse(config.handoffs), spawn, options))); return { engine: engine(), timings }; } -if (browser) { +if (worker) { + work(); +} else if (browser) { const post = (path: string, body: string) => fetch(path, { method: "POST", body }); main().then( (result) => post("/result", JSON.stringify(result)), diff --git a/ts/bench/main.ts b/ts/bench/main.ts index 76f19d6af..65edaf249 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -151,6 +151,7 @@ await Promise.all([ Bun.write(join(out, "bench.wasm"), wasmBytes), Bun.write(join(out, "task-hand.js"), taskHandBundle), Bun.write(join(out, "task-translated.js"), taskTranslation), + Bun.write(join(out, "task-spawned.js"), taskTranslation), Bun.write(join(out, "task-translated-aa.js"), taskTranslation), Bun.write(join(out, "task.wasm"), taskWasmBytes), Bun.write(join(out, "package.json"), '{ "type": "module" }\n'), @@ -370,7 +371,7 @@ function render(): string { "`js/net`, called as its own benchmarks call it, for `iter/` the same chain with ES", "iterator helpers (`bench/iter.ts`), for `btree/` a sorted array (`bench/sorted.ts`), and", "for `time/` `bigint`, as moq's `js/hang` converts timescales (`bench/rescale.ts`).", - "`A/A` is the translation loaded a second time: its", + "Each JS route and `wasm` runs alone in a worker of its own. `A/A` is the translation again: its", "gap from `translated` is the noise. A `reads` decode is the frame headers above as they", "arrive that many bytes at a time, so a header cut by a read comes up short and is", "decoded again on the next: its gap from the whole decode is the error path's price.", diff --git a/ts/bench/time.ts b/ts/bench/time.ts index 2cba64539..2f53a1daf 100644 --- a/ts/bench/time.ts +++ b/ts/bench/time.ts @@ -1,16 +1,59 @@ // Times every workload on every route in one engine, after checking that each route's // output matches the wasm32 build's, which is native Rust's. The WASM route is timed in // three parts: copying the input in, running, and copying the output out. +// +// Each route runs alone in a realm of its own, a worker, which `measure` drives one +// sample at a time. Routes in one realm share its state: JavaScriptCore merges the type +// profiles of modules with the same text, a module two routes call sees both routes' +// types, and V8 ran a route 2-12% slower loaded after an identical copy than before it. import type { Wasm } from "../tests/wasm.ts"; import type { Workload } from "./workloads.ts"; -export interface Route { - name: string; - /** Run a workload, as timed: synchronously, or as a promise. */ - run(input: Uint8Array): unknown; - /** Run a workload for its output. */ - output(input: Uint8Array): Promise; +/** What one realm times: a route's bundle, or the wasm32 build's three steps. */ +export type Subject = + | { + route: string; + /** The bundle, beside `entry.js`. */ + file: string; + /** The export that runs a workload. */ + exported: string; + /** Whether `exported` returns the translation's `{ data, length }` rather than bytes. */ + slice: boolean; + } + | { wasm: string }; + +/** What a realm is told first: what to load, and where later requests arrive. */ +export interface Load { + subject: Subject; + workloads: Workload[]; + sampleMs: number; + /** + * Each later request: a count `measure` bumps, the op, and its argument. The realm + * blocks on it in `Atomics.wait` rather than going back to its event loop, where the + * engine's idle-time work spread Firefox's samples of one body up to 2.5x apart. + */ + control: Int32Array; +} + +/** A realm's replies: the bodies it loaded, then one per request. */ +export type Reply = { value: unknown } | { error: string }; + +/** A worker, as `measure` drives it. */ +export interface Link { + post(load: Load): void; + /** The next reply, rejected if the worker fails. */ + reply(): Promise; + close(): void; +} + +const enum Op { + /** The route's output on a workload, in hex. */ + Output, + /** Warm a body up, answering with its calls per sample. */ + Warm, + /** One sample of a body, in milliseconds. */ + Sample, } export interface Options { @@ -42,7 +85,6 @@ interface Body { call: () => unknown; async: boolean; calls: number; - ns: number[]; } // Holds the last result, so no engine can drop a call as dead. @@ -52,60 +94,156 @@ export function hex(bytes: Uint8Array): string { return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); } -export async function measure(wasm: Wasm, routes: Route[], workloads: Workload[], options: Options): Promise { - const { progress } = options; - const bodies: Body[] = []; - for (const workload of workloads) { - const { input } = workload; - const want = wasm.call(input); - if (want === "panic") throw new Error(`${workload.name} ${workload.op} panics in native Rust`); - for (const route of routes) { - progress(`checking ${route.name} on ${workload.name} ${workload.op}`); - const got = hex(await route.output(input)); - if (got !== hex(want)) { - throw new Error(`${route.name} differs from native Rust on ${workload.name} ${workload.op}:\nnative ${hex(want)}\ngot ${got}`); +/** Times `workloads` on each subject, each in a worker `spawn` starts for it alone. */ +export async function measure( + wasm: Wasm, + subjects: Subject[], + workloads: Workload[], + spawn: () => Link, + options: Options, +): Promise { + const { progress, sampleMs } = options; + const links: Link[] = []; + try { + type Realm = (op: Op, arg: number) => Promise; + const realms: Realm[] = []; + const bodies: { realm: Realm; index: number; workload: Workload; route: string; calls: number; ns: number[] }[] = []; + for (const subject of subjects) { + progress(`loading ${"wasm" in subject ? subject.wasm : subject.file}`); + const link = spawn(); + links.push(link); + const control = new Int32Array(new SharedArrayBuffer(12)); + link.post({ subject, workloads, sampleMs, control }); + const list = (await link.reply()) as { route: string; workload: number }[]; + const realm = (op: Op, arg: number) => { + control[1] = op; + control[2] = arg; + Atomics.add(control, 0, 1); + Atomics.notify(control, 0); + return link.reply(); + }; + realms.push(realm); + list.forEach(({ route, workload }, index) => { + bodies.push({ realm, index, workload: workloads[workload], route, calls: 0, ns: [] }); + }); + } + + for (const [i, workload] of workloads.entries()) { + const want = wasm.call(workload.input); + if (want === "panic") throw new Error(`${workload.name} ${workload.op} panics in native Rust`); + for (const [r, subject] of subjects.entries()) { + if (!("route" in subject)) continue; + progress(`checking ${subject.route} on ${workload.name} ${workload.op}`); + const got = await realms[r](Op.Output, i); + if (got !== hex(want)) { + throw new Error(`${subject.route} differs from native Rust on ${workload.name} ${workload.op}:\nnative ${hex(want)}\ngot ${got}`); + } } } - let len = 0; - const body = (route: string, call: () => unknown, setup?: () => void) => - bodies.push({ workload, route, setup, call, async: false, calls: 1, ns: [] }); - body("wasm", () => wasm.run(), () => wasm.input(input)); - body("wasm copy-in", () => wasm.input(input)); - body("wasm copy-out", () => wasm.output(len), () => { - wasm.input(input); - len = wasm.run(); - }); - for (const route of routes) body(route.name, () => route.run(input)); + for (const body of bodies) { + progress(`warming up ${body.route} on ${body.workload.name} ${body.workload.op}`); + body.calls = (await body.realm(Op.Warm, body.index)) as number; + } + + // Each round runs the bodies in a new random order, so drift spreads over all of them + // and no body keeps a slot: a fixed order runs identical code slower in one slot than + // in another. Progress is per round: a report per sample slows Chrome's samples by + // about 10%. + const span = `${workloads[0].name} through ${workloads[workloads.length - 1].name}`; + const random = prng(options.seed); + const order = [...bodies]; + for (let round = 0; round < options.rounds; round++) { + progress(`timing round ${round + 1} of ${options.rounds} on ${span}`); + shuffle(order, random); + for (const body of order) { + const ms = (await body.realm(Op.Sample, body.index)) as number; + body.ns.push((ms * 1e6) / (body.calls * body.workload.items)); + } + } + + return bodies.map(({ workload, route, calls, ns }) => ({ workload: workload.name, op: workload.op, route, calls, ns })); + } finally { + for (const link of links) link.close(); + } +} + +/** + * A realm's side of `measure`: loads `load.subject`, importing a route's bundle with + * `bundle` and instantiating the wasm32 build with `wasm`, then answers each request + * through `reply` until the worker is closed. + */ +export async function serve( + load: Load, + reply: (reply: Reply) => void, + bundle: (file: string) => Promise>, + wasm: (file: string) => Promise, +): Promise { + const { subject, workloads, sampleMs, control } = load; + const bodies: Body[] = []; + const body = (workload: Workload, route: string, call: () => unknown, setup?: () => void) => + bodies.push({ workload, route, setup, call, async: false, calls: 1 }); + let output: ((workload: number) => Promise) | undefined; + try { + if ("wasm" in subject) { + const instance = await wasm(subject.wasm); + for (const workload of workloads) { + const { input } = workload; + let len = 0; + body(workload, "wasm", () => instance.run(), () => instance.input(input)); + body(workload, "wasm copy-in", () => instance.input(input)); + body(workload, "wasm copy-out", () => instance.output(len), () => { + instance.input(input); + len = instance.run(); + }); + } + } else { + const run = (await bundle(subject.file))[subject.exported] as (input: Uint8Array) => unknown; + const view = subject.slice + ? (out: { data: Uint8Array; length: number }) => out.data.subarray(0, out.length) + : (out: Uint8Array) => out; + output = async (i) => view((await run(workloads[i].input)) as never); + for (const workload of workloads) body(workload, subject.route, () => run(workload.input)); + } + } catch (err) { + return reply(failure(err)); } + reply({ value: bodies.map((b) => ({ route: b.route, workload: workloads.indexOf(b.workload) })) }); - // Find each body's calls per sample, which also warms it up. - for (const body of bodies) { - progress(`warming up ${body.route} on ${body.workload.name} ${body.workload.op}`); - body.setup?.(); - const first = body.call(); - body.async = first instanceof Promise; - await first; - while ((await sample(body)) < options.sampleMs) body.calls *= 2; + async function answer(op: Op, arg: number): Promise { + switch (op) { + case Op.Output: + if (!output) throw new Error("the wasm32 build is the reference, which has no output to check"); + return hex(await output(arg)); + case Op.Warm: { + const body = bodies[arg]; + body.setup?.(); + const first = body.call(); + body.async = first instanceof Promise; + await first; + while ((await sample(body)) < sampleMs) body.calls *= 2; + return body.calls; + } + case Op.Sample: + return sample(bodies[arg]); + } } - // Each round runs the bodies in a new random order, so drift spreads over all of them - // and no body keeps a slot: a fixed order runs identical code slower in one slot than - // in another. Progress is per round: a report per sample slows Chrome's samples by - // about 10%. - const span = `${workloads[0].name} through ${workloads[workloads.length - 1].name}`; - const random = prng(options.seed); - const order = [...bodies]; - for (let round = 0; round < options.rounds; round++) { - progress(`timing round ${round + 1} of ${options.rounds} on ${span}`); - shuffle(order, random); - for (const body of order) { - const ms = await sample(body); - body.ns.push((ms * 1e6) / (body.calls * body.workload.items)); + // Each wait blocks the worker in this loop, and each `await` goes back only to the + // microtask queue, which is all an async body needs: the event loop never runs again. + for (let seen = 0; ; ) { + Atomics.wait(control, 0, seen); + seen = Atomics.load(control, 0); + try { + reply({ value: await answer(control[1], control[2]) }); + } catch (err) { + reply(failure(err)); } } +} - return bodies.map(({ workload, route, calls, ns }) => ({ workload: workload.name, op: workload.op, route, calls, ns })); +function failure(err: unknown): Reply { + return { error: err instanceof Error ? (err.stack ?? err.message) : String(err) }; } /** Milliseconds for one sample of `body.calls` calls. */