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`
- [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
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
Expand Down
21 changes: 0 additions & 21 deletions quest/m2/ts-bench-timestamp.md

This file was deleted.

10 changes: 6 additions & 4 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,12 @@ The codec fixture's `bench::run` holds the cases moq's own TypeScript benchmarks
frame headers decoded as they arrive a few bytes at a time, which prices a decode that
comes up `Short` and retries, iterator chains over bytes, a `BTreeMap` of 16, 256, and
4096 keys, and timestamps converted between timescales as moq-net's `Timestamp::convert`
does, whose products pass 2^53. Each runs four ways: natively, as wasm32 at moq-wasm's
size-optimized profile, hand-written (moq's `js/net` in `bench/moq.ts`, ES iterator
helpers in `bench/iter.ts`, a sorted array in `bench/sorted.ts`, and `bigint` in
`bench/rescale.ts`, as moq's `js/hang` converts timescales), and translated; every route's output must match
does, whose products pass 2^53, then added by its `checked_add` and ordered by its `Ord`,
whose cross products do too. 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 its `Timestamp`
in `bench/timestamp.ts`, ES iterator helpers in `bench/iter.ts`, a sorted array in
`bench/sorted.ts`, and `bigint` in `bench/rescale.ts`, as moq's `js/hang` converts
timescales), and translated; every route's output must match
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
Expand Down
5 changes: 3 additions & 2 deletions ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,16 @@ function render(): string {
`- moq: ${text(["git", "-C", checkout, "rev-parse", "HEAD"])}`,
...engines.map((e) => `- ${e.engine}`),
"",
"Each cell is nanoseconds per item (a varint, a frame header, a message, a byte iterated, a key, or a timestamp converted): the median",
"Each cell is nanoseconds per item (a varint, a frame header, a message, a byte iterated, a key, or a timestamp converted, added, or compared): the median",
`of ${rounds} samples of at least ${sampleMs} ms, ± half their interquartile range. Each round runs`,
`the JS routes and workloads in a new random order, from seed ${seed}. \`native\` is a release`,
"build on the host, its workloads in a fixed order. `wasm` is the same",
"Rust at moq-wasm's size-optimized profile, run with its input already in memory;",
"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, 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`).",
"for `time/` conversions `bigint`, as moq's `js/hang` converts timescales (`bench/rescale.ts`),",
"and for `time/checked-add` and `time/ord` moq's own `Timestamp` (`bench/timestamp.ts`).",
"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",
Expand Down
5 changes: 5 additions & 0 deletions ts/bench/moq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as Varint from "moq/js/net/src/varint.ts";
import { iterate } from "./iter.ts";
import { rescale } from "./rescale.ts";
import { ordered } from "./sorted.ts";
import { interleave, stamp } from "./timestamp.ts";

type Version = (typeof Lite)[keyof typeof Lite];

Expand Down Expand Up @@ -54,6 +55,10 @@ export function run(input: Uint8Array): Uint8Array | Promise<Uint8Array> {
return ordered(data);
case 9:
return rescale(selector, data);
case 10:
return stamp(data);
case 11:
return interleave(data);
default:
throw new Error(`unknown workload ${op}`);
}
Expand Down
4 changes: 2 additions & 2 deletions ts/bench/rescale.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// The timestamp workloads' hand-written route: `bench.rs`'s op 8 with `bigint`, as moq's
// The timestamp workloads' hand-written route: `bench.rs`'s op 9 with `bigint`, as moq's
// TypeScript converts timescales (`wallClockTime` in `js/hang/src/catalog/clock.ts`).

const MAX_TIMESTAMP = (1n << 61n) - 1n;

/** Convert every timestamp, as `bench::run`'s op 8 does: how many, how many overflowed, and the last. */
/** Convert every timestamp, as `bench::run`'s op 9 does: how many, how many overflowed, and the last. */
export function rescale(selector: number, data: Uint8Array): Uint8Array {
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const [count, start, step, from, to] = [0, 1, 2, 3, 4].map((i) => view.getBigUint64(i * 8));
Expand Down
68 changes: 68 additions & 0 deletions ts/bench/timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// The `Timestamp` workloads' hand-written route: `bench.rs`'s ops 10 and 11 with moq's own
// TypeScript `Timestamp`, whose values are numbers and whose `isBefore` compares across
// scales in `bigint`.
//
// `moq/` imports resolve to the moq checkout when `main.ts` bundles this file.

import { Timescale, Timestamp } from "moq/js/net/src/time.ts";

/** Read the `u64`s `data` holds, as numbers: the workloads keep them under 2^53. */
function params(data: Uint8Array): number[] {
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
return Array.from({ length: data.byteLength / 8 }, (_, i) => Number(view.getBigUint64(i * 8)));
}

function u64s(...values: number[]): Uint8Array {
const out = new Uint8Array(values.length * 8);
const view = new DataView(out.buffer);
values.forEach((v, i) => view.setBigUint64(i * 8, BigInt(v)));
return out;
}

/** Op 10: a track's frames stamped one duration apart. */
export function stamp(data: Uint8Array): Uint8Array {
const [count, start, step, scale] = params(data);
const duration = new Timestamp(step, Timescale(scale));
let time = new Timestamp(start, Timescale(scale));
let stamped = 0;
for (let i = 0; i < count; i++) {
time = checkedAdd(time, duration);
stamped++;
}
return u64s(stamped, 0, time.value);
}

// moq's `Timestamp` has no `checked_add`; this is Rust's, except that the constructor
// throws past 2^53, which the workload stays under.
function checkedAdd(a: Timestamp, b: Timestamp): Timestamp {
if (a.scale !== b.scale) throw new RangeError("mismatched timescales");
return new Timestamp(a.value + b.value, a.scale);
}

/**
* Op 11: two tracks interleaved in presentation order. `isBefore` has no tie-break, as
* Rust's `Ord` does by `(scale, value)`; the first track is the finer-scaled, so a tie
* takes the second either way.
*/
export function interleave(data: Uint8Array): Uint8Array {
const [count, aStart, aStep, aScale, bStart, bStep, bScale] = params(data);
let a = new Timestamp(aStart, Timescale(aScale));
let b = new Timestamp(bStart, Timescale(bScale));
let fromA = 0;
let fromB = 0;
let switches = 0;
let lastA = true;
for (let i = 0; i < count; i++) {
const takeA = a.isBefore(b);
if (takeA) {
a = new Timestamp(a.value + aStep, a.scale);
fromA++;
} else {
b = new Timestamp(b.value + bStep, b.scale);
fromB++;
}
if (takeA !== lastA) switches++;
lastA = takeA;
}
return u64s(fromA, fromB, switches);
}
11 changes: 10 additions & 1 deletion ts/bench/workloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export interface Workload {
/** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, iterated, or converted. */
name: string;
op: "encode" | "decode" | "iterate" | "map" | "convert" | "handoff";
op: "encode" | "decode" | "iterate" | "map" | "convert" | "add" | "compare" | "handoff";
/** Varints, frame headers, messages, items, keys, timestamps, or handoffs per call, which each time is divided by. */
items: number;
input: Uint8Array;
Expand Down Expand Up @@ -84,6 +84,15 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[]
out.push({ name: `time/${type}/${name}`, op: "convert", items: count, input });
}
}

// The same frames stamped by moq-net's `Timestamp::checked_add` (op 10), and interleaved
// by its `Ord` (op 11) with an audio track's at 48 kHz, from the same instant: every
// value is under 2^53, as moq's TypeScript keeps them, and every cross product past it.
const video = [start, 33_333n, 1_000_000n];
const audio = [(start * 48_000n) / 1_000_000n, 1_024n, 48_000n];
const time = (op: number, params: bigint[]) => Uint8Array.of(op, 0, ...[BigInt(count), ...params].flatMap(u64));
out.push({ name: "time/checked-add/micros", op: "add", items: count, input: time(10, video) });
out.push({ name: "time/ord/micros-48k", op: "compare", items: count, input: time(11, [...video, ...audio]) });
return out;
}

