Skip to content

perf(ts): MIR's codec as fast as THIR's - #51

Merged
kixelated merged 5 commits into
mainfrom
quest/m2/ts-mir-codec
Oct 25, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m2/ts-mir-codec

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the MIR's codec as fast as THIR's quest.

Problem

After #36, the codec lowered from MIR ran 1.10x THIR's time in Node, 1.02x in Bun and 1.12x in Chromium, which blocks making MIR the only lowering (ts-mir). Group encode was 1.9-2.6x slower everywhere, frames encode 1.3-1.4x in Chromium, and 1-byte varint encodes up to 1.6x in Node.

Approach

First, separating the review fixes from what was always there: neither gap came from #36's review fixes. A build of the spike before them (aecbe8f) emits the same two patterns below. The earlier, closer run measured MIR against a THIR that didn't yet have #24's pooled byte arrays, which reached THIR's lowering but never MIR's.

  • Pooled [u8; N]: MIR lowered [0u8; 8] to new Uint8Array(8), so each allocated its own ArrayBuffer. It is rt.bytes(8) now, as in THIR. take_u64 runs twice per group encode and six times per frames call, so this was the whole group-encode and Chromium frames-encode gap (group encode in a Node microbench: 1470 to 532 ns, THIR 540).
  • Ranges stepped in place: for _ in 0..n called rt.Range.next, whose generic lt and step handle wide integers too. feat(ts): MIR lowering spike, which moves the translator to MIR #36's in-place step never fired, because the item-or-rt.DONE protocol for next results took the call first. In that path, a range of small ints now steps in place. The item lands in a number-only local, and a boolean flag records whether there was one. Stepping in place but still returning number | rt.DONE won back only half of it, since V8 is about 10% slower on a local that is sometimes a symbol (1-byte QUIC encode in a Node microbench: rt.Range.next 14.3 ns, in place with rt.DONE 12.6, with a flag 11.3, THIR 11.3).

The flag only applies to a next result that isn't ?'d and isn't the return value, where the protocol value has to stay whole.

Results

just ts bench --route mir=<MIR's translation> and A/B runs with THIR in the main slot, on a Ryzen 9 5900XT shared with CI and other agents. Codec geometric mean, MIR over THIR, with an A/A route control (THIR against a copy of itself as the --route):

run 1-min load Node Bun Chromium
before (main) 3.3-5.9 1.032 1.000 1.094
after, just ts bench 5.4-9.0 0.996 0.966 1.006
after, A/B 9.0-7.5 0.987 0.965 0.993
after, A/B 10.2-13.8 1.008 0.955 1.005
after, A/B (before merging main) 9.5-8.3 0.982 0.961 0.999
after, A/B (before merging main) 14.3-13.0 1.014 0.932 1.007
A/A route control 7.5-10.2 0.968 0.995 0.999
A/A route control 8.3-14.3 1.015 1.015 1.012

MIR now ties THIR within the controls' ±3% in Node and Chromium, and is 3-7% faster in Bun. Group encode is 0.94-1.14x (was 1.45-2.48x). One workload stays noisy: Node's 1-byte QUIC encode came out at 1.03, 1.33 and 1.35 across three runs. The same workload ties THIR at 11.1-11.2 ns in a Node microbench, including after warming every workload through the same bundle, and in a headless Chromium microbench, where every encode workload is within 2% of THIR.

Async handoff: unchanged at 0.48-0.54x THIR in Node, 0.31-0.33x in Bun and 0.47-0.49x in Chromium.

Bundle size, Bun-minified: the codec from MIR is 41,305 bytes raw and 12,381 gzipped (was 41,287 and 12,365, so +18 and +16 bytes). The task fixture is unchanged.

Correctness: codec_mir, lang_mir, task_mir and reject_mir pass, along with the rest of just test. The lang fixture gains signed ranges across zero and up to i8::MAX, and a range stepped by hand whose items are read after it changed. Those cases run differentially under both lowerings.

Impact

  • No public API change. Only --lower mir's output changes: byte arrays come from rt.bytes, and for over a range of numbers steps in place. The THIR lowering's output is unchanged; its lang snapshot changes only for the new fixture cases.

Alternatives

  • Lower a Range local to two scalar variables, as THIR's counter for loop does. A hand-edited bundle measured no faster than the flag, so I left the rt.Range object in place.
  • A sentinel number in place of the flag, such as -1 for unsigned ranges. That doesn't cover signed ranges and leaks into any reader of the local.

Follow-ups

  • Found in passing, MIR only: a signed % by a constant leaves its overflow check's operands as statements (4 === -1;), which tsc rejects. Added to ts-mir's plan, which also now records that ranges stepped in place stay.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 4 commits October 25, 2026 08:25
Quest-Claim: aa9a62927b93aad7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
# Conflicts:
#	ts/fixtures/lang/expected/shapes.ts
@kixelated kixelated changed the title MIR's codec as fast as THIR's perf(ts): MIR's codec as fast as THIR's Oct 25, 2026
@kixelated
kixelated marked this pull request as ready for review October 25, 2026 17:23
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T20:06:47.875197Z 8dae1f5 New commits
🔒 Security Review ✅ Completed 2026-10-25T20:10:00.713093Z 8dae1f5 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.

@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: 55299ea

No actionable bugs found. Reusing the established byte pool and limiting in-place range stepping to small-integer next results with constrained uses is a focused approach. Keeping the item and presence flag separate avoids a broader scalarization rewrite; the added signed-boundary and delayed-item-read cases target important semantic risks.

Verification: static inspection of MIR next-result analysis, call/discriminant lowering, control-flow emission, byte-allocation behavior, and changed fixtures. No tests or benchmarks run; reported performance improvements were not independently verified.

@kixelated
kixelated merged commit 6e3b28b into main Oct 25, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-mir-codec branch October 27, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant