From a6e74775fe00cc2c5fabdd35cbd020844c299a7d Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:24:39 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m2/ts-bench-error-path.md Quest-Claim: 3ec14568ec37aaa8 From 3fed326bdc429c0bcc0781c608eecc12731fde9c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:48:08 -0700 Subject: [PATCH 2/2] feat(ts): time a frame split across reads in the bench Adds a decode of the video frame headers as they arrive 3 bytes at a time, so every header comes up Short at least once and is decoded again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-bench-error-path.md | 14 ------- ts/README.md | 7 ++-- ts/bench/main.ts | 4 +- ts/bench/moq.ts | 37 ++++++++++++++++- ts/bench/workloads.ts | 14 ++++++- ts/fixtures/codec/expected/bench.ts | 64 +++++++++++++++++++++++++++++ ts/fixtures/codec/src/bench.rs | 53 +++++++++++++++++++++++- 8 files changed, 170 insertions(+), 24 deletions(-) delete mode 100644 quest/m2/ts-bench-error-path.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 073b47f38..a1d8b0529 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -27,7 +27,6 @@ reports; [m1](/quest/m1/README.md#plan) records the decision. - [Diverging calls through closures and generics](/quest/m2/ts-never-edges.md) - closures returning `!` and generics at `!` pass `tsc` - [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split - [Wide-integer fast paths](/quest/m2/ts-wide-fast.md) - a fused `from_halves` and an unboxed limb product, kept only if measured faster -- [Error-path workload](/quest/m2/ts-bench-error-path.md) - the benchmark prices `?` on a frame split across reads - [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 - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-bench-error-path.md b/quest/m2/ts-bench-error-path.md deleted file mode 100644 index 7359f9bae..000000000 --- a/quest/m2/ts-bench-error-path.md +++ /dev/null @@ -1,14 +0,0 @@ -# [S] Error-path workload in the TS benchmark - -## Goal - -`just ts bench` times a codec path that fails and retries: a frame split across -reads, decoded again on `Short`, so the cost of `?` reusing the incoming `Err` is -measured against native, WASM, and hand-written routes. - -## Plan - -From [#15](https://github.com/kixelated/rust2/pull/15): every workload today -decodes a complete buffer, so no error path is priced. - -Public API: none. diff --git a/ts/README.md b/ts/README.md index 12b6311b9..82129eeac 100644 --- a/ts/README.md +++ b/ts/README.md @@ -272,9 +272,10 @@ nix develop .#bench --command just ts bench # report to target/bench/report.md ``` The codec fixture's `bench::run` holds the cases moq's own TypeScript benchmarks time -(`js/net/bench/varint.ts` and `lite-varint.ts`), at the pinned moq revision, and -iterator chains over bytes. Each runs four ways: natively, as wasm32 at moq-wasm's -size-optimized profile, hand-written (moq's `js/net` in `bench/moq.ts`, and ES iterator +(`js/net/bench/varint.ts` and `lite-varint.ts`), at the pinned moq revision, the same +frame headers decoded as they arrive a few bytes at a time, which prices a decode that +comes up `Short` and retries, and iterator chains over bytes. Each runs four ways: +natively, as wasm32 at moq-wasm's size-optimized profile, hand-written (moq's `js/net` in `bench/moq.ts`, and ES iterator helpers in `bench/iter.ts`), and translated; every route's output must match native Rust's first. The JS routes run in Node, Bun, and Chrome, with the WASM route's copies across the boundary timed apart from its compute, and an A/A copy of the diff --git a/ts/bench/main.ts b/ts/bench/main.ts index 0062e4091..3f640226d 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -313,7 +313,9 @@ function render(): string { "copying the input in and the output out are timed apart. `hand-written` is moq's", "`js/net`, called as its own benchmarks call it, and for `iter/` the same chain with ES", "iterator helpers (`bench/iter.ts`). `A/A` is the translation loaded a second time: its", - "gap from `translated` is the noise.", + "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.", ...(extra ? [`\`${extra.name}\` is the translation in another run's output, given by \`--route\`.`] : []), "", "## Bundle size", diff --git a/ts/bench/moq.ts b/ts/bench/moq.ts index 5aa5d8318..682886876 100644 --- a/ts/bench/moq.ts +++ b/ts/bench/moq.ts @@ -1,8 +1,9 @@ // The benchmark's hand-written route: each workload in the codec fixture's `bench.rs`, // written with moq's own TypeScript codec (`js/net`) the way its benchmarks and session // code call it. Varints are sync, as `bench/varint.ts` times them; a frame header decodes -// through the Reader's sync `tryDecode`, as the subscriber's read loop does; every other -// encode and decode goes through moq's async Writer and Reader, as `lite-varint.ts`. +// through the Reader's sync `tryDecode`, as the subscriber's read loop does, and so does +// each read's worth of a split one; every other encode and decode goes through moq's +// async Writer and Reader, as `lite-varint.ts`. // // `moq/` imports resolve to the moq checkout when `main.ts` bundles this file. @@ -45,6 +46,8 @@ export function run(input: Uint8Array): Uint8Array | Promise { return decodeGroup(data, lite(selector)); case 6: return iterate(selector, data); + case 7: + return decodeSplit(data, lite(selector)); default: throw new Error(`unknown workload ${op}`); } @@ -139,6 +142,36 @@ function decodeFrames(wire: Uint8Array, version: Version): Uint8Array { return u64s(count, total, last); } +// The headers as they arrive a read at a time. The Reader over the bytes so far stands in +// for one that a stream fills, which awaits each read: its `tryDecode` drains every +// complete header, and a cut one comes up short and is decoded again on the next read. +function decodeSplit(data: Uint8Array, version: Version): Uint8Array { + const read = Math.max(1, Number(u64(data, 0))); + const wire = data.subarray(8); + let start = 0; + let count = 0; + let total = 0; + let last = 0n; + let short = 0; + // Nothing is kept from a header until its last read, which a retry runs again. + const header = (c: Cursor) => { + const delta = c.u62(); + const size = c.u53(); + last = unzigzag(delta); + total += size; + start += c.offset; + return true; + }; + for (let end = 0; end < wire.byteLength; ) { + end = Math.min(wire.byteLength, end + read); + const reader = new Reader(undefined, wire.subarray(start, end), version); + while (reader.tryDecode(header) !== undefined) count++; + if (start < end) short++; + } + if (start < wire.byteLength) throw new Error("the wire ends mid-header"); + return u64s(count, total, last, short); +} + function encodeGroup(data: Uint8Array, version: Version): Promise { const group = new Group({ subscribe: u64(data, 0), sequence: Number(u64(data, 1)) }); return Writer.collect(version, (w) => group.encode(w, version)); diff --git a/ts/bench/workloads.ts b/ts/bench/workloads.ts index a703d54a7..1c6624e0e 100644 --- a/ts/bench/workloads.ts +++ b/ts/bench/workloads.ts @@ -1,6 +1,6 @@ // The workloads every route runs, as inputs to the codec fixture's `bench::run`: moq's own // benchmarks' cases, from `js/net/bench/varint.ts` and `lite-varint.ts`. Each decode -// reads what its encode wrote. +// reads what its encode wrote, and a split decode reads it as it arrives over a stream. export interface Workload { /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, or iterated. */ @@ -21,6 +21,7 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] name = name.replace("%", `${wire.byteLength / items}B`); out.push({ name, op: "encode", items, input }); out.push({ name, op: "decode", items, input: Uint8Array.of(op + 1, selector, ...wire) }); + return wire; }; // One call codes this many copies, as `varint.ts` decodes them through one Cursor. @@ -40,7 +41,16 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] ] as const) { // One group: 30 fps, a keyframe every 10 frames. Params: count, delta (µs), first // size, size, key interval, key size. - pair(`frames/video/${version}`, 60, 2, selector, [60n, 33_333n, 60_000n, 8_000n, 10n, 17_000n]); + const video = pair(`frames/video/${version}`, 60, 2, selector, [60n, 33_333n, 60_000n, 8_000n, 10n, 17_000n]); + // The same headers in reads shorter than any of them, so each comes up short at least + // once and is decoded again: `bench.rs`'s op 7, whose param is the read size. + const read = 3; + out.push({ + name: `frames/video/${version}/${read}B-reads`, + op: "decode", + items: 60, + input: Uint8Array.of(7, selector, ...u64(BigInt(read)), ...video), + }); pair(`frames/audio/${version}`, 50, 2, selector, [50n, 20_000n, 160n, 160n, 0n, 0n]); pair(`group/${version}`, 1, 4, selector, [3n, 1_234n]); } diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 9d294e43a..b67937064 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -38,6 +38,8 @@ export function run(input: Uint8Array): rt.ByteVec { encoded(encodeGroup(data2, liteVersion(selector), out), out); } else if (op === 6) { iterate(selector, data2, out); + } else if (op === 7) { + decoded(decodeSplit(data2, liteVersion(selector), out), out); } else { decoded(decodeGroup(data2, liteVersion(selector), out), out); } @@ -158,6 +160,68 @@ export function decodeFrames(wire: Uint8Array, version: Version.Version, out: rt return rt.Ok(undefined); } +/** + * The FRAME headers of [`decode_frames`] as a subscriber reads them: the wire after the + * read size arrives that many bytes at a time, and each read decodes every header it + * completes. A header cut by the end of a read comes up [`DecodeError::Short`] and is + * decoded again from its start on the next. How many, their total size, the last delta, + * and how many decodes came up short. + */ +export function decodeSplit(data: Uint8Array, version: Version.Version, out: rt.ByteVec): rt.Result { + const [read, wire] = takeU64(data); + const read2 = rt.max(rt.u64.low(read), 1); + let start: rt.usize = 0; + let end: rt.usize = 0; + let count: rt.usize = 0; + let total: rt.u64 = 0; + let last: rt.i64 = 0; + let short: rt.usize = 0; + while (end < wire.length) { + end = rt.min(wire.length, rt.usize.add(end, read2)); + while (start < end) { + let r: Decoder.Decoder = Decoder.new(rt.slice.range(wire, start, end), Form.from(version)); + const frameHeader_ = frameHeader(r); + if (frameHeader_.ok) { + const delta = frameHeader_.value[0]; + const size = frameHeader_.value[1]; + start = rt.usize.sub(end, Decoder.remaining(r)); + last = delta; + total = rt.u64.add(total, size); + count = rt.usize.add(count, 1); + } else if (!frameHeader_.ok && frameHeader_.error.tag === "Short") { + short = rt.usize.add(short, 1); + break; + } else { + const err = frameHeader_.error; + return rt.Err(err); + } + } + } + if (start < end) { + return rt.Err({ tag: "Short" }); + } + putUsize(out, count); + putU64(out, total); + putU64(out, rt.u64.cast(last)); + putUsize(out, short); + return rt.Ok(undefined); +} + +/** One FRAME header: the zigzag timestamp delta, then the size. */ +export function frameHeader(r: Decoder.Decoder): rt.Result<[rt.i64, rt.u64], DecodeError.DecodeError> { + const varint = Decoder.varint(r); + if (!varint.ok) { + return rt.Err(varint.error); + } + const delta = unzigzag(varint.value); + const arg = delta; + const varint2 = Decoder.varint(r); + if (!varint2.ok) { + return rt.Err(varint2.error); + } + return rt.Ok([arg, varint2.value]); +} + /** One size-prefixed GROUP header, starting at the first frame. */ export function encodeGroup(data: Uint8Array, version: Version.Version, out: rt.ByteVec): rt.Result { const [subscribe, data2] = takeU64(data); diff --git a/ts/fixtures/codec/src/bench.rs b/ts/fixtures/codec/src/bench.rs index 3ba3f600b..36f46db4d 100644 --- a/ts/fixtures/codec/src/bench.rs +++ b/ts/fixtures/codec/src/bench.rs @@ -2,7 +2,7 @@ //! benchmarks: `js/net/bench/varint.ts` and `lite-varint.ts`. The first input byte picks //! the workload and the second its wire format; an encode's output is the matching //! decode's input. Op 6 runs iterator chains over its bytes instead, as moq-net's model -//! writes them. +//! writes them, and op 7 decodes frame headers as they arrive a few bytes at a time. use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, varint}; use crate::harness::{decode_error, encode_error, form, lite_version, put_u64, put_usize, take_u64}; @@ -25,6 +25,7 @@ pub fn run(input: &[u8]) -> Vec { 3 => decoded(decode_frames(data, lite_version(selector), &mut out), &mut out), 4 => encoded(encode_group(data, lite_version(selector), &mut out), &mut out), 6 => iterate(selector, data, &mut out), + 7 => decoded(decode_split(data, lite_version(selector), &mut out), &mut out), _ => decoded(decode_group(data, lite_version(selector), &mut out), &mut out), } out @@ -110,6 +111,56 @@ fn decode_frames(wire: &[u8], version: lite::Version, out: &mut Vec) -> Resu Ok(()) } +/// The FRAME headers of [`decode_frames`] as a subscriber reads them: the wire after the +/// read size arrives that many bytes at a time, and each read decodes every header it +/// completes. A header cut by the end of a read comes up [`DecodeError::Short`] and is +/// decoded again from its start on the next. How many, their total size, the last delta, +/// and how many decodes came up short. +fn decode_split(data: &[u8], version: lite::Version, out: &mut Vec) -> Result<(), DecodeError> { + let (read, wire) = take_u64(data); + let read = (read as usize).max(1); + let mut start = 0; + let mut end = 0; + let mut count = 0; + let mut total = 0; + let mut last = 0; + let mut short = 0; + while end < wire.len() { + end = wire.len().min(end + read); + while start < end { + let mut r = Decoder::new(&wire[start..end], version.into()); + match frame_header(&mut r) { + Ok((delta, size)) => { + start = end - r.remaining(); + last = delta; + total += size; + count += 1; + } + Err(DecodeError::Short) => { + short += 1; + break; + } + Err(err) => return Err(err), + } + } + } + // The wire ended mid-header. + if start < end { + return Err(DecodeError::Short); + } + put_usize(out, count); + put_u64(out, total); + put_u64(out, last as u64); + put_usize(out, short); + Ok(()) +} + +/// One FRAME header: the zigzag timestamp delta, then the size. +fn frame_header(r: &mut Decoder<'_>) -> Result<(i64, u64), DecodeError> { + let delta = varint::unzigzag(r.varint()?); + Ok((delta, r.varint()?)) +} + /// One size-prefixed GROUP header, starting at the first frame. fn encode_group(data: &[u8], version: lite::Version, out: &mut Vec) -> Result<(), EncodeError> { let (subscribe, data) = take_u64(data);