Expand Down
50 changes: 50 additions & 0 deletions ts/fixtures/codec/expected/bench.Timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { bounded } from "./bench.ts";

/** moq-net's `Timestamp`, with the scale it keeps as a `NonZero` unwrapped. */
export interface Timestamp {
value: rt.u64;
scale: rt.u64;
}

/**
* The sum, or `None` across scales or past [`MAX_TIMESTAMP`], as moq-net's
* `Timestamp::checked_add` returns `TimeOverflow`.
*/
export function checkedAdd(self: Timestamp, rhs: Timestamp): Timestamp | undefined {
if (!rt.u64Ne(self.scale, rhs.scale)) {
const _6: rt.u64 | undefined = rt.u64CheckedAdd(self.value, rhs.value);
if (_6 === undefined) {
return undefined;
}
const _11: rt.u64 | undefined = bounded(rt.u128Cast(_6));
if (_11 === undefined) {
return undefined;
}
return { value: _11, scale: self.scale };
}
return undefined;
}

export function partialCmp$PartialOrd(self: Timestamp, other: Timestamp): rt.Ordering | undefined {
return cmp$Ord(self, other);
}

/**
* moq-net's temporal order: across scales, the cross products in `u128`, with a tie
* broken by `(scale, value)`.
*/
export function cmp$Ord(self: Timestamp, other: Timestamp): rt.Ordering {
if (!rt.u64Eq(self.scale, other.scale)) {
return rt.orderingThen(rt.orderingThen(rt.cmp(rt.u128Mul(rt.u128Cast(self.value), rt.u128Cast(other.scale)), rt.u128Mul(rt.u128Cast(other.value), rt.u128Cast(self.scale))), (): rt.Ordering => {
return rt.cmp(self.scale, other.scale);
}, true), (): rt.Ordering => {
return rt.cmp(self.value, other.value);
}, true);
}
return rt.cmp(self.value, other.value);
}

//# sourceMappingURL=bench.Timestamp.ts.map
89 changes: 89 additions & 0 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { copy$Timestamp } from "./lib.ts";
import * as Timestamp from "./bench.Timestamp.ts";
import type * as DecodeError from "./coding/decode.DecodeError.ts";
import * as Decoder from "./coding/decode.Decoder.ts";
import type * as EncodeError from "./coding/encode.EncodeError.ts";
Expand Down Expand Up @@ -38,6 +40,10 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
ordered(_9[1], out);
} else if (op === 9) {
rescale(selector, _9[1], out);
} else if (op === 10) {
stamp(_9[1], out);
} else if (op === 11) {
interleave(_9[1], out);
} else {
decoded(decodeGroup(_9[1], liteVersion(selector), out), out);
}
Expand Down Expand Up @@ -406,6 +412,89 @@ export function bounded(value: rt.u128): rt.u64 | undefined {
return rt.u64Cast(value);
}

/**
* A track's frames stamped one duration apart by [`Timestamp::checked_add`], as a
* publisher stamps them. How many were stamped, how many overflowed, and the last.
*/
export function stamp(data: rt.ByteSlice, out: rt.ByteVec): void {
const _5: [rt.u64, rt.ByteSlice] = takeU64(data);
const count: rt.u64 = _5[0];
const _8: [rt.u64, rt.ByteSlice] = takeU64(_5[1]);
const start: rt.u64 = _8[0];
const _11: [rt.u64, rt.ByteSlice] = takeU64(_8[1]);
const step: rt.u64 = _11[0];
const scale: rt.u64 = takeU64(_11[1])[0];
let time: Timestamp.Timestamp = { value: start, scale };
let stamped: rt.u64 = 0;
let overflowed: rt.u64 = 0;
const iter: rt.Range<rt.u64> = new rt.Range(0, count, rt.u64Step);
while (true) {
const step2: Timestamp.Timestamp = { value: step, scale };
if (iter.next() === rt.DONE) {
break;
}
const _25: Timestamp.Timestamp | undefined = Timestamp.checkedAdd(time, step2);
if (_25 === undefined) {
overflowed = rt.u64Add(overflowed, 1);
} else {
time = _25;
stamped = rt.u64Add(stamped, 1);
}
}
putU64(out, stamped);
putU64(out, overflowed);
putU64(out, time.value);
}

