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 @@ -28,7 +28,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md);
- [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
Expand Down
14 changes: 0 additions & 14 deletions quest/m2/ts-bench-error-path.md

This file was deleted.

7 changes: 4 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,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
Expand Down
4 changes: 3 additions & 1 deletion ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 35 additions & 2 deletions ts/bench/moq.ts
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -45,6 +46,8 @@ export function run(input: Uint8Array): Uint8Array | Promise<Uint8Array> {
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}`);
}
Expand Down Expand Up @@ -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<Uint8Array> {
const group = new Group({ subscribe: u64(data, 0), sequence: Number(u64(data, 1)) });
return Writer.collect(version, (w) => group.encode(w, version));
Expand Down
14 changes: 12 additions & 2 deletions ts/bench/workloads.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -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.
Expand All @@ -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]);
}
Expand Down
64 changes: 64 additions & 0 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<undefined, DecodeError.DecodeError> {
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<undefined, EncodeError.EncodeError> {
const [subscribe, data2] = takeU64(data);
Expand Down
53 changes: 52 additions & 1 deletion ts/fixtures/codec/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -25,6 +25,7 @@ pub fn run(input: &[u8]) -> Vec<u8> {
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
Expand Down Expand Up @@ -110,6 +111,56 @@ fn decode_frames(wire: &[u8], version: lite::Version, out: &mut Vec<u8>) -> 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<u8>) -> 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<u8>) -> Result<(), EncodeError> {
let (subscribe, data) = take_u64(data);
Expand Down