Skip to content

feat(ts): place a loop's exit after it - #85

Merged
kixelated merged 4 commits into
mainfrom
quest/m2/ts-mir-size
Oct 26, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m2/ts-mir-size

Conversation

@kixelated

@kixelated kixelated commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

ts-mir-size continued after #79: the codec was 1.3% over THIR's gzip and the handoff 38% minified and 8.6% gzipped. MIR's structurer placed a loop's exit inside the loop, nesting the rest of the function in the branch that leaves (while (true) { if (!(c)) { ...rest...; return; } ... }), where THIR had for/while loops with the rest after them.

Approach

  • Exits after the loop. In cfg::Shape, a node that the loop's test leaves to (the header, and the nodes one way in and one way on after it, up to its first branch) is placed after the loop and reached by break. Other ways out, often a return or ? from within, stay inline, where they are shorter.
  • A final loop tidy pass (cfg::tidy_loops), after the passes that match labeled jumps: a loop takes the label of a block that holds only it, while (true) { if (c) break; .. } becomes while (!c) { .. } (and if (c) { a } else break; becomes while (c) { a .. }), and a jump to the innermost loop it is in drops its label.

ordered now reads while ((_15 = iterStart++) < iterEnd) { .. } let sum = 0; .. rather than four nested loops.

The quest stays, rewritten with what is left and why.

Impact

Output shape only. Behavior is unchanged: just ts test compares every fixture against native Rust, and just ts moq still type-checks.

Size

bundle THIR main this PR vs main vs THIR
codec, minified 30629 30000 29852 -0.5% -2.5%
codec, gzip 9422 9541 9503 -0.4% +0.9%
handoff, minified 10949 15119 15053 -0.4% +37%
handoff, gzip 3458 3756 3712 -1.2% +7.3%

Placing every exit after its loop, not only the test's, left the codec 23 bytes larger minified than this: an early return turns into a break plus the return after the loop.

Speed

Two just ts bench runs with main as --route, alternating with the A/A control: the geometric mean of this PR ÷ main, and A/A ÷ main for the noise. The host was loaded by other agents (load average 17-33 on 32 threads), so Node's and Bun's handoff are noise.

runtime codec codec A/A handoff handoff A/A
Chrome 155, run 1 1.025 0.991 1.010 0.982
Chrome 155, run 2 1.012 1.003 0.965 1.010
Bun 1.4, run 1 0.974 0.983 1.117 1.114
Bun 1.4, run 2 0.958 0.989 1.238 1.188
Node 24, run 1 0.990 0.990 1.046 1.051
Node 24, run 2 1.018 1.021 1.065 0.966

Chrome's codec gap in run 1 came from btree/16 and btree/256 (1.25, A/A 1.01) and the time/u64 pair (1.24-1.29, A/A 0.81-0.90); in run 2 those were 0.82/1.63 and 0.84/0.86. btree is ordered, the function this reshapes most, so it was timed alone: 8 alternating Chrome processes per minified bundle, this PR ÷ main at 16/256/4096 keys is 0.64/0.82/0.87 against A/A 0.95/0.96/0.77; Node 1.00/0.90/1.01 (A/A 1.05/0.96/1.02), Bun 0.98/0.98/0.89 (A/A 1.03/1.02/0.93). No loss shows above the noise.

Alternatives

  • A runtime base class for the coroutine scaffolding (feat(ts): smaller MIR output #79's suggestion): prototyped by hand on the handoff's 17 one-await classes, a base owning the resume-after-finish checks and the poisoning catch, each class keeping its own resume so its calls stay monomorphic. 792 bytes smaller minified, 38 larger gzipped, so not timed. A base can't read a subclass's private #state, so only those two pieces can move.
  • Eager poisoning (write #state = 2 on entry, so a cleanup that only poisons needs no catch arm): about size-neutral on a one-await class, since the write lands in each resumed arm.
  • Collapsing an async fn that only awaits another async fn of its arguments: exact, and it would erase the 16 levelN classes, but it removes the depth the handoff measures and moq-net's wrappers await kio::wait, which it can't collapse.

Follow-ups

The quest stays, rewritten: the handoff's minified gap is the fixture's 16 levelN classes (about 360 bytes each against 135 for THIR's generator), while its gzip gap (284 bytes with one level each) is outside them, in drainFuture's dispatch, recvFuture, and the stepped for loops of register and wake.

just ts bench times unminified bundles, while the quest asks for minified timings; making the bench time what it measures for size could be its own quest.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated changed the title Smaller output from MIR feat(ts): place a loop's exit after it Oct 26, 2026
@kixelated
kixelated marked this pull request as ready for review October 26, 2026 20:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T20:32:30.324711Z 696eae4 Draft marked ready
🔒 Security Review ✅ Completed 2026-10-26T20:36:38.367065Z 696eae4 Draft marked ready
ℹ️ 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 696eae4d209f3b21b1c0847ccba6e8e6a0823427 against base 87344d54dcdfe16062d1a57fd229540196cb3b67.

No actionable bugs found. Checked natural-loop membership and exit placement, nested/multiple exits, exit nodes that also head loops, label adoption/removal, switch-sensitive breaks, and the changed generated fixtures.

Direction: Keeping normal test exits after the loop while leaving other exits inline is a focused improvement. The final AST cleanup preserves the existing structurer and avoids adding coroutine/runtime machinery for the remaining size gap. The reported alternatives and measurements justify that scope; I found no clearly simpler equivalent that needs to block this change.

Verification: GitHub-only static review; I did not run tests, type-checks, or benchmarks or independently reproduce the size/timing claims. The head's Check workflow was still running at review time.

(Written by OpenAI)

@kixelated
kixelated merged commit be5cc64 into main Oct 26, 2026
1 check passed
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