/**
* Two tracks' frames, each one duration after the last at its own scale, interleaved in
* presentation order by [`Timestamp`]'s `Ord`, as a muxer interleaves them: one
* comparison per frame taken. How many came from each track, and how many times the
* interleave switched tracks.
*/
export function interleave(data: rt.ByteSlice, out: rt.ByteVec): void {
const _5: [rt.u64, rt.ByteSlice] = takeU64(data);
const count: rt.u64 = _5[0];
const _9: [Timestamp.Timestamp, rt.u64, rt.ByteSlice] = track(_5[1]);
const a: Timestamp.Timestamp = copy$Timestamp(_9[0]);
const aStep: rt.u64 = _9[1];
const _12: [Timestamp.Timestamp, rt.u64, rt.ByteSlice] = track(_9[2]);
const b: Timestamp.Timestamp = copy$Timestamp(_12[0]);
const bStep: rt.u64 = _12[1];
let fromA: rt.u64 = 0;
let fromB: rt.u64 = 0;
let switches: rt.u64 = 0;
let lastA: boolean = true;
const iter: rt.Range<rt.u64> = new rt.Range(0, count, rt.u64Step);
while (iter.next() !== rt.DONE) {
const takeA: boolean = Timestamp.partialCmp$PartialOrd(a, b) === -1;
if (!takeA) {
b.value = rt.u64Add(b.value, bStep);
fromB = rt.u64Add(fromB, 1);
} else {
a.value = rt.u64Add(a.value, aStep);
fromA = rt.u64Add(fromA, 1);
}
if (takeA !== lastA) {
switches = rt.u64Add(switches, 1);
}
lastA = takeA;
}
putU64(out, fromA);
putU64(out, fromB);
putU64(out, switches);
}

/** A track's first timestamp and its frame duration: start, step, and scale. */
export function track(data: rt.ByteSlice): [Timestamp.Timestamp, rt.u64, rt.ByteSlice] {
const _4: [rt.u64, rt.ByteSlice] = takeU64(data);
const value: rt.u64 = _4[0];
const _7: [rt.u64, rt.ByteSlice] = takeU64(_4[1]);
const step: rt.u64 = _7[0];
const _10: [rt.u64, rt.ByteSlice] = takeU64(_7[1]);
return [{ value, scale: _10[0] }, step, _10[1]] as [Timestamp.Timestamp, rt.u64, rt.ByteSlice];
}

export function decodeGroup(wire: rt.ByteSlice, version: Version.Version, out: rt.ByteVec): rt.Result<undefined, DecodeError.DecodeError> {
const _6: rt.Result<[Group.Group, rt.usize], DecodeError.DecodeError> = Group.decodeSlice$Decode_Version(wire, version);
if (_6.ok) {
Expand Down
5 changes: 5 additions & 0 deletions ts/fixtures/codec/expected/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated by rust2-ts. Do not edit.

import type * as Timestamp from "./bench.Timestamp.ts";
import type * as Form from "./coding/varint.Form.ts";
import type * as Version from "./coding/version.Version.ts";

Expand All @@ -10,6 +11,10 @@ export function copy$Form(v: Form.Form): Form.Form {
return v;
}

export function copy$Timestamp(v: Timestamp.Timestamp): Timestamp.Timestamp {
return { value: v.value, scale: v.scale };
}

export function copy$Version(v: Version.Version): Version.Version {
return { _0: v._0 };
}
Expand Down
Loading