Skip to content

perf(ts): pass byte slices as their parts, and return Results of them in a slot - #160

Merged
moq-bot[bot] merged 38 commits into
mainfrom
quest/m2/ts-byte-views/arguments
Nov 1, 2026
Merged

moq-bot[bot] merged 38 commits into
mainfrom
quest/m2/ts-byte-views/arguments

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Claims quest/m2/ts-byte-views/returns.md, which absorbs arguments.md.

Problem

Reading through a translated cursor makes an object per split, and a fallible read returns rt.Ok([value, new rt.ByteSlice(...)]): three objects per varint. Passing &[u8] parameters as their parts alone gained nothing in Chrome; the win needs the return side too.

Decided

  • 2026-10-28, maintainer: fold returns into this PR as one quest.
  • 2026-10-28, maintainer: option 1. The translator implements e1e, a slot whose fields start as undefined. Land only if Chrome wins on both varint and frame decode, with the A/A under 5% at the median and the max.
  • 2026-10-29, maintainer: park as a draft until the host is quiet.
  • 2026-10-31, maintainer: judge on the family-level bar. Chrome must win varint decode and frame decode on family geomeans, with each family's A/A under 5%; the per-workload A/A max is dropped, since fix(ts): time each bench round in fresh workers #360's bench shows some workloads are bimodal per worker. First fix the 8-byte leading-ones regression (1.16x main), measuring the alternatives, and pick the one that removes it without giving back the decode wins. time/ and btree/ must not regress at the family level.
  • 2026-10-31, maintainer: option 1 for Chrome's btree/ gap (1.06x main, from take_u64 rebuilding the slice ordered passed as parts): keep a &[u8] parameter whole when the callee would rebuild it anyway, worked out across calls that pass a slice along, covering recursion, mutual recursion, and a function only some of whose callers pass parts. Same bar, with btree/ and time/ judged against main-vs-main noise.

Approach

  • Parameters as parts: a function only translated code calls takes a &[u8] / &[u8; N] as data, start, length only when its body holds that slice as parts and never makes its object.

    • Otherwise it takes the slice whole. Parting such a slice only moves the object from the caller into the callee. Through take_u64 rebuilding the slice ordered passed, that made Chrome's btree/ 1.06x main.
    • Translator::parted runs the callee's own analysis (Fx::new on a throwaway body, with refusals held back) and checks two things: whether the parameter lands in scalars, and whether no read makes its object (remade, which unheld now reports).
    • A body's answer depends on what its callees take. So every instance a call reaches while one is settling starts at all of its slices and drops those its body would make, until a full pass changes nothing. This is a greatest fixpoint in a deterministic order, and it covers recursion and mutual recursion.
    • In the codec, the varint readers keep parts. Decoder::new, take_u64, and the bench's entry points take the slice whole, as on main.
  • Results in a slot: a function of the crate's that only translated code calls, and that returns Result<T, E>, qualifies when:

    • T holds byte slices and scalars, with no enum;
    • E is one JS value that is never undefined (not (), an Option, or a type parameter).

    It hands back the Ok's parts through a module-level const slot shared per T, such as readOut. There are two return shapes:

    • First part a number (read, read_quic, read_leading_ones): the function returns that number, or undefined for an error. The rest of the parts go in the slot, and an error goes in the slot's err field. The caller reads err right after the call and branches on it.
    • Otherwise (Decoder::slice, bytes): the function returns E | undefined, with every part in the slot.

    Details shared by both:

    • Slot fields start as undefined as never.
    • Callee: it holds _0 as parts and writes the slot at the JS return, after its drops. An assignment of _0 that runs straight into a return returns right there. ?'s FromResidual::from_residual is a producer, so an early return Err stays a bare return.
    • Caller: it reads every slot field into a local right after the call, before anything else can write it. A call into _0 that returns straight away through the same slot is just return call(...).
    • A caller that keeps the result whole (a collection, inspect_err, a closure) rebuilds the Result at once. A function used as a value is wrapped in an arrow that rebuilds it.
    • Std's functions are not slotted. Their callers already take their results apart.
    • A slot sits beside the first function in source order that writes it, so where it lands doesn't depend on translation order. It takes that function's instance suffix, such as leadOut$u16, and the census of names declares each …Out.
  • Why the number is returned: the old ABI wrote every part into the slot. The slot is long-lived, and V8 boxes a number past 2^31 written into it by a function too big to inline (read_leading_ones). That put Chrome's 8-byte leading-ones varints at 1.16x main on the old bench and 1.06-1.09x on fix(ts): time each bench round in fresh workers #360's. A slice's bytes stay in the slot, since returning them gave back frame decode.

