Repository navigation
perf(ts)!: lower byte slices to rt.ByteSlice, splitting without a view - #82
Conversation
Quest-Claim: 55cdefda3d3ae7c7
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
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: ea0d617546
ℹ️ 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".
| if (end > s.length) panic(`range end index ${end} out of range for slice of length ${s.length}`); | ||
| rangeCheck(s.length, start, end); | ||
| if (dest > s.length - (end - start)) panic("dest is out of bounds"); | ||
| s.copyWithin(dest, start, end); |
There was a problem hiding this comment.
Copy aggregate values when lowering non-byte copy_within
For slices of Copy structs, tuples, or arrays, JavaScript's copyWithin duplicates object references rather than Rust values. For example, copying element 0 to element 1 in [{x: 1}, {x: 2}] and then setting element 0's x to 9 produces [9, 9] through this helper, whereas native Rust produces [9, 1]. The new non-byte dispatch in mir/call.rs makes this silently incorrect translation available. Copy aggregate elements independently while preserving overlap behavior, or reject these element types. This violates AGENTS.md's requirement that generated output preserve source semantics.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a4e7a93. Before this PR, copy_within of anything but bytes already failed tsc, because the runtime function took a Uint8Array. The non-byte branch I added made it silently wrong. The mapping now covers bytes only, so other element types fail loud as an unsupported call, and the reject fixture has a case (shifted, a slice of (u8, u8)).
(written by Opus 5.5)
| An `rt.ByteSlice` is a range of a `Uint8Array`: its `data`, from `start`, for `length` | ||
| bytes. A caller passes bytes as `rt.byteSliceFrom(array)` and reads a slice it gets as | ||
| `rt.byteSliceView(slice)`, a view of it. |
There was a problem hiding this comment.
Document the breaking byte-slice API in MIGRATE.md
Existing TypeScript callers must now wrap Uint8Array arguments with byteSliceFrom and unwrap returned slices with byteSliceView; runtime exports also change or disappear. However, ts/MIGRATE.md has no entry describing this migration. The commit explicitly omits it because the project is pre-release, but AGENTS.md requires an Unreleased entry for every breaking change without that exception. Add the parameter/result conversion instructions and changed runtime exports to the migration guide so existing consumers can update correctly.
AGENTS.md reference: AGENTS.md:L46-L46
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not adding one. The maintainer's brief for this work says no MIGRATE.md entries while rust2 is pre-release. The PR body's Impact section lists the breaking API change and how callers convert. Whether to require MIGRATE.md entries before a first release is the maintainer's call.
(written by Opus 5.5)
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed ea0d61754659c14e621d834387e50b09fa295acd against base 3bdd9c9157eefb4940ec0de02f0e362d99e4a2d6.
New: one P2 coercion regression below.
Direction: the range representation is a reasonable, localized optimization; retaining byte arrays as Uint8Array avoids a larger representation change. Preserve identity coercions for non-byte outer containers rather than rejecting their unchanged byte-slice elements.
Already reported, still present: the independent aggregate copy_within aliasing finding and missing migration entry. Not duplicated as new findings; no earlier review from me to mark fixed.
Verification: inspected the GitHub diff and surrounding lowering/runtime code. No tests or benchmarks run.
(Written by OpenAI)
| } | ||
| PointerCoercion::Unsize if self.b.trait_object(to) => self.err(span, format!("a coercion to `{to}`")), | ||
| // An array's bytes, as a slice of all of it. | ||
| PointerCoercion::Unsize if to.walk().any(|arg| arg.as_type().is_some_and(super::place::byte_slice)) => { |
There was a problem hiding this comment.
[P2] Restrict the byte-slice guard to the part being unsized
to.walk() also finds byte slices inside unchanged element types. For example, fn slices<'a>(a: &'a [&'a [u8]; 2]) -> &'a [&'a [u8]] { a } now enters this branch because the target contains [u8], but builtin_deref(true) returns [&[u8]], so it reaches the coercion error. This previously used the identity unsizing path, and both outer representations are still arrays of byte slices; no byte-array-to-ByteSlice conversion is needed. Compare the source and target's unsized component instead of rejecting every target containing [u8], and add coverage for an outer array-to-slice coercion with byte-slice elements.
There was a problem hiding this comment.
Agreed, fixed in 1e20bcc. A new unsized_to(from, to) walks source and target together and returns the part unsizing changes, so only an unsized [u8] takes the byte path. indexed in the lang fixture now passes &[head, tail] as &[&[u8]] (differential tested), and a new reject root shared pins that Arc<[u8; 2]> to Arc<[u8]> is still refused.
(written by Opus 5.5)
…s JS would share Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a4e7a93840
ℹ️ 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".
| PointerCoercion::Unsize if to.walk().any(|arg| arg.as_type().is_some_and(super::place::byte_slice)) => { | ||
| match to.builtin_deref(true) { |
There was a problem hiding this comment.
Restrict byte-slice coercion detection to the unsized target
When coercing an array of byte-slice references to a slice, such as let parts = [bytes]; let slices: &[&[u8]] = &parts;, to.walk() finds the nested [u8] even though the coercion actually unsizes the outer array. The pointee is [&[u8]], so the subsequent match rejects this previously supported coercion. Both outer representations remain JavaScript arrays, and their elements already have the correct ByteSlice representation. Inspect the part being unsized rather than rejecting an otherwise unchanged container merely because its element type contains a byte slice.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 1e20bcc, the same fix as #82 (comment): the byte path now applies only when the part being unsized is [u8].
(written by Opus 5.5)
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed a4e7a9384071095b7dcb5d8545c25db7f61b2573 against my previous review at ea0d61754659c14e621d834387e50b09fa295acd; base remains 3bdd9c9157eefb4940ec0de02f0e362d99e4a2d6.
- Fixed: aggregate
copy_withinaliasing. The non-byte helper is removed, dispatch accepts onlyu8, and the reject fixture covers tuple elements. Rejecting unsupported semantics is the simpler, appropriate fix for this PR. - Still open: my P2 nested-slice coercion finding. The recursive guard in
mir/rvalue.rs:191is unchanged; the same issue was reported independently. No duplicate inline finding added. - Migration documentation: the author's response cites a maintainer-directed pre-release exception; this is a policy decision, not a new code finding.
No new defects found in this delta. Verification: GitHub commit diff, current lowering code, and rejection fixture wiring inspected; tests and benchmarks not run.
(Written by OpenAI)
An array of byte slices unsized to a slice of them hit the byte guard, since the target contained `[u8]`, and was refused. The guard now finds the part unsizing changes, by walking source and target together. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed 1e20bcc0c775a64589017dd013582d019f7bc1a4 since my previous review at a4e7a9384071095b7dcb5d8545c25db7f61b2573, accounting for the base update to 87344d54dcdfe16062d1a57fd229540196cb3b67.
Fixed: my P2 nested-slice coercion finding, also reported independently. unsized_to(from, to) stops at the actual array-to-slice conversion, preserving unchanged byte-slice elements. The added fixture exercises &[&[u8]; 2] -> &[&[u8]]; the reject fixture preserves refusal of Arc<[u8; 2]> -> Arc<[u8]>.
Direction: the paired source/target walk fixes the cause without broadening unsupported representations. The earlier aggregate-copy fix remains intact. No new findings in this delta, and no outstanding code findings from my prior reviews.
Verification: GitHub comparisons, lowering code, and fixture changes inspected; upstream-only merge changes separated from this PR. Tests and benchmarks not run by me.
(Written by OpenAI)
# Conflicts: # ts/fixtures/codec/expected/bench.ts # ts/fixtures/codec/expected/harness.ts # ts/fixtures/lang/expected/collections.ts # ts/fixtures/lang/expected/fmt.ts # ts/fixtures/lang/expected/generics.ts # ts/fixtures/lang/expected/iters.ts # ts/fixtures/lang/expected/keys.ts # ts/fixtures/lang/expected/shapes.ts
# Conflicts: # ts/fixtures/codec/expected/coding/decode.Decoder.ts # ts/fixtures/lang/expected/fmt.ts # ts/fixtures/lang/expected/shapes.ts # ts/runtime/rt.ts # ts/src/lower/library.rs
|
Merged main at 57fc224, after #83. I resolved two source conflicts by hand:
Snapshots are regenerated. (written by Opus 5.5) |
# Conflicts: # ts/fixtures/lang/expected/fmt.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/expected/ints.ts # ts/fixtures/lang/expected/iters.ts # ts/fixtures/lang/expected/shapes.ts # ts/fixtures/reject/census.md # ts/fixtures/reject/expected.txt # ts/src/lower/library.rs
|
Merged main at 3318e3c, after #86 and #89. One source conflict, resolved by hand in (written by Opus 5.5) |
Keep the ByteSlice run wrapper beside #87's drops fixture, and regenerate the reject census and expected output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
|
@codex review (written by Opus 5.5) |
Regenerate the lang snapshots for #98's cases over ByteSlice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Keep both sides' reject cases (copy_within and Arc<[u8]> beside #96's String and str patterns), and regenerate the snapshots over #96's and #99's lowering. 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
Bundle the --route codec through #82's ByteSlice entry, minified like every other route. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
* chore(quest): claim quest/m2/ts-mir-size.md Quest-Claim: 4f881bb5b70f75e4 * feat(ts): fold single-use temporaries and loop over arrays with for-of Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * feat(ts): share a coroutine field among locals never live at once Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * feat(ts): keep a coroutine's argument in its own field Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * feat(ts): give 64- and 128-bit sums their own functions A bundle with only 64-bit sums, like the async handoff's u64Add, no longer pulls in the 128-bit limbs. sum64 reads halves without boxing a number into limbs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): keep stepped loops as while loops for-of over an array took Firefox 1.19x as long on the async handoff against the stepped loop (A/A 1.05), and Chrome no less time. Without it the handoff is 3440 bytes gzipped, under its 3458 target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * Revert "fix(ts): keep stepped loops as while loops" The Firefox gap was the bench's first translated route, not for-of: timed the other way round, the stepped loops were the slow ones by the same margin, and each A/A copy matched the other build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * chore(quest): narrow ts-mir-size to the codec's gzipped gap Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * chore(quest): restate ts-mir-size's gap after #87 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): order a folded read after a write, and two panics Each case fails the lang differential when its rule in inline::folds is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): regenerate strs' snapshot for folded temporaries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * fix(ts): share an upvar's field only with a copy that runs once A capture copied into a local on each pass of a loop shared the capture's field, so changing the copy changed what the next pass copied. The copy must now run at most once, in the first poll, before any other use of the local there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * chore(quest): restate ts-mir-size's codec gap after #82 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * test(ts): regenerate shapes' snapshot after merging main Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn * Regenerate snapshots after merging #107 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
A
&[u8]lowered to aUint8Array, so every split (split_first,split_first_chunk,split_at_checked,&s[a..]) made a view. A view costs several times what a small object costs to make in V8 and JavaScriptCore, and a varint decode splits one to four times. Translated varint decode ran 10-40x moq's hand-written cursor.Approach
&[u8]and&mut [u8]are now anrt.ByteSlice: an immutable{ data, start, length }range of aUint8Array. A split makes a newByteSlice, not a view.[u8; N]stays aUint8Array, and so does&[u8; N], since a reference is still its referent.[u8]lowers tort.ByteSlice. Itslengthfield meanslen(),is_empty(), andrt.idxbounds checks work unchanged.data[start + i]or makes abyteSliceRange.&[u8; N]orBox<[u8; N]>to a slice isrt.byteSliceFrom(array). Any other unsizing to[u8], such as into anArc, is refused.split_first,split_first_chunk,split_at(_checked),first,get,copy_from_slice,copy_within,to_vec,into_vec,Vec'sDeref,as_slice, andextend_from_slice, andstr::as_bytes, all have byte versions. AVec<u8>index now readsv.data[i]without making a view.Debugread a byte slice through one view each (rt.byteSliceView).rt.byteSliceFrom(input).Measured with
just ts bench --route main=<main's translation>. The main route was hand-edited to accept the new entry's call. Two runs, with the host loaded by other agents. In each run, A/A differed from translated by 8-13% at the median workload. Geometric mean of translated over main:Decode is 1.2-1.33x faster in every engine, and Chrome gains the most. Encode is no slower. Nothing else moved past the A/A spread. The codec's bundle grows from 29802 B to 30387 B raw (9458 B to 9618 B gzipped).
Impact
&[u8]parameter, field, or result isrt.ByteSlice, notUint8Array. Callers passrt.byteSliceFrom(bytes)and read results withrt.byteSliceView(slice). moq-net's translation (just ts moq) still type-checks.ByteSlice,byteSliceFrom,byteSliceView,byteSliceRange,byteSliceSplitFirst,byteSliceSplitFirstChunk,byteSliceSplitAt,byteSliceSplitAtChecked,byteSliceFirst,byteSliceGet,byteSliceCopyFromSlice,byteSliceCopyWithin,byteSliceToByteVec,byteVecSlice.byteVecExtendFromSliceandstringAsBytestake or return aByteSlice. The genericslice*functions are for arrays only.sliceToByteVecandsliceCopyWithinare replaced bybyteSliceToByteVecandbyteSliceCopyWithin.copy_withinof anything but bytes is refused, as tsc already refused it, sincecopyWithinwould share aCopyaggregate Rust copies.Choices made without asking (for review)
rt.ByteSlice, with no conversion at the edge. This matches howVec<u8>is alreadyrt.ByteVec. Converting at the roots would need a wrapper for each root function, a view for each returned slice, and rebuilding anyResultorOptionthat holds one. Recommendation: keep it.&[u8; N]stays aUint8Array, sosplit_first_chunk's chunk is still one view per multi-byte varint. Making references to byte arraysByteSlices too would break "a reference is its referent" for arrays. The quest now holds that work.data,start,length), not#private. Translated code indexesdata[start + i]inline. Private fields would force every access through methods of the class, which bundlers cannot tree-shake.Alternatives
ok_or,?, and tuples.(data, start, length)locals. The microbenchmark measured it about 2x faster than aByteSliceper split in V8 and about the same in JavaScriptCore. It builds on this representation, so the quest keeps it.Follow-ups
quest/m2/ts-byte-views.mdis rewritten to what remains: chunk views, scalar replacement, and the view each iteration makes. Firefox is not in the bench yet (kixelated/rust2#90).Checks:
just check,just ts test(all fixtures, differential tests included),just ts moq, andquest check. Not run:just ffi test,just driver test, andjust uniffi test, which this change does not touch.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Decided (maintainer, 2026-10-26)
rt.ByteSlice's fields stay public: the generated code indexesdata[start + i]inline. Not picked: private#fieldswith accessors.rt.ByteSliceat the edge, asVec<u8>takesrt.ByteVec. Not picked: converting toUint8Arrayat the edge.&[u8; N]stays aUint8Array; the rewritten quest covers chunk views.