From 6cc708b52a2f068991525af159732640e19af503 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 05:16:03 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m2/ts-mir-size.md Quest-Claim: ac82fbd881dca22c From 27830876e2787773fa5767e0fadeb2578db7ef23 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 05:53:28 -0700 Subject: [PATCH 2/3] perf(ts): leave out what the codec's bundle never calls Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/runtime/rt/collections.ts | 7 +- ts/runtime/rt/int.ts | 277 ++++++++++++++++++++--------------- ts/runtime/rt/iter.ts | 21 +-- 3 files changed, 173 insertions(+), 132 deletions(-) diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index 58824a4bd..e1b53884c 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -16,7 +16,7 @@ import { panic } from "../rt.ts"; import { type Limbs, lt } from "./int.ts"; -import { DONE, type Done, Iter, iterToArray, RangeInclusive } from "./iter.ts"; +import { DONE, type Done, Iter, iterToArray, type RangeInclusive } from "./iter.ts"; import { type Arc, dropAll } from "./task.ts"; // Ordering @@ -1227,8 +1227,9 @@ export type Bounds = { readonly start: K; readonly end: K } | RangeInclusive< /** `RangeBounds`: the start, the end, and whether the end is in the range. */ function bounds(r: Bounds): [{ key: K } | undefined, { key: K } | undefined, boolean] { if (r === undefined) return [undefined, undefined, false]; - // Exhausted, an inclusive range ends before its end, as its `end_bound` says. - if (r instanceof RangeInclusive) return [{ key: r.lo }, { key: r.hi }, !r.exhausted]; + // Exhausted, an inclusive range ends before its end, as its `end_bound` says. A field + // test, not `instanceof`, so a bundle without `a..=b` leaves the class out. + if ("exhausted" in r) return [{ key: r.lo }, { key: r.hi }, !r.exhausted]; return ["start" in r ? { key: r.start } : undefined, "end" in r ? { key: r.end } : undefined, false]; } diff --git a/ts/runtime/rt/int.ts b/ts/runtime/rt/int.ts index adac30886..31c67ba2d 100644 --- a/ts/runtime/rt/int.ts +++ b/ts/runtime/rt/int.ts @@ -66,13 +66,7 @@ export type i128 = Num<"i128"> | I128Limbs; export type Step = (n: T, by: number) => [T, boolean]; /** `core::num::TryFromIntError`: which way the value was out of range. */ -export class TryFromIntError { - readonly kind: "PosOverflow" | "NegOverflow"; - - constructor(kind: "PosOverflow" | "NegOverflow") { - this.kind = kind; - } -} +export type TryFromIntError = { readonly kind: "PosOverflow" | "NegOverflow" }; const TWO32 = 4294967296; const MAX_SAFE = 0x1fffffffffffff; @@ -153,6 +147,11 @@ function beWord(bytes: Uint8Array): number { return value; } +/** The big-endian 32-bit word of `b` at `i`, a uint32. */ +function word(b: Uint8Array, i: number): number { + return ((b[i] << 24) | (b[i + 1] << 16) | (b[i + 2] << 8) | b[i + 3]) >>> 0; +} + function beBytes(a: number, n: number): Uint8Array { const out = new Uint8Array(n); for (let i = n - 1; i >= 0; i--) { @@ -166,7 +165,7 @@ function narrowTryFrom(value: number | Limbs, min: number, max: number): Result< // Rounding past 2^53 keeps the order, so the bounds still test exactly. const n = typeof value === "number" ? value : approx(value); if (n >= min && n <= max) return Ok(n); - return Err(new TryFromIntError(n < min ? "NegOverflow" : "PosOverflow")); + return Err({ kind: n < min ? "NegOverflow" : "PosOverflow" }); } /** `n + by` within `[min, max]`, wrapped by `wrap` past them; `by` may be past the type's range. */ @@ -316,7 +315,7 @@ export const u32LeadingOnes = (a: u32): number => Math.clz32(~a); /** `u32`'s `leading_zeros`: the count of leading zero bits. */ export const u32LeadingZeros = (a: u32): number => Math.clz32(a); /** `u32`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const u32FromBeBytes = (bytes: Uint8Array): u32 => beWord(bytes) >>> 0; +export const u32FromBeBytes = (bytes: Uint8Array): u32 => word(bytes, 0); /** `u32`'s `to_be_bytes`: its big-endian bytes. */ export const u32ToBeBytes = (a: u32): Uint8Array => beBytes(a, 4); /** `u32`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ @@ -457,7 +456,7 @@ export const i32LeadingOnes = (a: i32): number => Math.clz32(~a); /** `i32`'s `leading_zeros`: the count of leading zero bits. */ export const i32LeadingZeros = (a: i32): number => Math.clz32(a); /** `i32`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const i32FromBeBytes = (bytes: Uint8Array): i32 => beWord(bytes) | 0; +export const i32FromBeBytes = (bytes: Uint8Array): i32 => word(bytes, 0) | 0; /** `i32`'s `to_be_bytes`: its big-endian bytes. */ export const i32ToBeBytes = (a: i32): Uint8Array => beBytes(a, 4); /** `i32`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ @@ -573,12 +572,12 @@ export const isizeStep: Step = i32Step as never; // 64- and 128-bit integers: numbers while they are safe integers, whose arithmetic stays // in numbers while its result does, and limbs past that. Each function takes the type's -// width and sign, which each type's own passes as literals. A type's own takes the -// number path itself where it is hot, small enough for a JIT to inline: through the -// shared function alone, V8 took up to 1.7x as long on the codec's frames. Limbs compute -// on their 32-bit words, several times faster than through bigints: sums, differences, -// comparisons, casts, products of 64-bit values or of two numbers, and division by a -// divisor below 2^32. The rest goes through bigints. +// sign, and its width where both widths share the function, which each type's own passes +// as literals. A type's own takes the number path itself where it is hot, small enough +// for a JIT to inline: through the shared function alone, V8 took up to 1.7x as long on +// the codec's frames. Limbs compute on their 32-bit words, several times faster than +// through bigints: sums, differences, comparisons, casts, products of 64-bit values or of +// two numbers, and division by a divisor below 2^32. The rest goes through bigints. type Wide = number | Limbs; @@ -877,20 +876,27 @@ function divideWords(a: Limbs, b: number, bits: number, signed: boolean, remaind return one({ hi: high(words[0], signed), w2: words[1], w1: words[2], lo: words[3] } as Limbs); } -function shl(a: Wide, b: number | Limbs, bits: number, signed: boolean): Wide { - const n = shift(b, bits, "left"); +// The shifts and bitwise operators compute 64 bits on limbs and 128 through bigints, each +// width its own function, so a bundle keeps only the widths it calls. + +function shl64(a: Wide, b: number | Limbs, signed: boolean): Wide { + const n = shift(b, 64, "left"); if (typeof a === "number" && safe(a * 2 ** n)) return a * 2 ** n; - const x = limbs(a, bits, signed); - if (bits !== 64) return one(box(exact(x) << BigInt(n), bits, signed)); + const x = limbs(a, 64, signed); if (n === 0) return one(x); if (n < 32) return join(high((x.hi << n) | (x.lo >>> (32 - n)), signed), (x.lo << n) >>> 0); return join(high(x.lo << (n - 32), signed), 0); } -function shr(a: Wide, b: number | Limbs, bits: number, signed: boolean): Wide { - const n = shift(b, bits, "right"); +function shl128(a: Wide, b: number | Limbs, signed: boolean): Wide { + const n = shift(b, 128, "left"); + if (typeof a === "number" && safe(a * 2 ** n)) return a * 2 ** n; + return one(box(exact(limbs(a, 128, signed)) << BigInt(n), 128, signed)); +} + +function shr64(a: Wide, b: number | Limbs, signed: boolean): Wide { + const n = shift(b, 64, "right"); if (typeof a === "number") return Math.floor(a / 2 ** n) + 0; - if (bits !== 64) return one(box((signed ? exact(a) : BigInt.asUintN(bits, exact(a))) >> BigInt(n), bits, signed)); if (n === 0) return one(a); if (signed) { if (n < 32) return join(a.hi >> n, ((a.lo >>> n) | (a.hi << (32 - n))) >>> 0); @@ -900,9 +906,15 @@ function shr(a: Wide, b: number | Limbs, bits: number, signed: boolean): Wide { return join(0, a.hi >>> (n - 32)); } -// The bitwise operators. On two safe integers, a type's own computes them a 32-bit half -// at a time, which can make -2^53 in a signed type, the one result past the safe -// integers; past that, these compute them on limbs. +function shr128(a: Wide, b: number | Limbs, signed: boolean): Wide { + const n = shift(b, 128, "right"); + if (typeof a === "number") return Math.floor(a / 2 ** n) + 0; + return one(box((signed ? exact(a) : BigInt.asUintN(128, exact(a))) >> BigInt(n), 128, signed)); +} + +// On two safe integers, a type's own computes the bitwise operators a 32-bit half at a +// time, which can make -2^53 in a signed type, the one result past the safe integers; +// past that, these compute them. function wordAnd(a: number, b: number): number { return (Math.floor(a / TWO32) & Math.floor(b / TWO32)) * TWO32 + ((a & b) >>> 0); @@ -916,87 +928,114 @@ function wordXor(a: number, b: number): number { return (Math.floor(a / TWO32) ^ Math.floor(b / TWO32)) * TWO32 + ((a ^ b) >>> 0); } -function and(a: Wide, b: Wide, bits: number, signed: boolean): Wide { - const x = limbs(a, bits, signed); - const y = limbs(b, bits, signed); - if (bits === 64) return join(high(x.hi & y.hi, signed), (x.lo & y.lo) >>> 0); - return one(box(exact(x) & exact(y), bits, signed)); +function and64(a: Wide, b: Wide, signed: boolean): Wide { + const x = limbs(a, 64, signed); + const y = limbs(b, 64, signed); + return join(high(x.hi & y.hi, signed), (x.lo & y.lo) >>> 0); +} + +function and128(a: Wide, b: Wide, signed: boolean): Wide { + return one(box(exact(limbs(a, 128, signed)) & exact(limbs(b, 128, signed)), 128, signed)); +} + +function or64(a: Wide, b: Wide, signed: boolean): Wide { + const x = limbs(a, 64, signed); + const y = limbs(b, 64, signed); + return join(high(x.hi | y.hi, signed), (x.lo | y.lo) >>> 0); +} + +function or128(a: Wide, b: Wide, signed: boolean): Wide { + return one(box(exact(limbs(a, 128, signed)) | exact(limbs(b, 128, signed)), 128, signed)); } -function or(a: Wide, b: Wide, bits: number, signed: boolean): Wide { - const x = limbs(a, bits, signed); - const y = limbs(b, bits, signed); - if (bits === 64) return join(high(x.hi | y.hi, signed), (x.lo | y.lo) >>> 0); - return one(box(exact(x) | exact(y), bits, signed)); +function xor64(a: Wide, b: Wide, signed: boolean): Wide { + const x = limbs(a, 64, signed); + const y = limbs(b, 64, signed); + return join(high(x.hi ^ y.hi, signed), (x.lo ^ y.lo) >>> 0); } -function xor(a: Wide, b: Wide, bits: number, signed: boolean): Wide { - const x = limbs(a, bits, signed); - const y = limbs(b, bits, signed); - if (bits === 64) return join(high(x.hi ^ y.hi, signed), (x.lo ^ y.lo) >>> 0); - return one(box(exact(x) ^ exact(y), bits, signed)); +function xor128(a: Wide, b: Wide, signed: boolean): Wide { + return one(box(exact(limbs(a, 128, signed)) ^ exact(limbs(b, 128, signed)), 128, signed)); } -function not(a: Wide, bits: number, signed: boolean): Wide { +function not64(a: Wide, signed: boolean): Wide { if (signed && typeof a === "number" && a !== MAX_SAFE) return -a - 1; - const x = limbs(a, bits, signed); - if (bits === 64) return join(high(~x.hi, signed), ~x.lo >>> 0); - return one(box(~exact(x), bits, signed)); + const x = limbs(a, 64, signed); + return join(high(~x.hi, signed), ~x.lo >>> 0); } -/** The number of leading ones of `v`'s bits. */ -function leading(v: bigint, bits: number): number { - let n = 0; - for (let bit = BigInt(bits - 1); bit >= 0n && (v >> bit) & 1n; bit--) n++; - return n; +function not128(a: Wide, signed: boolean): Wide { + if (signed && typeof a === "number" && a !== MAX_SAFE) return -a - 1; + return one(box(~exact(limbs(a, 128, signed)), 128, signed)); } -function leadingZeros(a: Wide, bits: number, signed: boolean): number { - const x = limbs(a, bits, signed); - if (bits !== 64) return leading(BigInt.asUintN(bits, ~exact(x)), bits); +function leadingZeros64(a: Wide, signed: boolean): number { + const x = limbs(a, 64, signed); const hi = x.hi >>> 0; return hi !== 0 ? Math.clz32(hi) : 32 + Math.clz32(x.lo); } -function leadingOnes(a: Wide, bits: number, signed: boolean): number { - const x = limbs(a, bits, signed); - if (bits !== 64) return leading(BigInt.asUintN(bits, exact(x)), bits); +function leadingOnes64(a: Wide, signed: boolean): number { + const x = limbs(a, 64, signed); const hi = ~x.hi >>> 0; return hi !== 0 ? Math.clz32(hi) : 32 + Math.clz32(~x.lo >>> 0); } -function fromBeBytes(b: Uint8Array, bits: number, signed: boolean): Wide { - if (bits === 64) { - const hi = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]; - return join(high(hi, signed), ((b[4] << 24) | (b[5] << 16) | (b[6] << 8) | b[7]) >>> 0); - } +/** The number of leading ones of a 128-bit value's bits. */ +function leading128(v: bigint): number { + let n = 0; + for (let bit = 127n; bit >= 0n && (v >> bit) & 1n; bit--) n++; + return n; +} + +function leadingZeros128(a: Wide, signed: boolean): number { + return leading128(BigInt.asUintN(128, ~exact(limbs(a, 128, signed)))); +} + +function leadingOnes128(a: Wide, signed: boolean): number { + return leading128(BigInt.asUintN(128, exact(limbs(a, 128, signed)))); +} + +function fromBeBytes64(b: Uint8Array, signed: boolean): Wide { + return join(high(word(b, 0), signed), word(b, 4)); +} + +function fromBeBytes128(b: Uint8Array, signed: boolean): Wide { // A 32-bit word at a time: one bigint step per word, not per byte. - let value = 0n; - for (let i = 0; i < b.length; i += 4) { - const word = ((b[i] << 24) | (b[i + 1] << 16) | (b[i + 2] << 8) | b[i + 3]) >>> 0; - value = (value << 32n) | BigInt(word); - } - return one(box(value, bits, signed)); + const v = (((((BigInt(word(b, 0)) << 32n) | BigInt(word(b, 4))) << 32n) | BigInt(word(b, 8))) << 32n) | BigInt(word(b, 12)); + return one(box(v, 128, signed)); } -function toBeBytes(a: Wide, bits: number, signed: boolean): Uint8Array { - const x = limbs(a, bits, signed); - const words = "w1" in x ? [x.hi, x.w2, x.w1, x.lo] : [x.hi, x.lo]; - const out = new Uint8Array(bits / 8); - for (let i = 0; i < words.length; i++) { - const word = words[i]; - out[4 * i] = word >>> 24; - out[4 * i + 1] = word >>> 16; - out[4 * i + 2] = word >>> 8; - out[4 * i + 3] = word; - } +/** `w`'s big-endian bytes, into `out` at `i`. */ +function putWord(out: Uint8Array, i: number, w: number): void { + out[i] = w >>> 24; + out[i + 1] = w >>> 16; + out[i + 2] = w >>> 8; + out[i + 3] = w; +} + +function toBeBytes64(a: Wide, signed: boolean): Uint8Array { + const x = limbs(a, 64, signed); + const out = new Uint8Array(8); + putWord(out, 0, x.hi); + putWord(out, 4, x.lo); + return out; +} + +function toBeBytes128(a: Wide, signed: boolean): Uint8Array { + const x = limbs(a, 128, signed) as U128Limbs | I128Limbs; + const out = new Uint8Array(16); + putWord(out, 0, x.hi); + putWord(out, 4, x.w2); + putWord(out, 8, x.w1); + putWord(out, 12, x.lo); return out; } function tryFrom(value: number | Limbs, bits: number, signed: boolean): Result { const v = typeof value === "number" ? BigInt(value) : exact(value); if (fitsBig(v, bits, signed)) return Ok(one(box(v, bits, signed))); - return Err(new TryFromIntError(v < 0n ? "NegOverflow" : "PosOverflow")); + return Err({ kind: v < 0n ? "NegOverflow" : "PosOverflow" }); } function wideDisplay(a: Wide, f: Formatter): FmtResult { @@ -1053,26 +1092,26 @@ export const u64Div = (a: u64, b: u64): u64 => (typeof a === "number" && typeof /** `u64`'s `a % b`, panicking on a zero divisor or overflow. */ export const u64Rem = (a: u64, b: u64): u64 => (typeof a === "number" && typeof b === "number" && b !== 0 ? (a % b) + 0 : rem(a, b, 64, false)) as u64; /** `u64`'s `a << b`, panicking on a shift by the width or more. */ -export const u64Shl = (a: u64, b: number | Limbs): u64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 && safe(a * 2 ** b) ? a * 2 ** b : shl(a, b, 64, false)) as u64; +export const u64Shl = (a: u64, b: number | Limbs): u64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 && safe(a * 2 ** b) ? a * 2 ** b : shl64(a, b, false)) as u64; /** `u64`'s `a >> b`, panicking on a shift by the width or more. */ -export const u64Shr = (a: u64, b: number | Limbs): u64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 ? Math.floor(a / 2 ** b) + 0 : shr(a, b, 64, false)) as u64; +export const u64Shr = (a: u64, b: number | Limbs): u64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 ? Math.floor(a / 2 ** b) + 0 : shr64(a, b, false)) as u64; /** `u64`'s `a & b`. */ export const u64And = (a: u64, b: u64): u64 => { const n = typeof a === "number" && typeof b === "number" ? wordAnd(a, b) : NaN; - return (fits(n, false) ? n : and(a, b, 64, false)) as u64; + return (fits(n, false) ? n : and64(a, b, false)) as u64; }; /** `u64`'s `a | b`. */ export const u64Or = (a: u64, b: u64): u64 => { const n = typeof a === "number" && typeof b === "number" ? wordOr(a, b) : NaN; - return (fits(n, false) ? n : or(a, b, 64, false)) as u64; + return (fits(n, false) ? n : or64(a, b, false)) as u64; }; /** `u64`'s `a ^ b`. */ export const u64Xor = (a: u64, b: u64): u64 => { const n = typeof a === "number" && typeof b === "number" ? wordXor(a, b) : NaN; - return (fits(n, false) ? n : xor(a, b, 64, false)) as u64; + return (fits(n, false) ? n : xor64(a, b, false)) as u64; }; /** `u64`'s `!a`. */ -export const u64Not = (a: u64): u64 => not(a, 64, false) as u64; +export const u64Not = (a: u64): u64 => not64(a, false) as u64; /** `u64`'s `checked_add`: `a + b`, or `undefined` on overflow. */ export const u64CheckedAdd = (a: u64, b: u64): u64 | undefined => (typeof a === "number" && typeof b === "number" && fits(a + b, false) ? a + b : optional(sum64(a, b, 1, false))) as u64 | undefined; /** `u64`'s `checked_sub`: `a - b`, or `undefined` on overflow. */ @@ -1088,13 +1127,13 @@ export const u64WrappingAdd = (a: u64, b: u64): u64 => (typeof a === "number" && /** `u64`'s `wrapping_sub`: `a - b`, wrapped into the type's range. */ export const u64WrappingSub = (a: u64, b: u64): u64 => (typeof a === "number" && typeof b === "number" && fits(a - b, false) ? a - b : sum64(a, b, -1, false)) as u64; /** `u64`'s `leading_ones`: the count of leading one bits. */ -export const u64LeadingOnes = (a: u64): number => leadingOnes(a, 64, false); +export const u64LeadingOnes = (a: u64): number => leadingOnes64(a, false); /** `u64`'s `leading_zeros`: the count of leading zero bits. */ -export const u64LeadingZeros = (a: u64): number => leadingZeros(a, 64, false); +export const u64LeadingZeros = (a: u64): number => leadingZeros64(a, false); /** `u64`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const u64FromBeBytes = (bytes: Uint8Array): u64 => fromBeBytes(bytes, 64, false) as u64; +export const u64FromBeBytes = (bytes: Uint8Array): u64 => fromBeBytes64(bytes, false) as u64; /** `u64`'s `to_be_bytes`: its big-endian bytes. */ -export const u64ToBeBytes = (a: u64): Uint8Array => toBeBytes(a, 64, false); +export const u64ToBeBytes = (a: u64): Uint8Array => toBeBytes64(a, false); /** `u64`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ export const u64TryFrom = (value: number | Limbs): Result => tryFrom(value, 64, false) as Result; /** `u64`'s `Display`: its decimal digits, padded by the formatter's spec. */ @@ -1143,26 +1182,26 @@ export const i64Rem = (a: i64, b: i64): i64 => (typeof a === "number" && typeof /** `i64`'s `-a`, panicking on overflow. */ export const i64Neg = (a: i64): i64 => (typeof a === "number" ? -a + 0 : strict(sum64(0, a, -1, true), "negate")) as i64; /** `i64`'s `a << b`, panicking on a shift by the width or more. */ -export const i64Shl = (a: i64, b: number | Limbs): i64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 && safe(a * 2 ** b) ? a * 2 ** b : shl(a, b, 64, true)) as i64; +export const i64Shl = (a: i64, b: number | Limbs): i64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 && safe(a * 2 ** b) ? a * 2 ** b : shl64(a, b, true)) as i64; /** `i64`'s `a >> b`, panicking on a shift by the width or more. */ -export const i64Shr = (a: i64, b: number | Limbs): i64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 ? Math.floor(a / 2 ** b) + 0 : shr(a, b, 64, true)) as i64; +export const i64Shr = (a: i64, b: number | Limbs): i64 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 64 ? Math.floor(a / 2 ** b) + 0 : shr64(a, b, true)) as i64; /** `i64`'s `a & b`. */ export const i64And = (a: i64, b: i64): i64 => { const n = typeof a === "number" && typeof b === "number" ? wordAnd(a, b) : NaN; - return (fits(n, true) ? n : and(a, b, 64, true)) as i64; + return (fits(n, true) ? n : and64(a, b, true)) as i64; }; /** `i64`'s `a | b`. */ export const i64Or = (a: i64, b: i64): i64 => { const n = typeof a === "number" && typeof b === "number" ? wordOr(a, b) : NaN; - return (fits(n, true) ? n : or(a, b, 64, true)) as i64; + return (fits(n, true) ? n : or64(a, b, true)) as i64; }; /** `i64`'s `a ^ b`. */ export const i64Xor = (a: i64, b: i64): i64 => { const n = typeof a === "number" && typeof b === "number" ? wordXor(a, b) : NaN; - return (fits(n, true) ? n : xor(a, b, 64, true)) as i64; + return (fits(n, true) ? n : xor64(a, b, true)) as i64; }; /** `i64`'s `!a`. */ -export const i64Not = (a: i64): i64 => not(a, 64, true) as i64; +export const i64Not = (a: i64): i64 => not64(a, true) as i64; /** `i64`'s `checked_add`: `a + b`, or `undefined` on overflow. */ export const i64CheckedAdd = (a: i64, b: i64): i64 | undefined => (typeof a === "number" && typeof b === "number" && fits(a + b, true) ? a + b : optional(sum64(a, b, 1, true))) as i64 | undefined; /** `i64`'s `checked_sub`: `a - b`, or `undefined` on overflow. */ @@ -1178,13 +1217,13 @@ export const i64WrappingAdd = (a: i64, b: i64): i64 => (typeof a === "number" && /** `i64`'s `wrapping_sub`: `a - b`, wrapped into the type's range. */ export const i64WrappingSub = (a: i64, b: i64): i64 => (typeof a === "number" && typeof b === "number" && fits(a - b, true) ? a - b : sum64(a, b, -1, true)) as i64; /** `i64`'s `leading_ones`: the count of leading one bits. */ -export const i64LeadingOnes = (a: i64): number => leadingOnes(a, 64, true); +export const i64LeadingOnes = (a: i64): number => leadingOnes64(a, true); /** `i64`'s `leading_zeros`: the count of leading zero bits. */ -export const i64LeadingZeros = (a: i64): number => leadingZeros(a, 64, true); +export const i64LeadingZeros = (a: i64): number => leadingZeros64(a, true); /** `i64`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const i64FromBeBytes = (bytes: Uint8Array): i64 => fromBeBytes(bytes, 64, true) as i64; +export const i64FromBeBytes = (bytes: Uint8Array): i64 => fromBeBytes64(bytes, true) as i64; /** `i64`'s `to_be_bytes`: its big-endian bytes. */ -export const i64ToBeBytes = (a: i64): Uint8Array => toBeBytes(a, 64, true); +export const i64ToBeBytes = (a: i64): Uint8Array => toBeBytes64(a, true); /** `i64`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ export const i64TryFrom = (value: number | Limbs): Result => tryFrom(value, 64, true) as Result; /** `i64`'s `Display`: its decimal digits, padded by the formatter's spec. */ @@ -1229,26 +1268,26 @@ export const u128Div = (a: u128, b: u128): u128 => (typeof a === "number" && typ /** `u128`'s `a % b`, panicking on a zero divisor or overflow. */ export const u128Rem = (a: u128, b: u128): u128 => (typeof a === "number" && typeof b === "number" && b !== 0 ? (a % b) + 0 : rem(a, b, 128, false)) as u128; /** `u128`'s `a << b`, panicking on a shift by the width or more. */ -export const u128Shl = (a: u128, b: number | Limbs): u128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 && safe(a * 2 ** b) ? a * 2 ** b : shl(a, b, 128, false)) as u128; +export const u128Shl = (a: u128, b: number | Limbs): u128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 && safe(a * 2 ** b) ? a * 2 ** b : shl128(a, b, false)) as u128; /** `u128`'s `a >> b`, panicking on a shift by the width or more. */ -export const u128Shr = (a: u128, b: number | Limbs): u128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 ? Math.floor(a / 2 ** b) + 0 : shr(a, b, 128, false)) as u128; +export const u128Shr = (a: u128, b: number | Limbs): u128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 ? Math.floor(a / 2 ** b) + 0 : shr128(a, b, false)) as u128; /** `u128`'s `a & b`. */ export const u128And = (a: u128, b: u128): u128 => { const n = typeof a === "number" && typeof b === "number" ? wordAnd(a, b) : NaN; - return (fits(n, false) ? n : and(a, b, 128, false)) as u128; + return (fits(n, false) ? n : and128(a, b, false)) as u128; }; /** `u128`'s `a | b`. */ export const u128Or = (a: u128, b: u128): u128 => { const n = typeof a === "number" && typeof b === "number" ? wordOr(a, b) : NaN; - return (fits(n, false) ? n : or(a, b, 128, false)) as u128; + return (fits(n, false) ? n : or128(a, b, false)) as u128; }; /** `u128`'s `a ^ b`. */ export const u128Xor = (a: u128, b: u128): u128 => { const n = typeof a === "number" && typeof b === "number" ? wordXor(a, b) : NaN; - return (fits(n, false) ? n : xor(a, b, 128, false)) as u128; + return (fits(n, false) ? n : xor128(a, b, false)) as u128; }; /** `u128`'s `!a`. */ -export const u128Not = (a: u128): u128 => not(a, 128, false) as u128; +export const u128Not = (a: u128): u128 => not128(a, false) as u128; /** `u128`'s `checked_add`: `a + b`, or `undefined` on overflow. */ export const u128CheckedAdd = (a: u128, b: u128): u128 | undefined => (typeof a === "number" && typeof b === "number" && fits(a + b, false) ? a + b : optional(sum128(a, b, 1, false))) as u128 | undefined; /** `u128`'s `checked_sub`: `a - b`, or `undefined` on overflow. */ @@ -1264,13 +1303,13 @@ export const u128WrappingAdd = (a: u128, b: u128): u128 => (typeof a === "number /** `u128`'s `wrapping_sub`: `a - b`, wrapped into the type's range. */ export const u128WrappingSub = (a: u128, b: u128): u128 => (typeof a === "number" && typeof b === "number" && fits(a - b, false) ? a - b : sum128(a, b, -1, false)) as u128; /** `u128`'s `leading_ones`: the count of leading one bits. */ -export const u128LeadingOnes = (a: u128): number => leadingOnes(a, 128, false); +export const u128LeadingOnes = (a: u128): number => leadingOnes128(a, false); /** `u128`'s `leading_zeros`: the count of leading zero bits. */ -export const u128LeadingZeros = (a: u128): number => leadingZeros(a, 128, false); +export const u128LeadingZeros = (a: u128): number => leadingZeros128(a, false); /** `u128`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const u128FromBeBytes = (bytes: Uint8Array): u128 => fromBeBytes(bytes, 128, false) as u128; +export const u128FromBeBytes = (bytes: Uint8Array): u128 => fromBeBytes128(bytes, false) as u128; /** `u128`'s `to_be_bytes`: its big-endian bytes. */ -export const u128ToBeBytes = (a: u128): Uint8Array => toBeBytes(a, 128, false); +export const u128ToBeBytes = (a: u128): Uint8Array => toBeBytes128(a, false); /** `u128`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ export const u128TryFrom = (value: number | Limbs): Result => tryFrom(value, 128, false) as Result; /** `u128`'s `Display`: its decimal digits, padded by the formatter's spec. */ @@ -1317,26 +1356,26 @@ export const i128Rem = (a: i128, b: i128): i128 => (typeof a === "number" && typ /** `i128`'s `-a`, panicking on overflow. */ export const i128Neg = (a: i128): i128 => (typeof a === "number" ? -a + 0 : strict(sum128(0, a, -1, true), "negate")) as i128; /** `i128`'s `a << b`, panicking on a shift by the width or more. */ -export const i128Shl = (a: i128, b: number | Limbs): i128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 && safe(a * 2 ** b) ? a * 2 ** b : shl(a, b, 128, true)) as i128; +export const i128Shl = (a: i128, b: number | Limbs): i128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 && safe(a * 2 ** b) ? a * 2 ** b : shl128(a, b, true)) as i128; /** `i128`'s `a >> b`, panicking on a shift by the width or more. */ -export const i128Shr = (a: i128, b: number | Limbs): i128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 ? Math.floor(a / 2 ** b) + 0 : shr(a, b, 128, true)) as i128; +export const i128Shr = (a: i128, b: number | Limbs): i128 => (typeof a === "number" && typeof b === "number" && b >= 0 && b < 128 ? Math.floor(a / 2 ** b) + 0 : shr128(a, b, true)) as i128; /** `i128`'s `a & b`. */ export const i128And = (a: i128, b: i128): i128 => { const n = typeof a === "number" && typeof b === "number" ? wordAnd(a, b) : NaN; - return (fits(n, true) ? n : and(a, b, 128, true)) as i128; + return (fits(n, true) ? n : and128(a, b, true)) as i128; }; /** `i128`'s `a | b`. */ export const i128Or = (a: i128, b: i128): i128 => { const n = typeof a === "number" && typeof b === "number" ? wordOr(a, b) : NaN; - return (fits(n, true) ? n : or(a, b, 128, true)) as i128; + return (fits(n, true) ? n : or128(a, b, true)) as i128; }; /** `i128`'s `a ^ b`. */ export const i128Xor = (a: i128, b: i128): i128 => { const n = typeof a === "number" && typeof b === "number" ? wordXor(a, b) : NaN; - return (fits(n, true) ? n : xor(a, b, 128, true)) as i128; + return (fits(n, true) ? n : xor128(a, b, true)) as i128; }; /** `i128`'s `!a`. */ -export const i128Not = (a: i128): i128 => not(a, 128, true) as i128; +export const i128Not = (a: i128): i128 => not128(a, true) as i128; /** `i128`'s `checked_add`: `a + b`, or `undefined` on overflow. */ export const i128CheckedAdd = (a: i128, b: i128): i128 | undefined => (typeof a === "number" && typeof b === "number" && fits(a + b, true) ? a + b : optional(sum128(a, b, 1, true))) as i128 | undefined; /** `i128`'s `checked_sub`: `a - b`, or `undefined` on overflow. */ @@ -1352,13 +1391,13 @@ export const i128WrappingAdd = (a: i128, b: i128): i128 => (typeof a === "number /** `i128`'s `wrapping_sub`: `a - b`, wrapped into the type's range. */ export const i128WrappingSub = (a: i128, b: i128): i128 => (typeof a === "number" && typeof b === "number" && fits(a - b, true) ? a - b : sum128(a, b, -1, true)) as i128; /** `i128`'s `leading_ones`: the count of leading one bits. */ -export const i128LeadingOnes = (a: i128): number => leadingOnes(a, 128, true); +export const i128LeadingOnes = (a: i128): number => leadingOnes128(a, true); /** `i128`'s `leading_zeros`: the count of leading zero bits. */ -export const i128LeadingZeros = (a: i128): number => leadingZeros(a, 128, true); +export const i128LeadingZeros = (a: i128): number => leadingZeros128(a, true); /** `i128`'s `from_be_bytes`: a value from its big-endian bytes. */ -export const i128FromBeBytes = (bytes: Uint8Array): i128 => fromBeBytes(bytes, 128, true) as i128; +export const i128FromBeBytes = (bytes: Uint8Array): i128 => fromBeBytes128(bytes, true) as i128; /** `i128`'s `to_be_bytes`: its big-endian bytes. */ -export const i128ToBeBytes = (a: i128): Uint8Array => toBeBytes(a, 128, true); +export const i128ToBeBytes = (a: i128): Uint8Array => toBeBytes128(a, true); /** `i128`'s `try_from` another integer: it, or a `TryFromIntError` past the type's range. */ export const i128TryFrom = (value: number | Limbs): Result => tryFrom(value, 128, true) as Result; /** `i128`'s `Display`: its decimal digits, padded by the formatter's spec. */ diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 4bc45aaa4..591123fe9 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -17,21 +17,22 @@ export const DONE: unique symbol = /* @__PURE__ */ Symbol("DONE"); /** The type of `DONE`. */ export type Done = typeof DONE; +/** + * What only the iterators std implements these traits for have, which Rust calls only + * there: declared, not stubbed, so no bundle carries a stub. + */ +export interface Iter { + /** `DoubleEndedIterator::next_back`. */ + nextBack(): T | Done; + /** `ExactSizeIterator::len`. */ + len(): number; +} + /** `Iterator`: `next`, and the methods std's iterators each do their own way. */ export abstract class Iter implements Iterable { /** The next item, or `DONE`. */ abstract next(): T | Done; - /** `DoubleEndedIterator::next_back`, which Rust calls only where std implements it. */ - nextBack(): T | Done { - throw new Error("rust2-ts: an iterator without `next_back`"); - } - - /** `ExactSizeIterator::len`, which Rust calls only where std implements it. */ - len(): number { - throw new Error("rust2-ts: an iterator without `len`"); - } - /** `nth`, as `next` returns it. */ nthItem(n: number): T | Done { for (let i = 0; i < n; i++) if (this.next() === DONE) return DONE; From cdcb5789570a61d20998c0c33198b6cb64e5e553 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 05:54:14 -0700 Subject: [PATCH 3/3] chore(quest): finish ts-mir-size Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-mir-size.md | 54 ----------------------------------------- ts/README.md | 5 ++-- 3 files changed, 2 insertions(+), 58 deletions(-) delete mode 100644 quest/m2/ts-mir-size.md diff --git a/quest/m2/README.md b/quest/m2/README.md index e0a6fa9ad..5d89d936f 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -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` -- [Smaller output from MIR](/quest/m2/ts-mir-size.md) - the codec bundle shrinks back to THIR's gzipped size - [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 diff --git a/quest/m2/ts-mir-size.md b/quest/m2/ts-mir-size.md deleted file mode 100644 index 58ec3c604..000000000 --- a/quest/m2/ts-mir-size.md +++ /dev/null @@ -1,54 +0,0 @@ -# [S] Smaller output from MIR - -## Goal - -[rust2-ts](/ts/README.md)'s bundles on `just ts bench` are no larger than the THIR -lowering's were, without giving up the speed MIR gained: the codec's at most 30629 -bytes minified and 9422 gzipped, and the async handoff's at most 3458 gzipped. - -## Plan - -[#95](https://github.com/kixelated/rust2/pull/95) brought the handoff to 3423 bytes -gzipped. The codec is what remains: 30935 bytes minified and 9898 gzipped, 306 and 476 -over. - -That gap came after THIR's numbers were taken, not from the lowering. On the same -fixtures with #95, these PRs added to the codec: - -- [#89](https://github.com/kixelated/rust2/pull/89) grew `rt/int.ts`. THIR's own output - bundled with #95's `rt/int.ts`, before #87, is 31112 bytes minified and 9666 gzipped, - over MIR's 30094 and 9651. -- [#87](https://github.com/kixelated/rust2/pull/87)'s drop glue with - [#98](https://github.com/kixelated/rust2/pull/98) added 253 bytes minified and 93 - gzipped. -- [#82](https://github.com/kixelated/rust2/pull/82)'s `rt.ByteSlice` added 580 bytes - minified and 156 gzipped. - -Closing the gap means trimming what the codec pulls from the runtime or its drop glue, -without losing speed, or restating the goal against today's runtime. - -Small cuts measured by hand on the handoff and left out, each under 1% gzipped: -hoisting a dispatch's entry case out of its `switch` (3-7 bytes), dropping `unwind` -stores before a statement that can't throw (4), and merging `if` arms whose cleanup is -the same (about 15). - -Tried and dropped: - -- A runtime base class for the scaffolding (the resume-after-finish checks and the - poisoning `catch`), with `resume` per class so its calls stay monomorphic: prototyped - by hand on the 17 one-await classes, 792 bytes smaller minified but 38 larger gzipped. - A base can't read a subclass's `#state`, so only those two pieces move. -- Collapsing an `async fn` whose body only awaits another that only moves its arguments - in (`level2` into `level1` and on): exact, but it removes the depth the handoff - measures, and moq-net's wrappers await `kio::wait`, not such an `async fn`. - -Decided with the maintainer on 2026-10-26, from -[#85](https://github.com/kixelated/rust2/pull/85): the handoff's target is gzipped -only. Its minified gap is the fixture's repeated one-await classes, which only a -shared driver (measured slower or larger) or collapsing the wrappers (which empties -the bench) would close. - -Generators and closures stay out: they measured 1.4-5.5x and 1.3-1.8x slower on the -handoff. Coroutine fields stay private. - -Public API: the translator's output, which changes shape but not behavior. diff --git a/ts/README.md b/ts/README.md index ae4d1b24e..fa3bafabf 100644 --- a/ts/README.md +++ b/ts/README.md @@ -298,9 +298,8 @@ they need. the coroutine transform's state machines, a class each. Against the THIR lowering it replaced, MIR closes about 1,100 of the 1,400 drop refusals in moq-net's census, and runs `just ts bench`'s codec 2-6% faster at the geometric mean and the async handoff - 1.4-5.9x faster at depths 1 to 16 in Node, Bun, and Chrome, for 1% more gzip on the - codec's bundle and 9% on the async fixture's - ([size](/quest/m2/ts-mir-size.md)). Blocks become nested blocks, loops, and `if`s by + 1.4-5.9x faster at depths 1 to 16 in Node, Bun, and Chrome, in bundles no larger + gzipped. Blocks become nested blocks, loops, and `if`s by Ramsey's "Beyond Relooper", except a coroutine's resume function, which enters loops in the middle and becomes a loop over a `switch` on the next block. Temporaries assigned once and read once fold back into the expression that reads them unless that