Alternatives measured for the 8-byte case, in Chrome, against this branch before the change (three runs each):

shape varint decode frame decode leading-ones 8B
p_0 starts at 0 1.13 1.24
return the number, write err on every call 0.93-0.95 1.01-1.08 0.79-0.83
return the number, write err only on failure 0.96-0.97 0.96-1.02 0.87-0.92
return the first part, slices included gave back
translator, numbers only, flag read after the parts 0.94-0.96 1.06-1.08 0.80-0.81

The last row lost about 7% of frame decode to the failure-only hand edit. Reading err first and branching on it recovered that, which is the shape here.

Measurements

  • Host: an Intel Core Ultra 7 270K Plus.
  • Bench: fix(ts): time each bench round in fresh workers #360's, 25 rounds of 2 ms in fresh workers.
  • Pinning: taskset to P-cores 4-7.
  • Date: 2026-10-31.
  • Versions: main at 5433a3e8; this branch at 6c614fb7, whose codec output the review fixes keep.
  • Runs: four of this branch against main, and two of main against itself, to size the noise.
  • What differs: the two translations differ only in coding/varint.ts, coding/decode.Decoder.ts, and the differential harness.ts. bench.ts, which holds the btree/, time/, iter/, and order/ workloads, is byte-identical to main's.

Time as a fraction of main's, family geomeans per run (below 1 is faster), Chrome, with the family's A/A gap:

run varint decode frame decode leading-ones 8B btree/ map time/ add, compare, convert
1 0.83 (0.8%) 0.86 (0.2%) 0.87 0.97 (1.0%) 0.95, 1.03, 1.01
2 0.80 (0.3%) 0.88 (3.1%) 0.94 1.05 (9.6%) 1.00, 1.19, 1.01
3 0.81 (0.5%) 0.84 (2.6%) 0.88 1.01 (0.7%) 0.99, 1.00, 0.99
4 0.82 (0.2%) 0.84 (0.3%) 0.85 0.97 (6.4%) 1.06, 0.99, 0.99
main against main 1.00, 1.00 0.99, 1.00 1.05, 1.02 0.92 and 0.87, 0.98 and 1.01, 1.00
  • Decode bar: met. Chrome's varint and frame decode win in every run, with family A/A within 3.1%.
  • btree/ and time/: within main-against-main noise. That noise is up to 5% for btree/ and 13% for time/ add, on identical code. The one outlier, run 2's time/ compare at 1.19, did not repeat (1.03, 1.00, 0.99). It is identical code on both sides.
  • Leading-ones 8B: 0.85-0.94x main, against 1.06-1.09x before the slot returned the number.
  • Other engines, runs 1-4:
    • Node: varint 0.68-0.73, frames 0.83-0.91.
    • Firefox: varint 0.86-0.91, frames 0.93-0.95.
    • Bun: varint 0.81-0.95, frames 0.90-0.99, with A/A up to 7%.
    • btree/ 0.97-1.08 across the three, with Node's 1.08 in run 3 beside a 39% A/A on that run's time/ add.
  • Before this rule (head a30214d5, 2026-10-31): Chrome btree/ was 1.07, 1.15, 1.01 in three runs and 1.06, 0.99, 1.07 with routes swapped. Hand-restoring ordered and take_u64 to main's text took it to 0.97, 0.99, 0.98, which the rule now does.

Impact

  • Public API: none in Rust. Roots and closures keep their TS signatures. A non-root function's generated TS signature changes:
    • a byte-slice parameter its body holds as parts and never makes is taken as data, start, length;
    • a slotted Result returns its first number or its error, with the rest in a …Out slot exported beside it.
    • MIGRATE.md says both. The lang fixture's hand-written brands.ts now passes tail_break its slice whole, since that function makes it in a loop.
  • Each slot keeps its last payload (a Uint8Array reference) alive until the next write.
  • Translation runs a parted function's analysis once more per fixpoint pass. The local test suite took 416-526 s, against 551-607 s at the previous head.
  • Consumer: moq's codec (Decoder::slice, bytes, and varint::read*). just ts moq translates and type-checks it.

