Repository navigation
feat(ts): wide integers are numbers while safe, and each integer type is branded - #26
Conversation
Quest-Claim: c50f424a11193534
…oint) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…kpoint) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Ranges of 64- and 128-bit integers carry their type, so they step past 2^53 into limbs, and max and min compare limbs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…sed or wrapped Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 691b800bf7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
`!`, `&`, and `^` on two safe `i64` or `i128` numbers can make -2^53, which was left a number, so it compared unequal to the same value as limbs. The lang fixture now checks each result equals its bytes read back, and the wide sweep adds small and safe-edge negative operands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Every integer type is its own TypeScript type, `rt.u8` through `rt.i128` and `rt.usize`, a number with a type-only brand (limbs past 2^53 for the wide four), so `tsc` refuses one type's value where another's goes. The brand is optional, so a plain number such as a literal passes for any type. A cast that keeps the value is now `x as rt.u16`. The lang fixture's `brands.ts` holds mixes `tsc` must refuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
|
Summary before merge at 83640b3:
(written by Opus 5.5) |
A u64 is now a number while safe, or limbs, so the hand-written executor and the bench's translated routes make each value with `rt.u64.of` and read the sum with `rt.u64.toBeBytes`, rather than as a bigint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…e async spike (#16) * chore(quest): claim quest/m2/ts-async-spike.md Quest-Claim: b76999a56b3a7148 * wip(ts): async spike, generators from THIR with drops Checkpoint: the task fixture, its FIFO executors in Rust and TS, and the generator prototype passing the trace differential test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * wip(ts): async spike, MIR state machine prototype Both prototypes pass the task fixture's trace differential test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * wip(ts): async spike, hoisted generators and the handoff bench's native side Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * wip(ts): async spike, generators win; delete the MIR state machines Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * wip(ts): async spike cleanup: one closure path, trimmed runtime, text diffs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): census-safe drop analysis, and refuse a pattern-bound parameter that drops Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): an async block drops only what it owns, on the path that moves it A block borrowing a value that drops no longer drops it when cancelled, a block created on one branch moves its captures out on that branch only, and a block drops the captures it owns when it returns or is cancelled mid-await, in capture order after its locals. The task fixture's `blocks` covers each against native Rust. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): a loop broken inside a drop scope still ends its match arm Merging main's tail loops taught `breaks_out` about `try` blocks, which wrap a loop body that holds a value that drops. Without that, the task fixture's `turns` falls through to the next arm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): rt.spawn yields after its poll budget even when a task wakes itself `run` cleared `scheduled` before its batch, so a task waking itself queued another microtask, which ran ahead of the `setTimeout` continuation and starved timers. `scheduled` now stays set until the queue drains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse dropping a borrow of a value that drops through an untyped holder An `Arc`, a `Mutex`, and a generic class's glue drop their contents through `[Symbol.dispose]`, but a reference is its referent in JS: `Arc<&Noisy>` dropping its last count would drop the `Noisy` it borrows. Such a holder now fails with its span, as the reject fixture's `shared_borrow` shows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse an operand that drops ahead of an .await in the same call `consume(guard, fut.await)` moved `guard` into a temporary before the `yield`, where no scope dropped it if the future was cancelled. It now fails with its span (3 in moq-net, none in kio). The README and the Drop quest list it with the `Arc` of a borrow, and the quest's counts follow the census after main's merges. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): rt.spawn rejects when a finished future panics as it drops The completed future's disposal ran outside the `try` that settles the promise, so a panic there left it pending and escaped the scheduler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): a let that binds part of a value that drops leaves the rest to drop `let [kept, _] = pair` moved `pair` out whole through the destructuring shortcut, so the skipped guard never dropped. A place now binds part by part and keeps the rest for its scope to drop, and a temporary bound only in part fails with its span, as a `match` or `if let` on one does (27 in moq-net). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): drop a pinned box, and refuse two more drops a cancel or pattern skips - `Pin<P>` drops as its pointer does, so `Pin::new(Box::new(guard))` drops the guard. - `Some(..)` names no field, so a `match` or `if let` on a temporary that drops no longer counts it as moving the payload out (3 in moq-net). - A value that drops, assigned to a place whose index awaits, would wait out the `.await` in a temporary no scope drops: it now fails with its span. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): a ref binding borrows its place, and `let _` still evaluates one `let ref r = g` moved and cleared `g`, so nothing dropped it; it now borrows `g` where it is, and `let ref r = make()` owns the temporary it keeps alive to the end of the block. `let _ = guards[i]` skipped the place entirely; it now evaluates the index and its bounds check, moving nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): copy an async block's Copy captures, and refuse replacing an Arc in place `async move { read(tally) }` shared the caller's `Tally` object, so a later change reached the future; a by-value capture is now copied as any `Copy` aggregate is. `*dst = src` through a `&mut Arc` copied fields into the one JS object every clone shares; it now fails with its span, as does the same through a waker, context, or future. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): a ref temporary keeps its block scoped, and see Option and Poll through wrappers A block whose `let ref` keeps a temporary that drops was flattened into the enclosing scope, delaying the drop past the block's value; it now keeps a scope of its own. `Option` and `Poll` now check what their payload can already erase to through `Option`, `Poll`, `Box`, `Pin`, and references: `Poll<Option<Poll<u8>>>` and `Option<Poll<()>>` collided and are refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): replace a generator's mutable borrow in place, and decide recursive drops whole An async block that replaced a variable it borrows mutably rebound its own parameter, so the caller never saw the write; it now writes into the caller's object, as `*r = v` does. `drops` cached a provisional `false` for a type that reached one still being decided, so `Inner` in a cycle with a dropping `Outer` never dropped; a `false` is now cached only once the cycle's first type is decided. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): an if let takes a temporary whole from every variant, and refuse Poll outputs `if let None = Some(guard)` passed the whole-move check, since `None` has no fields, and the failed test leaked the guard. An `if let` or let chain now asks `leaves_drop`, which also counts the other variants and is exact where `takes_all` was conservative (moq-net: 6 fewer refusals). An async body whose output can already be `rt.Pending` is refused, as `Poll<Poll<T>>` is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): read through a Box without moving it, and close two more refusal gaps `boxed.id` on a `Box` that drops lowered the deref as a move, clearing the box so a second read threw and nothing dropped it; reading through an owning pointer now reads it in place (moq-net: 2 fewer refusals). A ternary `match` on a temporary that drops skipped the check a `match` makes, and replacing a `Pin<Arc<_>>` through a reference reached every clone; both now fail with their span. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): regenerate snapshots after merging main Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * chore(quest): count iterators that own a value that drops in the Drop quest Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): drive the task fixture's u64s through rt.u64, after #26 A u64 is now a number while safe, or limbs, so the hand-written executor and the bench's translated routes make each value with `rt.u64.of` and read the sum with `rt.u64.toBeBytes`, rather than as a bigint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): regenerate snapshots after merging main Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse three more drops JS would skip, from Codex's review - Replacing a `MutexGuard` through a reference: the guard is its mutex in JS, so the copy would change the other mutex. - A std mapping taking a value that drops by value, unless it moves every such operand into its result or a closure: `unwrap_or`, `ok_or`, `map_or`, `then_some`, and `Result::ok` discard one. - A field read through a `Box` made in place: the deref of a temporary box is part of the temporary, which drops at the end of the statement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse a rest pattern that moves values that drop out of a place Its slice copy would drop the elements, and so would the array they stay in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse replacing a pinned reference through a reference A reference is its referent in JS, so copying the new one's fields in would change what the old one pointed at. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): refuse an array repeat of a value that drops `[v; 0]` drops `v` at once, which no element of the array would. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): keep a move inside the arm of a `match` or `if` that takes it An expression that moves a value that drops clears its place with a statement of its own, so it is not simple enough for `?:`: both arms' statements would run, and the second would read the `undefined` the first left. The task fixture's `blocks` moves a guard on each arm of a `match`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --------- Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Problem
The wide integer spike: rust2-ts translated
u64,i64,u128, andi128tobigint, which the design kept untiljust ts benchmeasured it. #15's gap tables putbigintat up to 55 ns per varint and 30-140 ns per frame header.Approach
Measured four representations on the codec's varint, frame, and message paths, and on an arithmetic loop, then shipped the winner for all four types:
bigint, today's output (main's translation as a route).{ hi, lo }object (first shipped, then measured as a route).uint32numbers in locals, fields, arguments, and tuples, boxed only insideResult, with a hi register for returns, as Scala.js does (a hand-written translation of the bench paths).numberwhile the value is a safe integer, and limbs past 2^53. Each value has one form, sox === 0is exact.Limbs allocate per value, and a V8 object costs about what a small
bigintdoes, so limbs lost decode in V8. Unboxed halves won decode, but would lower every Rust value to two JS values throughout the translator. Number-or-limbs ties unboxed halves on the codec, beats it on sums, keeps one JS value per Rust value, and reads best:frameStart: 0,return rt.Ok([first, rest]),this.frameStart !== 0. It's also what moq's hand-written TS does for varints.What changed:
rt.ts:rt.u64,rt.i64,rt.u128, andrt.i128take numbers or limbs. Sums, products, quotients, shifts, and bitwise ops stay in numbers while the result is safe. Past 2^53 they fall back on 64-bit limb arithmetic: add, sub, mul by a 32-bit factor, and long division by a 32-bit divisor are computed on the halves, the rest throughbigint. 128-bit limbs go throughbigint.rt.u64.of(0xffffffffffffffffn). A cast that keeps the value isx as rt.u64,x as u32isrt.u64.low(x), and every other operator and comparison is a runtime call.==against a literal stays===.rt.u8throughrt.i128andrt.usize: anumberwith a type-only brand, or limbs past 2^53 for the wide four. The brand is an optionalunique symbolkey, sotscrefuses au16where au8goes, or au64where ani64goes, while a plain number such as a literal passes for any.fixtures/lang/brands.tsholds five such mixes under@ts-expect-error.new rt.Range(a, b, rt.u64)) to step past 2^53, andmax/min/max_by_keycompare limbs throughrt.lt. Aforover a wide range iterates thatrt.Range.differential.ts wideruns every 64- and 128-bitlangcase over each pair of values next to a power of two (2^k ± 1 at limb, safe-integer, and sign edges, -2^k ± 1 for small and safe-edge negatives, plus four unstructured values) and 14 shift amounts. That's 175k cases, about 5 s. Each result must also equal its bytes read back, which a wide value does only in its one form. A cast case now prints its text as well as its bytes, which shows a value's sign and form.iterssteps ranges across 2^53 and up tou64::MAX. Of 18 hand mutations of the runtime's fast paths (a carry, a shift, the long division, the canonical form, a cast), the sweep caught all 18 and the random cases alone missed 6.Numbers
ns per item: the median of 25 interleaved samples of 15 ms. Load average 9-23 on the shared 32-thread host. A/A noise at the median workload was 2.6% (Node), 5.9% (Bun), and 4.9% (Chrome).
Every workload, with limbs for every value
Node v24.21.0
Bun 1.4.2
Chromium 155
Arithmetic, ns per item over 1024 items, as the translator emits each kernel.
accountingadds, subtracts, and comparesu64s around 2^40.rescalecomputest * 90_000 / 1_000_000withtnear 1.7e12, so the product passes 2^53. Native Rust takes 1.0 and 1.6 ns.A loop whose values all pass 2^53 takes 77 ns in Node, against
bigint's 35.Bundle: the translated codec grows from 31.3 KB to 37.5 KB raw, and from 8.8 KB to 11.0 KB gzipped, all of it runtime.
Verdict: number-or-limbs is the representation for all four types. It matches unboxed halves on the consumer's paths, while unboxed halves would add a second lowering for every value. Its loss is arithmetic past 2^53: 1.5-1.9x slower than
bigintwhen a product passes 2^53, and 2x when every value is past it.Decisions
Type-level brand, decided by the maintainer:
rt.u8throughrt.i128, type-only, sotsctells every integer type apart at no run-time cost.Branding costs nothing at run time: TypeScript erases it, and the translated codec bundles to the same JS before and after (byte-identical unminified; 48372 bytes minified both ways, and 13493 vs 13492 gzipped, the difference being Bun's choice of short names). With identical JS,
just ts benchcan only show noise. A run on the branded output passed, but the shared host was loaded (A/A noise 4.9-7.2% at the median, and hand-written moq 20-50% slower than in the table above), so its numbers are not a new baseline.Impact
rt.<its Rust name>:u8-u32,usize, and the signed ones instead ofnumber, andu64,i64,u128, andi128(a branded number orrt.U64Limbs, and so on) instead ofbigint. A cast that keeps the value isx as rt.u16. Safe literals are numbers, operators and comparisons on them arert.u64.*calls, and narrowing isrt.u64.low(x).rt.ts: new exported typesu8throughi128,usize, andisize, the wide four's*Limbs,Limbs, andWideInt. Newrt.lt.rt.u64and the other three areWideInts withof,cast,low, comparisons, and bitwise methods.rt.maxandrt.mintake numbers only, andrt.option.mapOrinfers its type from its closure, not its fallback.Int<T>is no longer constrained, andbigintappears nowhere.rt/iter.ts:Rangetakes an optional integer type,RangeInclusive's third argument is anInt<T>, andmax/min/maxByKey/minByKeytakeLimbskeys where they tookbigint.differential.tstakes an optionalwideargument.MIGRATE.mdentry: rust2-ts is unreleased (0.0.0,publish = false) and has none.Alternatives
rescaleand in JavaScriptCore'saccounting, and tied on the codec. It needs a pair-valued lowering of every expression, pattern, field, argument, and return. Not done.bigintkept: slowest on encode in every engine, and on everything in JavaScriptCore.Follow-ups
bigint.from_halvesgoes throughrt.u64.or(rt.u64.shl(hi, 32), lo), and a fused form is likely worth measuring.rescale's allocations.just ts benchhas no way to load another translation as a route. This spike and perf(ts): number-only small ints, inline from_be_bytes, and indexed byte patterns #15 both patchedmain.tslocally to do it, and a--route name=diroption would keep A/B runs reproducible.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn