Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions quest/m2/ts-bench-aa.md

This file was deleted.

5 changes: 3 additions & 2 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
110 changes: 84 additions & 26 deletions ts/bench/entry.ts
Original file line number Diff line number Diff line change
@@ -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 }[];
Expand All @@ -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<Uint8Array> {
progress(`loading ${name}`);
const url = new URL(name, import.meta.url);
if (url.protocol === "file:") {
const fs = await import("node:fs/promises");
Expand All @@ -34,11 +37,61 @@ async function load(name: string): Promise<Uint8Array> {
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<T>(name: string, file: string, view: (out: T) => Uint8Array, exported = "run"): Promise<Route> {
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<unknown>[] = [];
let failed: Error | undefined;
const settle = () => {
while (waiting.length > 0 && (failed || arrived.length > 0)) {
const next = waiting.shift() as PromiseWithResolvers<unknown>;
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<Reply>) => 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<unknown> {
const next = Promise.withResolvers<unknown>();
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<Load>) => start(e.data);
}

function engine(): string {
Expand All @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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.",
Expand Down
Loading