Skip to content

perf(ts): hold byte slice locals as their parts - #130

Merged
kixelated merged 11 commits into
mainfrom
quest/m2/ts-byte-views/locals
Oct 28, 2026
Merged

kixelated merged 11 commits into
mainfrom
quest/m2/ts-byte-views/locals

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A &[u8] is an rt.ByteSlice, so every split_first, split_first_chunk, split_at(_checked), the tuple and Option around it, ok_or's Result, and ?'s ControlFlow make 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:

  • Parts: a slice's data, start, and length; a flag for which variant an enum is; and every other leaf whole. An error payload stays one value.
  • Qualifying locals: nothing borrows or writes into them, their type doesn't drop, and every def is one of std's splits, Option::ok_or, Try::branch, an aggregate, or a copy or subslice of another held local.
  • Reads: a read that takes the local apart reads its parts: a field, a byte, a length, a discriminant, a bounds check, or a subslice. Any other read makes the object there.
  • When a local stays an object:
    • its object would be made inside a loop that doesn't make its parts again;
    • its object would be made twice on one path from the same origins;
    • every def only hands it one existing object's parts, which would save nothing.
      This is a fixpoint over the candidates.
  • Representation: a single-def local holds pure expressions in place, since its def dominates its reads and shared borrows keep the bytes fixed. A multi-def local gets let variables, assigned in parallel when a new value reads the old.
  • Producers inline:
    • split_at: inlined with its "mid > len" panic.
    • ok_or: builds a pure-aggregate error only on the Err path. An effectful error still runs first. It qualifies only when its error is held as parts or has no enum in it; otherwise the Result stays an object.
    • ? on an Option: just negates the flag.

Supporting changes:

  • new rt.ByteSlice(d, s, n) folds in slice_member, byte reads, subslices, len/is_empty, and bounds checks. A bounds check tests the length part rather than making a slice for rt.idx.
  • new rt.ByteSlice(...) counts as pure when moving temporaries past it.
  • A shared byte slice local's fields count as frozen, since ByteSlice is readonly.
  • Locals saved across .await need no rule: bodies lower from the coroutine transform, which makes them state fields.

Impact

just ts bench, A/B against main as a --route in 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:

engine varint decode frame decode group decode varint encode frame encode A/A (decode)
Chrome 155 0.49 (0.29-0.82) 0.61 (0.54-0.71) 0.95 1.00 0.97 0.033 / 0.011
Firefox 158 0.57 (0.47-0.84) 0.56 (0.51-0.63) 0.86 1.05 1.02 0.037 / 0.047
Node 24 0.64 (0.45-0.91) 0.70 (0.60-0.80) 0.90 1.01 1.04 0.127 / 0.025
Bun 1.4 0.56 (0.48-0.84) 0.61 (0.56-0.69) 0.89 1.00 0.99 0.034 / 0.027

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 readQuic now reads buf.data[buf.start + i] in place and returns rt.Ok([v, new rt.ByteSlice(buf.data, buf.start + n, buf.length - n)]): the one slice, tuple, and Ok it returns, where main also made each split's tuple and slices. Decoder.slice no longer calls rt.byteSliceSplitAtChecked, which the codec's tree-shake test now drops.

A new splits module in the lang fixture (harness case 49) covers each producer and its error path, ? on Option and ok_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 effectful ok_or error.

Public API: the translator's output for byte slices; no Rust API changes.

Review

Both automated findings on ddfffb5 were fixed in fix(ts): test only a held enum's own discriminant by its flag, and each was reproduced in the splits fixture first:

  • P1: a discriminant of an error enum held whole read the error as the flag, so Err(Short::Empty) took the Need arm. scalar_enum now requires the projection to reach an Enum node.
  • P2: ok_or(e) with e: Option<u8> panicked in take_apart. It now stays an object.

Alternatives

  • Split parameters too, as the spike's variant did. This changes every signature and caller, so it's left to the returns and fields quests.
  • Drop the error part of a Drop type on Err, as the quest allowed. Excluding those locals was simpler, and no codec local needs it.

Follow-ups

  • Pre-existing miscompile, not caused by this PR: (a, b) = (b, a) on two &[u8] in a loop translates to a = b; b = a; on main too. place_expr reads a once-made tuple's reference field as its operand in place (the var_parts arm in place.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

kixelated and others added 3 commits October 27, 2026 13:32
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
@kixelated kixelated changed the title Byte slices in locals perf(ts): hold byte slice locals as their parts Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 22:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T23:54:05.429653Z bcdf0a4 New commits
🔒 Security Review ✅ Completed 2026-10-27T23:56:35.446262Z bcdf0a4 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread ts/src/lower/mir/scalar.rs Outdated
/// 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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 an e: Option<u8> parameter, let (a, b) = s.split_at_checked(1).ok_or(e)?; in a function returning Result<usize, Option<u8>> qualifies, but the error has Shape::Enum and no slice, so it cannot be a held scalar. scalar_operand falls through to take_apart's unreachable! 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)

kixelated and others added 2 commits October 27, 2026 15:11
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 kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)
Reviewed commit: 9dda38a

Compared with my previous review, separating the merged main changes:

  • Fixed: the P2 ok_or enum-error translation panic. scalar.rs:410–422 now rejects errors that cannot be decomposed; the generated second() fixture preserves the ordinary Result path.
  • Fixed: the independent P1 projected error-discriminant finding. scalar_enum checks the projected shape and guards both discriminant paths; the generated inline Short match 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)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread ts/fixtures/lang/src/harness.rs Outdated
Comment on lines 80 to 81
47 => splits::splits(kind, data, &mut out),
_ => durations::durations(kind, data, &mut out),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

kixelated and others added 2 commits October 27, 2026 16:07
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 kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

Follow-up from the PR body still open: the pre-existing (a, b) = (b, a) slice-swap miscompile in place_expr, to plan through /quest-plan.

(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
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged main again for #129, by hand: both PRs added a lang fixture case. durations stays at opcode 47 as on main, splits moves to 48, combinators is the catch-all, and the dispatch is op % 50. That puts caller.ts back on main's opcode 47, so e1c4df3's change folds away. Snapshots were regenerated: the codec's varint.ts is unchanged, and the unheld ok_or and map_or calls now use #129's core combinators. just ts test passes.

(Written by Claude Opus 5.5)

kixelated and others added 2 commits October 27, 2026 16:32
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
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged main for #128, by hand:

Regenerated snapshots: only harness.ts changed. just ts test and just ts check pass.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit ecb655b into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-byte-views/locals branch October 28, 2026 00:04
kixelated added a commit that referenced this pull request Oct 28, 2026
#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>
kixelated added a commit that referenced this pull request Oct 28, 2026
#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>
kixelated added a commit that referenced this pull request Oct 28, 2026
)

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kixelated added a commit that referenced this pull request Oct 28, 2026
#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>
kixelated added a commit that referenced this pull request Oct 28, 2026
* 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>
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