Alternatives

  • Byte slices returned #138's per-function scratch was slower, because forwarding copied between scratches. One slot per payload type makes forwarding free.
  • Writing the slot at each Ok assignment would be wrong when drops run between that assignment and the return. So the writes happen at the return, and an assignment returns early only when nothing runs in between.

Checks

  • just ci passes locally on this head, after merging origin/main (5433a3e8).
  • args::args runs differentially against native wasm32 on 50k random inputs. Its new arms cover:
    • a slice passed along to a function that takes it whole (relay), so it is taken whole too, and one passed to a function holding parts (relay_sum), which stays parts;
    • recursion that makes its slice at the bottom (last_run, whole all the way down);
    • mutual recursion that only splits (evens and odds, both parts);
    • mutual recursion where one side makes the slice (ping whole, pong parts);
    • callees reached both from callers holding the slice whole and from ones holding its parts (sum, relay, relay_sum on stored halves and on a parameter).
  • returns::returns keeps its earlier coverage and adds:
    • a slotted function of no arguments (fixed);
    • a ? whose residual error is a slice and a byte, converted (wrapped).
  • Planted bugs:
    • Dropping the remade test, so a slice the body makes stays parts, fails the lang snapshot.
    • An error path that skips writing err made the differential report 88 differences.

Review

  • Reviewer: quest review with Codex, gpt-6.1-sol at medium effort, against origin/main (fbcd2582).
  • Round 1 on 6c614fb7 raised two findings, both fixed in bfa1dbbc and reproduced first with new lang cases:
    • high: a slotted producer of no arguments panicked, from &args[0] in scalar.rs. produced_copy now reads the first argument only if there is one. The case is returns::fixed.
    • medium: ? on a residual held as parts passed From::from only the error's first part, which tsc rejected. The Err is now built from its variant's parts. The case is returns::wrapped.
  • Round 2 on bfa1dbbc: no findings.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 7 commits October 27, 2026 23:43
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Byte slices passed as parts perf(ts): pass byte slices to translated functions as their parts Oct 28, 2026
kixelated and others added 2 commits October 28, 2026 19:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated and others added 3 commits October 28, 2026 22:04
A function only translated code calls that returns a Result of byte slices
and scalars returns its error or undefined and writes the Ok's parts into a
slot shared per payload type, whose fields start undefined so V8 keeps them
tagged. Callers read the slot at once; a call returned as is forwards it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/expected/refs.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title perf(ts): pass byte slices to translated functions as their parts perf(ts): pass byte slices as their parts, and return Results of them in a slot Oct 29, 2026
kixelated added a commit that referenced this pull request Oct 29, 2026
…s reserve

A handle never split nor taken from a Bytes is bytes' vector kind, whose
offset and capacity rt.BytesMut now keeps exactly, so it grows and panics
as Vec does. Once storage was shared or taken, bytes' capacity depends on
which handles dropped, so reserve throws a rust2-ts error past 512 MiB,
counting what the handle read past; below that, bytes' storage stays
under 1 GiB and grows without panicking whichever handles dropped. The
stock fixture's caller names inputs at the limit's edges, and a runtime
test checks the throw. Drops the bench quest, which #160 fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The slot is now one of main's Item::Value module values, whose pure mark goes
only on a call or new, since Rollup warns of one on a literal. Only functions
take parts or a slot, as main lowers thread-locals' consts as instances too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated added a commit that referenced this pull request Oct 29, 2026
* chore(quest): claim quest/m2/ts-model/bytes.md

Quest-Claim: 52d3e75868421f10

* feat(ts): map bytes' BytesMut, Buf, and BufMut without config

BytesMut is an rt.BytesMut, a window of storage that split_to, split, and
freeze share without overlapping. Buf and BufMut of Bytes, BytesMut, &[u8],
and Vec<u8> map to rt/bytes.ts with bytes' panics; the rest of bytes
translates from its MIR. BytesMut's capacity, which depends on whether
other handles dropped, is refused. moq's model.toml drops its BytesMut
binding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(quest): plan a BytesMut's exact capacity

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(quest): title the BytesMut capacity quest as quest check requires

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ts): refuse BufMut::put of a crate's own Buf into a BytesMut

bytes calls the source's copy_to_bytes or its chunk and advance by whether
the BytesMut's capacity is zero, which depends on whether other handles of
its storage dropped. rt.BytesMut can't tell, so the calls a crate's own Buf
sees could differ. Put into a Vec<u8> stays mapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ts): throw on Bytes::slice_ref of bytes from other storage

