Repository navigation
perf(ts): hold a struct's private byte slice field as its parts - #379
Merged
Merged
Conversation
Quest-Claim: 99771d7d1f6c5da1
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
marked this pull request as ready for review
November 1, 2026 10:09
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
kixelated
enabled auto-merge (squash)
November 1, 2026 10:10
This was referenced Nov 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Finishes quest/m2/ts-byte-views/fields.md.
Problem
Decoder::bufis anrt.ByteSlice, so every read that advances the cursor makes a new one:self.buf = new rt.ByteSlice(...)per varint and perslice. #160 passes and returns slices as their parts, but the field still makes an object at every update.Approach
pub)&[u8]or&[u8; N]field is three JS fields,bufData,bufStart, andbufLen(StructField::Slice). This covers a crate's own structs, classes included, whose constructors take the three in its place.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 asDebug,Clone,PartialEq, or a std call, makes it there.slice()'s holds them as variables. Parts read from fields are not steady, so they are read into variables at once.assign_to). This covers a direct store,mem::take,replace, andswap, an alias of&mut self.buf, and a closure's capture by&mut. Anrt.Placeof the field, which a&mutpassed on or a raw pointer makes, gets a setter over the three. A part that reads a field written before it is read first.bytes()'sinspect_errdoes.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
&mutworks rather than keeping the field whole. The quest planned to leave a field whole crate-wide on any&mutother thanmem::take. Writing all three fields through every write path,rt.Placeincluded, made that pass unnecessary, so nothing keeps a field whole.#private. A type's functions are module functions, not class methods, so they can't reach#fields, andObject.assign, which writes a whole object through a&mut, doesn't copy them.Data,Start, andLen.Measurements
nixos2, an Intel Core i7-8700 (12 threads), quiet.desktopwas loaded at 29.just ts bench --route main=<main's translations>, three runs.mainat826d56d7against this branch at4ffb5a9b. The two translations differ only incoding/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:iter/,time/,order/, andbtree/sit within their A/A in every engine.btree/was 1.04, 1.11, and 0.94, with that family's A/A at 6%, 4%, and 13%.time/ordwas 1.06, 1.09, and 0.89. Node'stime/checked-addwas 1.15, then 1.03 and 0.96.Impact
&[u8]or&[u8; N]field isfooData,fooStart, andfooLenin its interface or class, and a class's constructor takes the three in its place. Apubfield is still anrt.ByteSlice. Recorded ints/MIGRATE.md.Consumer: moq-net's
Decoder.just ts moqtranslates and type-checks it withbufas its parts.Alternatives
&mutbutmem::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.bufon.Decoder::bufwhole, sincerest()andDebugread it whole. The quest decided fields split anyway.Follow-ups
Decoder::varintstill returns anrt.Okper read, andframe_headeranrt.Okof a pair, since onlyResults holding a slice return in a slot.Clonecopies 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
quest review --base origin/main.24aa64ff), one finding:bb9b3cb3:parts_ofregroups the three. A differential case reproduced it first.bb9b3cb3): no findings.Checks
just cipasses locally onbb9b3cb3.fields::fieldscase: reads and splits,mem::take,replace, andswap,&mutpassed on and kept, a closure writing the field back,Copy,Clone,PartialEq, andDebug, a constant's and a static's, a generic struct, a class, apubfield, and an immutable struct literal.just ts moqpasses.🤖 Generated with Claude Code