Repository navigation
perf(ts): MIR's codec as fast as THIR's - #51
Conversation
Quest-Claim: aa9a62927b93aad7
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
# Conflicts: # ts/fixtures/lang/expected/shapes.ts
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. |
kixelated
left a comment
There was a problem hiding this comment.
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.
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.
[u8; N]: MIR lowered[0u8; 8]tonew Uint8Array(8), so each allocated its ownArrayBuffer. It isrt.bytes(8)now, as in THIR.take_u64runs 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).for _ in 0..ncalledrt.Range.next, whose genericltandstephandle 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.DONEprotocol fornextresults 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 returningnumber | rt.DONEwon 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.next14.3 ns, in place withrt.DONE12.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):just ts benchMIR 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_mirandreject_mirpass, along with the rest ofjust test. The lang fixture gains signed ranges across zero and up toi8::MAX, and a range stepped by hand whose items are read after it changed. Those cases run differentially under both lowerings.Impact
--lower mir's output changes: byte arrays come fromrt.bytes, andforover 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
Rangelocal to two scalar variables, as THIR's counterforloop does. A hand-edited bundle measured no faster than the flag, so I left thert.Rangeobject in place.-1for unsigned ranges. That doesn't cover signed ranges and leaks into any reader of the local.Follow-ups
%by a constant leaves its overflow check's operands as statements (4 === -1;), whichtscrejects. 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