Rust may share storage that the translation copies, such as a &'static
str's bytes, which as_bytes encodes anew, so a subset from another buffer
may be one Rust accepts. Throw a rust2-ts error there instead of
panicking as if Rust would.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ts): track when a BytesMut's storage kind depends on drops

bytes keeps shared storage shared when it grows with no other handle alive,
and makes it a vector when one is, so after shared storage grows, after
From<Bytes>, and after a vector's put of a Bytes or BytesMut, its kind is
unknown here. A reserve past usize::MAX, whose panic message is "overflow"
for shared storage and "capacity overflow" for a vector, throws a rust2-ts
error there instead of guessing. The stock fixture reserves past
usize::MAX only into handles sure of their kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ts): grow a BytesMut vector as Vec does and limit shared storage's reserve

A handle never split nor taken from a Bytes is bytes' vector kind, whose
offset and capacity rt.BytesMut now keeps exactly, so it grows and panics
as Vec does. Once storage was shared or taken, bytes' capacity depends on
which handles dropped, so reserve throws a rust2-ts error past 512 MiB,
counting what the handle read past; below that, bytes' storage stays
under 1 GiB and grows without panicking whichever handles dropped. The
stock fixture's caller names inputs at the limit's edges, and a runtime
test checks the throw. Drops the bench quest, which #160 fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(quest): name the std calls bytes' getters need in ts-unsafe

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kixelated and others added 5 commits October 29, 2026 12:36
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d btree/ aren't

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated and others added 6 commits October 31, 2026 16:43
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/src/lower/body.rs
…vironment

Main's `lowers` now takes the typing environment of the body naming the
instance; `inside` has none, so it takes the crate's, as `translated` does.
Regenerates the lang fixture's snapshots after the merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated and others added 3 commits October 31, 2026 19:09
# Conflicts:
#	ts/fixtures/lang/expected/core/option.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
A parameter passed as its parts that the body makes into an object, at
entry or at a use, only moves that object from the caller into the
callee. Through `take_u64` that made Chrome's `btree/` workloads 1.06x
`main`'s time. `parted` now runs the callee's own analysis and keeps a
slice as parts only where the body holds it so and never makes it. A
body's answer depends on what its callees take, so the instances a call
reaches settle together as a greatest fixpoint, recursion included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…al's whole error

A slotted producer copies none of its arguments, and may take none, so
reading its first argument panicked. `?` on a residual held as its parts
passed `From::from` only the error's first part; it now makes the whole
`Err` from that variant's parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review November 1, 2026 03:43
@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 638a5870-5311-4561-8f42-92ce1677d34f
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@kixelated
kixelated enabled auto-merge (squash) November 1, 2026 03:44
@moq-bot
moq-bot Bot merged commit 43f05be into main Nov 1, 2026
@moq-bot
moq-bot Bot deleted the quest/m2/ts-byte-views/arguments branch November 1, 2026 04:08
moq-bot Bot pushed a commit that referenced this pull request Nov 1, 2026
Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: moq-bot <moq-bot@users.noreply.github.com>
kixelated added a commit that referenced this pull request Nov 1, 2026
A function only translated code calls that returns a Result of numbers
alone, such as Decoder::varint's Result<u64, E> or a Result<(i64, u64), E>,
now hands back through #160's slot: it returns the first number, or
undefined with the error in the slot, and writes any other numbers there.
A local with no slice is held as its parts only where it meets a slot, and
a tuple made where its parts are taken apart is never made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
moq-bot Bot pushed a commit that referenced this pull request Nov 1, 2026
* chore(quest): claim quest/m2/ts-byte-views/README.md

Quest-Claim: 00e3717952d0d17a

* perf(ts): return a Result of numbers alone in a slot

A function only translated code calls that returns a Result of numbers
alone, such as Decoder::varint's Result<u64, E> or a Result<(i64, u64), E>,
now hands back through #160's slot: it returns the first number, or
undefined with the error in the slot, and writes any other numbers there.
A local with no slice is held as its parts only where it meets a slot, and
a tuple made where its parts are taken apart is never made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(ts): update results' snapshot for #381's merge

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(ts): regenerate core/result.ts's snapshot for #384's merge

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: quest fixer <quest-fixer@localhost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant