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
2 changes: 1 addition & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md);

- [Lower bodies from MIR](/quest/m2/ts-mir.md) - the MIR spike's prototype becomes the only lowering, and the THIR one goes
- [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
- [Wide-integer runtime off bigint](/quest/m2/ts-wide-bigint.md) - checked sums, 128-bit quotients, and comparisons with numbers stop going through `bigint`, kept only if measured faster
- [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
31 changes: 31 additions & 0 deletions quest/m2/ts-wide-bigint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# [M] Wide-integer runtime off bigint

## Goal

[rust2-ts](/ts/README.md)'s 64- and 128-bit runtime stops going through `bigint` on the
paths moq-net's code reaches, each change kept only if the
[TS benchmark](/ts/README.md#bench) measures it faster, with the wide differential sweep
still passing.

## Plan

Decided with the maintainer on 2026-10-25: the quest also measures `u128` and `i128` as
native JS `bigint`s instead of numbers and limbs, on the codec and timestamp workloads,
bundle size included, and keeps whichever wins. `u64` and `i64` stay numbers below 2^53,
since they already come near `bigint` there.

The `time/u128` workloads, moq-net's `Timestamp::convert`, still take 8-12x the
hand-written `bigint` route's time after products moved onto halves. What remains there,
and elsewhere:

- `u128` and `i128` division and remainder of limbs by a number go through `bigint`,
where a long division by a 32-bit divisor, as `u64`'s, would not.
- Comparing a number with limbs boxes the number into limbs, though each value has one
form: limbs lie past every number, on the side their sign says.
- `checked_add`, `checked_sub`, and the saturating ones go through `bigint` for every
value, numbers included; moq-net's `Timestamp::checked_add` is one.

Measure each against main's translation with `just ts bench --route`, and profile the
`time/` workloads first: what dominates may not be on this list.

Public API: none; the runtime's internals.
22 changes: 0 additions & 22 deletions quest/m2/ts-wide-fast.md

This file was deleted.

38 changes: 24 additions & 14 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ object of 32-bit limbs, `{ hi, lo }`, with `w2` and `w1` between them at 128 bit
`hi` signed in a signed type. Each value has one form, so `x === 0` is exact; a caller
makes one past 2^53 with `rt.u64Of(18446744073709551615n)`. Its other operators and
comparisons are runtime calls, `rt.u64Add(a, b)` and `rt.u64Lt(a, b)`, and `x as u32`
is `rt.u64Low(x)`.
is `rt.u64Low(x)`. From MIR, `((hi as u64) << 32) | lo as u64` of halves of at most 32
bits is one call, `rt.u64FromHalves(hi, lo)`.

`format!`, `write!`, `panic!`, `assert!`, and the other macros of `format_args!`
translate, as do `Display` and `Debug` impls, derived and thiserror's included. The
Expand Down Expand Up @@ -182,14 +183,20 @@ they need.
numbers in locals, fields, and arguments. It ties unboxed halves on the codec and
beats `bigint` there in Node, Bun, and Chrome: varint encode 13-65% faster, frame
decode 8-32%, and message decode 22-31%, though Chrome's 8-byte varint decode is
25-45% slower. A loop of sums and comparisons past 2^32 measured 2-6x faster. Limbs
for every value allocate an object each, which costs what a `bigint` does in V8, so
they lost decode; unboxed halves would lower every value to two. Past 2^53 an
operation allocates limbs: a product past 2^53 divided back down is 1.5-1.9x slower
than `bigint`, and 128-bit arithmetic there goes through `bigint` inside the runtime.
The runtime adds 6 KB to the codec's bundle, 2 KB gzipped. Brands tell `u64` from
`i64` and `u32` from `usize` at no cost, since TypeScript erases them; a required
brand would cast every literal, so a plain number passes for any type.
25-45% slower; joining a varint's halves in one call, `rt.u64FromHalves`, makes that
decode 1.05-1.2x faster in Chrome. A loop of sums and comparisons past 2^32 measured
2-6x faster. Limbs for every value allocate an object each, which costs what a
`bigint` does in V8, so they lost decode; unboxed halves would lower every value to
two. Past 2^53 an operation allocates limbs, and 128-bit arithmetic goes through
`bigint` inside the runtime, but a product multiplies 32-bit halves without boxing
either factor, at 128 bits when both are numbers. Converting timestamps between
timescales, products past 2^53 divided back down, measured 2.1-3.1x faster that way
than through boxed factors in `u64`, and 1.6-1.7x in `u128` as moq-net converts them:
1.4-2.1x `bigint`'s time in `u64`, and 8-12x in `u128`, whose division and
comparisons still go through `bigint`. The runtime adds 6 KB to the codec's bundle, 2
KB gzipped. Brands tell `u64` from `i64` and `u32` from `usize` at no cost, since
TypeScript erases them; a required brand would cast every literal, so a plain number
passes for any type.
- A struct is a plain object, and each function of a type, trait impls included, is its
own function in the type's module, so a caller's bundler drops every one it never
calls: no bundler drops a class's unused methods or an object's unused properties. A struct that
Expand Down Expand Up @@ -309,7 +316,8 @@ built natively for wasm32 and run beside its translation on the same inputs by
`Eq`, and `Ord` disagree with JS's. Its differential test runs through `caller.ts`, a
hand-written caller that also prints a `printable` error nothing translated prints,
and adds every 64- and 128-bit case over each pair of values next to a power of two,
where the runtime switches between numbers and limbs, and its limbs carry and borrow.
where the runtime switches between numbers and limbs, and its limbs carry and borrow,
with products of those values at 128 bits and their halves joined back.
`brands.ts`, another caller, mixes integer types where `tsc` must refuse them.
- `task` trims moq's kio to a channel of `Arc`ed state, waiters, and wakers, under async
functions. Its caller is hand-written TS (`tests/task.ts`): a FIFO executor that runs a
Expand Down Expand Up @@ -337,10 +345,12 @@ 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, the same
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, and a `BTreeMap` of 16, 256,
and 4096 keys. 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`, and a sorted array in `bench/sorted.ts`), and translated; every route's output must match
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
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
translation to show the noise. Each round runs the JS routes and workloads in a new
Expand Down
5 changes: 3 additions & 2 deletions ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,15 @@ 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, or a key): the median",
"Each cell is nanoseconds per item (a varint, a frame header, a message, a byte iterated, a key, or a timestamp converted): 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`), and for `btree/` a sorted array (`bench/sorted.ts`).",
"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",
"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
3 changes: 3 additions & 0 deletions ts/bench/moq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Cursor, Reader, Writer } from "moq/js/net/src/stream.ts";
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";

type Version = (typeof Lite)[keyof typeof Lite];
Expand Down Expand Up @@ -51,6 +52,8 @@ export function run(input: Uint8Array): Uint8Array | Promise<Uint8Array> {
return decodeSplit(data, lite(selector));
case 8:
return ordered(data);
case 9:
return rescale(selector, data);
default:
throw new Error(`unknown workload ${op}`);
}
Expand Down
31 changes: 31 additions & 0 deletions ts/bench/rescale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The timestamp workloads' hand-written route: `bench.rs`'s op 8 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. */
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));
// `u64` overflows where `u128` doesn't.
const limit = selector === 0 ? (1n << 128n) - 1n : (1n << 64n) - 1n;
let converted = 0n;
let overflowed = 0n;
let last = 0n;
for (let i = 0n; i < count; i++) {
const scaled = (start + i * step) * to;
const value = scaled > limit ? undefined : scaled / from;
if (value === undefined || value > MAX_TIMESTAMP) {
overflowed++;
} else {
last = value;
converted++;
}
}
const out = new Uint8Array(24);
const put = new DataView(out.buffer);
put.setBigUint64(0, converted);
put.setBigUint64(8, overflowed);
put.setBigUint64(16, last);
return out;
}
23 changes: 20 additions & 3 deletions ts/bench/workloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// 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. */
/** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, iterated, or converted. */
name: string;
op: "encode" | "decode" | "iterate" | "map" | "handoff";
/** Varints, frame headers, messages, items, keys, or handoffs per call, which each time is divided by. */
op: "encode" | "decode" | "iterate" | "map" | "convert" | "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 @@ -67,6 +67,23 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[]
for (const count of [16, 256, 4096]) {
out.push({ name: `btree/${count}`, op: "map", items: count, input: Uint8Array.of(8, 0, ...u64(BigInt(count))) });
}

