Skip to content

perf(ts): hold a struct's private byte slice field as its parts - #379

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

moq-bot[bot] merged 6 commits into
mainfrom
quest/m2/ts-byte-views/fields

Conversation

@kixelated

@kixelated kixelated commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Finishes quest/m2/ts-byte-views/fields.md.

Problem

Decoder::buf is an rt.ByteSlice, so every read that advances the cursor makes a new one: self.buf = new rt.ByteSlice(...) per varint and per slice. #160 passes and returns slices as their parts, but the field still makes an object at every update.

Approach

  • Representation: a struct's private (not pub) &[u8] or &[u8; N] field is three JS fields, bufData, bufStart, and bufLen (StructField::Slice). This covers a crate's own structs, classes included, whose constructors take the three in its place.
  • Reads: a read is new rt.ByteSlice(o.bufData, o.bufStart, o.bufLen). Everything that already takes a made slice apart reads the fields directly: parts arguments, lengths, indexing, and splits. A read that needs the slice whole, such as Debug, Clone, PartialEq, or a std call, makes it there.
  • Scalar pass: a copy of the field gives parts rather than one object's, so a local such as slice()'s holds them as variables. Parts read from fields are not steady, so they are read into variables at once.
  • Writes: every write writes all three fields, through one helper (assign_to). This covers a direct store, mem::take, replace, and swap, an alias of &mut self.buf, and a closure's capture by &mut. An rt.Place of the field, which a &mut passed on or a raw pointer makes, gets a setter over the three. A part that reads a field written before it is read first.
  • Construction: a struct literal takes a made slice's parts, or a path's members. Anything else is held in a path first.
  • Closures: a closure capturing a slice held as steady parts captures them without making an object, as bytes()'s inspect_err does.

The codec's hot path is now the spike's hand edit: read(self.bufData, self.bufStart, self.bufLen, self.form), then three field writes from the slot.

Choices made without asking

  • Every &mut works rather than keeping the field whole. The quest planned to leave a field whole crate-wide on any &mut other than mem::take. Writing all three fields through every write path, rt.Place included, made that pass unnecessary, so nothing keeps a field whole.
  • Plain fields, not #private. A type's functions are module functions, not class methods, so they can't reach # fields, and Object.assign, which writes a whole object through a &mut, doesn't copy them.
  • Field names. The parts are named as perf(ts): pass byte slices as their parts, and return Results of them in a slot #160 names a slice parameter's parts: Data, Start, and Len.

Measurements

  • Host: nixos2, an Intel Core i7-8700 (12 threads), quiet. desktop was loaded at 29.
  • Date: 2026-11-01.
  • Bench: just ts bench --route main=<main's translations>, three runs.
  • What differs: main at 826d56d7 against this branch at 4ffb5a9b. The two translations differ only in coding/decode.Decoder.ts.

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

engine varint decode frame decode frame decode, 3B reads
Chrome 0.89 (0.4%), 0.91 (0.9%), 0.89 (0.5%) 0.87 (2.4%), 0.91 (0.0%), 0.89 (0.1%) 0.95, 0.94, 0.89
Firefox 0.71, 0.70, 0.70 0.82, 0.80, 0.81 0.89, 0.92, 0.93
Node 0.92, 0.91, 0.89 0.94, 0.94, 0.94 0.94, 0.96, 0.97
Bun 0.80, 0.75, 0.76 0.80, 0.76, 0.66 0.90, 0.83, 0.85
  • Bar: met. Chrome's varint and frame decode win in every run, past their families' A/A.
  • Nothing meaningfully slower.
    • Varint and frame encode, group, iter/, time/, order/, and btree/ sit within their A/A in every engine.
    • Chrome's btree/ was 1.04, 1.11, and 0.94, with that family's A/A at 6%, 4%, and 13%. time/ord was 1.06, 1.09, and 0.89. Node's time/checked-add was 1.15, then 1.03 and 0.96.
    • All of those workloads run byte-identical code on both sides, as perf(ts): pass byte slices as their parts, and return Results of them in a slot #160 found.

Impact

  • Output, breaking: a struct's private &[u8] or &[u8; N] field is fooData, fooStart, and fooLen in its interface or class, and a class's constructor takes the three in its place. A pub field is still an rt.ByteSlice. Recorded in ts/MIGRATE.md.
  • No Rust API change.

Consumer: moq-net's Decoder. just ts moq translates and type-checks it with buf as its parts.

Alternatives

  • Keep a field whole on any &mut but mem::take, crate-wide, by rerunning translation until settled (the quest's plan): more code, and it loses the split for a crate that passes &mut self.buf on.
  • Keep a field whole where a read makes its object (as perf(ts): pass byte slices as their parts, and return Results of them in a slot #160 does for parameters): that would keep Decoder::buf whole, since rest() and Debug read it whole. The quest decided fields split anyway.

Follow-ups

  • The epic's remaining work, now in its README: Decoder::varint still returns an rt.Ok per read, and frame_header an rt.Ok of a pair, since only Results holding a slice return in a slot.
  • A derived Clone copies the field through <&[u8] as Clone>::clone, which the scalar pass doesn't take apart, so it makes the slice and takes it apart again. It is off the hot path and left as is.

Review

  • Reviewer: Codex, gpt-6.1-sol, medium effort, via quest review --base origin/main.
  • Round 1 (head 24aa64ff), one finding:
    • [P1] Aggregate optimizations indexed a struct literal's operands by Rust field, and a split field's three parts shifted them. Fixed in bb9b3cb3: parts_of regroups the three. A differential case reproduced it first.
  • Round 2 (head bb9b3cb3): no findings.

Checks

  • just ci passes locally on bb9b3cb3.
  • The differential tests against native wasm32 cover the new fields::fields case: reads and splits, mem::take, replace, and swap, &mut passed on and kept, a closure writing the field back, Copy, Clone, PartialEq, and Debug, a constant's and a static's, a generic struct, a class, a pub field, and an immutable struct literal.
  • just ts moq passes.

🤖 Generated with Claude Code

kixelated and others added 5 commits November 1, 2026 01:17
A private &[u8] or &[u8; N] field of a struct is three JS fields, its bytes,
start, and length, so a cursor advancing through it makes no object. A read
that needs the slice whole makes it there; a write, through a &mut, an
rt.Place, or a closure's capture too, writes all three.

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

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Byte slices in fields perf(ts): hold a struct's private byte slice field as its parts Nov 1, 2026
@kixelated
kixelated marked this pull request as ready for review November 1, 2026 10:09
@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: d25339ef-1a82-4ad4-a1e2-c0e3ee7ba63f
  • 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 10:10
@moq-bot
moq-bot Bot merged commit 2f2ca66 into main Nov 1, 2026
@moq-bot
moq-bot Bot deleted the quest/m2/ts-byte-views/fields branch November 1, 2026 10:29
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