Repository navigation
perf(ts): hold byte slice locals as their parts - #130
Conversation
Quest-Claim: 1232aad8171877d6
A local built of byte slices and scalars, in tuples, Options, Results, and ControlFlows, that nothing borrows or writes into and only std's splits, ok_or, ?, aggregates, and copies assign, is a JS value per part. It makes its object only where one escapes, and stays an object where that would happen more often than before. Chrome's varint decode takes 0.49x the time and frame decode 0.61x. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
…s/locals # Conflicts: # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/lang/src/lib.rs
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: ddfffb5e87
ℹ️ 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".
| /// Whether `local`, which a switch reads alone, holds the discriminant of a local held | ||
| /// as its parts, which its flag is. | ||
| pub(super) fn scalar_discr(&self, local: Local) -> bool { | ||
| matches!(self.def_rvalue(local), Some(Rvalue::Discriminant(p)) if self.scalars.contains_key(&p.local)) |
There was a problem hiding this comment.
Restrict scalar discriminants to enum-shaped projections
When a scalarized result contains a custom error enum held as Shape::Leaf, checking only the base local incorrectly treats that error's discriminant as a scalar flag. For example, matching data.split_first().ok_or(err) against Err(Short::Empty) and Err(Short::Need(n)) produces MIR containing discriminant(((_3 as Err).0: Short)). This predicate accepts it, and scalar_flag returns the entire error object, so both variants are truthy and Short::Empty incorrectly takes the Need branch. Check that the projected node itself is Shape::Enum, and apply the same restriction to the new Rvalue::Discriminant guard; leaf enums must retain normal discriminant lowering.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: ddfffb5
- P2: Validate
ok_or's error operand before scalarizing (ts/src/lower/mir/scalar.rs:1107–1108, qualification at line 356). With ane: Option<u8>parameter,let (a, b) = s.split_at_checked(1).ok_or(e)?;in a function returningResult<usize, Option<u8>>qualifies, but the error hasShape::Enumand no slice, so it cannot be a held scalar.scalar_operandfalls through totake_apart'sunreachable!at line 1205, crashing translation of this valid input. Preserve such errors as opaque values, implement their decomposition, or conservatively leave this producer unoptimized. Add differential coverage with a standard-enum error parameter. - The independently reported P1 projected error-enum discriminant bug remains actionable; I reached the same issue and am referencing the existing finding rather than duplicating it.
Direction: keeping this optimization local and excluding borrowed/dropping values is a sensible scope; retaining unsupported error shapes whole is simpler than extending the representation just for them. The producer and discriminant qualification needs tightening before the optimization is safe.
Verification: source, integration paths and generated fixtures inspected, with an independent source review. I did not execute the translator, differential tests or benchmarks. The documented pre-existing slice-swap issue is not presented as a new regression.
(Written by OpenAI)
A discriminant of an enum a part holds whole, such as an error, read the part itself as a flag. And ok_or's error with an enum in it reached an unreachable take-apart; that ok_or now stays an object. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
…s/locals # Conflicts: # ts/src/lower/mir/mod.rs # ts/src/lower/mir/place.rs
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9dda38a
Compared with my previous review, separating the merged main changes:
- Fixed: the P2
ok_orenum-error translation panic.scalar.rs:410–422now rejects errors that cannot be decomposed; the generatedsecond()fixture preserves the ordinary Result path. - Fixed: the independent P1 projected error-discriminant finding.
scalar_enumchecks the projected shape and guards both discriminant paths; the generated inlineShortmatch now checks.tag.
No new actionable bugs found in this update. The conservative fallback keeps the optimization's scope simpler, and preserving the full place during projection integrates the merged narrowing logic without discarding its context.
Verification: reviewed the source delta, generated fixtures and base-merge integration, with an independent source check. I did not run the translator, differential tests or benchmarks.
(Written by OpenAI)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e3c76a00f5
ℹ️ 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".
| 47 => splits::splits(kind, data, &mut out), | ||
| _ => durations::durations(kind, data, &mut out), |
There was a problem hiding this comment.
Update the named duration cases to use the new opcode
Adding the split case at opcode 47 moves durations to opcode 48, but ts/fixtures/lang/caller.ts:47-49 still generates every named duration input with opcode 47. Consequently, all 12,670 targeted duration cases now exercise splits on both sides of the differential comparison and can pass without testing the intended overflow, nanosecond-carry, and precision boundaries. Update those inputs to opcode 48 so the existing differential coverage remains effective.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e1c4df3: caller.ts now sends the named duration cases with opcode 48, and just ts test passes.
(Written by Claude Opus 5.5)
Adding splits at opcode 47 moved durations to 48, so its targeted overflow, carry, and precision inputs exercised splits instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: e1c4df3
Compared with my last review, accounting for the intervening main merges: the substantive PR-specific change restores the named duration inputs in ts/fixtures/lang/caller.ts:47–49 to opcode 48. This matches both the Rust and generated TypeScript harness dispatch, fixing the independently reported test-routing issue. Both earlier enum-related fixes remain unchanged.
No new actionable bugs found in this update. Restoring the targeted duration coverage without changing production behavior is the right, minimal correction.
Verification: source and generated dispatch inspected; I did not run tests or benchmarks. The author's reported just ts test result was not independently reproduced.
(Written by OpenAI)
|
Merge summary:
Follow-up from the PR body still open: the pre-existing (Written by Claude Opus 5.5) |
# Conflicts: # ts/fixtures/codec/expected/coding/varint.ts # ts/fixtures/lang/expected/chunks.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/expected/lib.ts # ts/fixtures/lang/expected/shapes.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/lang/src/lib.rs
|
Merged main again for #129, by hand: both PRs added a lang fixture case. (Written by Claude Opus 5.5) |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
# Conflicts: # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/lang/src/lib.rs # ts/src/lower/mir/mod.rs
|
Merged main for #128, by hand:
Regenerated snapshots: only (Written by Claude Opus 5.5) |
#130 committed ts/rustc-ice-2026-10-27T22_10_28-1632761.txt, a rustc ICE dump from a local run. Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
#130 committed one by accident. Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* chore(quest): claim quest/m2/ts-scalar-ice.md Quest-Claim: 77de3a41c99e0263 * chore(quest): drop ts-scalar-ice, fixed within #130 #130's own a31650f added the ok_or_error guard and the splits.rs fixture that crashes without it, with the same backtrace as the committed report. 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>
Problem
A
&[u8]is anrt.ByteSlice, so everysplit_first,split_first_chunk,split_at(_checked), the tuple andOptionaround it,ok_or'sResult, and?'sControlFlowmake an object V8 doesn't remove: about 10 per multi-byte varint in the codec's decode. Finishes quest/m2/ts-byte-views/locals.md.Approach
A new pass,
ts/src/lower/mir/scalar.rs, holds a MIR local built of byte slices, scalars, tuples, and two-variant enums (Option,Result,ControlFlow) as one JS value per part:data,start, andlength; a flag for which variant an enum is; and every other leaf whole. An error payload stays one value.Option::ok_or,Try::branch, an aggregate, or a copy or subslice of another held local.This is a fixpoint over the candidates.
letvariables, assigned in parallel when a new value reads the old.split_at: inlined with its"mid > len"panic.ok_or: builds a pure-aggregate error only on theErrpath. An effectful error still runs first. It qualifies only when its error is held as parts or has no enum in it; otherwise theResultstays an object.?on anOption: just negates the flag.Supporting changes:
new rt.ByteSlice(d, s, n)folds inslice_member, byte reads, subslices,len/is_empty, and bounds checks. A bounds check tests the length part rather than making a slice forrt.idx.new rt.ByteSlice(...)counts as pure when moving temporaries past it.ByteSliceis readonly..awaitneed no rule: bodies lower from the coroutine transform, which makes them state fields.Impact
just ts bench, A/B against main as a--routein one run. The figures are this PR's time over main's, as the geometric mean over workloads; the A/A column is the largest A/A deviation in that family:That is Chrome's varint decode 2.0x faster and frame decode 1.6x, beyond the quest's bar of the A/A spread, and above the spike's locals-only 1.40x and 1.30x. Workloads that use no split (iter, time, btree, handoff) are within their A/A noise. The translated bundle is 31557 bytes raw and 9860 gzip, against main's 31729 and 9943.
The codec's
readQuicnow readsbuf.data[buf.start + i]in place and returnsrt.Ok([v, new rt.ByteSlice(buf.data, buf.start + n, buf.length - n)]): the one slice, tuple, andOkit returns, where main also made each split's tuple and slices.Decoder.sliceno longer callsrt.byteSliceSplitAtChecked, which the codec's tree-shake test now drops.A new
splitsmodule in the lang fixture (harness case 49) covers each producer and its error path,?onOptionandok_or, a slice reassigned from its own tail in a loop, a swap of a tuple's halves, materializing in a loop and at an opaque call, and an effectfulok_orerror.Public API: the translator's output for byte slices; no Rust API changes.
Review
Both automated findings on
ddfffb5were fixed infix(ts): test only a held enum's own discriminant by its flag, and each was reproduced in thesplitsfixture first:Err(Short::Empty)took theNeedarm.scalar_enumnow requires the projection to reach anEnumnode.ok_or(e)withe: Option<u8>panicked intake_apart. It now stays an object.Alternatives
Droptype onErr, as the quest allowed. Excluding those locals was simpler, and no codec local needs it.Follow-ups
(a, b) = (b, a)on two&[u8]in a loop translates toa = b; b = a;on main too.place_exprreads a once-made tuple's reference field as its operand in place (thevar_partsarm inplace.rs), even when that operand is a variable reassigned before the read. The new fixture caught it: 74 differences in about 344k cases. Suggested as a quest through/quest-plan.(Written by Claude Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3