// Microseconds since moq's 2020 epoch, a frame apart at 30 fps, to 90 kHz and back:
// each product passes 2^53 and each quotient is back under it. `bench.rs`'s op 9 converts
// them as moq-net's `Timestamp::convert` does in `u128`, then in `u64`.
const count = 1024;
const start = 190_000_000_000_000n;
const micros = [start, 33_333n, 1_000_000n, 90_000n];
const ninety = [(start * 90_000n) / 1_000_000n, 3_000n, 90_000n, 1_000_000n];
for (const [selector, type] of ["u128", "u64"].entries()) {
for (const [name, params] of [
["micros-to-90k", micros],
["90k-to-micros", ninety],
] as const) {
const input = Uint8Array.of(9, selector, ...[BigInt(count), ...params].flatMap(u64));
out.push({ name: `time/${type}/${name}`, op: "convert", items: count, input });
}
}
return out;
}

Expand Down
63 changes: 63 additions & 0 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function run(input: Uint8Array): rt.ByteVec {
decoded(decodeSplit(data2, liteVersion(selector), out), out);
} else if (op === 8) {
ordered(data2, out);
} else if (op === 9) {
rescale(selector, data2, out);
} else {
decoded(decodeGroup(data2, liteVersion(selector), out), out);
}
Expand Down Expand Up @@ -323,6 +325,67 @@ export function iterate(chain: rt.u8, data: Uint8Array, out: rt.ByteVec): void {
}
}

/**
* Timestamps from one timescale to another, products past 2^53: moq-net's
* `Timestamp::convert` in `u128`, or the same in `u64` (selector 1), which overflows
* sooner. How many converted, how many overflowed, and the last converted.
*/
export function rescale(selector: rt.u8, data: Uint8Array, out: rt.ByteVec): void {
const [count, data2] = takeU64(data);
const [start, data3] = takeU64(data2);
const [step, data4] = takeU64(data3);
const [from, data5] = takeU64(data4);
const [to, ] = takeU64(data5);
let [converted, overflowed, last] = [0, 0, 0] as [rt.u64, rt.u64, rt.u64];
const end = rt.u64Low(count);
for (let i = 0; i < end; i++) {
const value = rt.u64Add(start, rt.u64Mul(i as rt.u64, step));
let scaled: rt.u64 | undefined;
if (selector === 0) {
const checkedMul = rt.u128CheckedMul(rt.u128Cast(value), rt.u128Cast(to));
if (checkedMul !== undefined) {
const scaled2 = checkedMul;
scaled = bounded(rt.u128Div(scaled2, rt.u128Cast(from)));
} else {
scaled = undefined;
}
} else {
const map = rt.optionMap(rt.u64CheckedMul(value, to), (scaled3: rt.u64): rt.u64 => {
return rt.u64Div(scaled3, from);
});
arms: {
if (map !== undefined) {
const v = map;
if (rt.u64Le(v, rt.u64Of(2305843009213693951n))) {
scaled = v;
break arms;
}
}
scaled = undefined;
}
}
if (scaled !== undefined) {
const v2 = scaled;
last = v2;
converted = rt.u64Add(converted, 1);
} else {
overflowed = rt.u64Add(overflowed, 1);
}
}
putU64(out, converted);
putU64(out, overflowed);
putU64(out, last);
}

/** `value`, or `None` past [`MAX_TIMESTAMP`], as moq-net bounds a converted timestamp. */
export function bounded(value: rt.u128): rt.u64 | undefined {
if (rt.u128Le(value, rt.u128Cast(rt.u64Of(2305843009213693951n)))) {
return rt.u64Cast(value);
} else {
return undefined;
}
}

export function decodeGroup(wire: Uint8Array, version: Version.Version, out: rt.ByteVec): rt.Result<undefined, DecodeError.DecodeError> {
const decodeSlice = Group.decodeSlice$Decode_Version(wire, version);
if (!decodeSlice.ok) {
Expand Down
53 changes: 51 additions & 2 deletions ts/fixtures/codec/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//! 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, op 7 decodes frame headers as they arrive a few bytes at a time, and op 8
//! keeps a `BTreeMap`, as moq-net's model keeps its state.
//! writes them, op 7 decodes frame headers as they arrive a few bytes at a time, op 8
//! keeps a `BTreeMap`, as moq-net's model keeps its state, and op 9 converts timestamps
//! between timescales.

use std::collections::BTreeMap;

Expand All @@ -30,6 +31,7 @@ pub fn run(input: &[u8]) -> Vec<u8> {
6 => iterate(selector, data, &mut out),
7 => decoded(decode_split(data, lite_version(selector), &mut out), &mut out),
8 => ordered(data, &mut out),
9 => rescale(selector, data, &mut out),
_ => decoded(decode_group(data, lite_version(selector), &mut out), &mut out),
}
out
Expand Down Expand Up @@ -229,6 +231,53 @@ fn iterate(chain: u8, data: &[u8], out: &mut Vec<u8>) {
}
}

/// Timestamps from one timescale to another, products past 2^53: moq-net's
/// `Timestamp::convert` in `u128`, or the same in `u64` (selector 1), which overflows
/// sooner. How many converted, how many overflowed, and the last converted.
fn rescale(selector: u8, data: &[u8], out: &mut Vec<u8>) {
let (count, data) = take_u64(data);
let (start, data) = take_u64(data);
let (step, data) = take_u64(data);
let (from, data) = take_u64(data);
let (to, _) = take_u64(data);
let (mut converted, mut overflowed, mut last) = (0, 0, 0);
for i in 0..count as usize {
let value = start + i as u64 * step;
let scaled = match selector {
0 => match (value as u128).checked_mul(to as u128) {
Some(scaled) => bounded(scaled / from as u128),
None => None,
},
_ => match value.checked_mul(to).map(|scaled| scaled / from) {
Some(v) if v <= MAX_TIMESTAMP => Some(v),
_ => None,
},
};
match scaled {
Some(v) => {
last = v;
converted += 1;
}
None => overflowed += 1,
}
}
put_u64(out, converted);
put_u64(out, overflowed);
put_u64(out, last);
}

/// moq-net's largest timestamp.
const MAX_TIMESTAMP: u64 = (1 << 61) - 1;

/// `value`, or `None` past [`MAX_TIMESTAMP`], as moq-net bounds a converted timestamp.
const fn bounded(value: u128) -> Option<u64> {
if value <= MAX_TIMESTAMP as u128 {
Some(value as u64)
} else {
None
}
}

fn decode_group(wire: &[u8], version: lite::Version, out: &mut Vec<u8>) -> Result<(), DecodeError> {
let (group, used) = lite::Group::decode_slice(wire, version)?;
put_u64(out, group.subscribe);
Expand Down
Loading