Skip to content

feat(ts): smaller output from MIR - #95

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

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

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Claims quest/m2/ts-mir-size.md.

Problem

The MIR lowering's bundles are larger than THIR's. On main (80f4487), the async handoff is 3720 bytes gzipped against a 3458 target. The codec is 31347 minified and 10063 gzipped, against 30629 and 9422.

Approach

Four cuts. Each one keeps Chrome's speed.

  • Fold single-use temporaries (inline.rs): a const read once moves into the next statement's first evaluated expression. It only moves when nothing between the two could observe the move, meaning no write, panic, or memory read on either side.
  • Turn stepped array loops into for-of (step::for_of): this matches register and wake's while (true) { const x = i < s.length ? s[i++] : DONE; if (x === DONE) break; ... }.
  • Coroutine fields:
    • Saved locals that are never live together and share a TS type share one field (storage_conflicts).
    • A saved local can keep its value in an upvar's field when it is copied from that upvar under three conditions. The copy must be the upvar's only use. It must run at most once, in the first poll. Nothing the first poll runs before the copy may use the local.
    • this.#x = this.#x is dropped.
  • Runtime: rt/int.ts gets sum64 and sum128 in place of one sum that took the width. The handoff's only wide op is u64Add. Its overflow path used to pull in the 128-bit limbs, which a bundler can't shake out of a shared function.

Sizes, minified / gzipped:

codec handoff
main (80f4487) 31347 / 10063 14995 / 3720
this PR 30935 / 9898 14341 / 3423
goal (THIR) 30629 / 9422 - / 3458

On the main before #87, the cuts measured one at a time were as follows (handoff gzipped):

  • Folding temporaries and for-of: 3668 to 3576.
  • Shared fields: 3549.
  • The upvar field and self-assignments: 3531.
  • sum64 / sum128: 3363. The codec also went from 9733 to 9651.

Only the handoff's goal is met. The codec misses both of its goals.

The codec's gap came from PRs that landed after THIR's numbers were taken, not from the lowering:

The quest now covers only the codec's remainder.

Timing

quest gate runs of just ts bench --route main=<main's translation>. Each cell is the geomean of PR/main, with A/A/main and the A/A spread (median and max over workloads) in brackets.

Against main at 80f4487, the head (host load average 10-13):

engine codec run 1 codec run 2 handoff run 1 handoff run 2
Chrome 155 1.020 (1.010; 6.6% / 29%) 1.010 (1.006; 5.2% / 26%) 1.011 (1.113; 9.5% / 26%) 1.007 (1.077; 17.3% / 25%)
Firefox 158 0.967 (0.984; 5.5% / 22%) 0.987 (1.005; 4.3% / 26%) 1.125 (0.953; 12.5% / 25%) 1.176 (1.022; 9.3% / 23%)
Bun 1.4.2 1.004 (0.997; 4.1% / 32%) 0.986 (0.989; 4.1% / 20%) 1.251 (1.137; 6.4% / 15%) 1.060 (0.989; 6.1% / 9%)
Node 24 1.012 (1.005; 2.8% / 16%) 1.005 (0.992; 1.9% / 11%) 1.023 (0.993; 3.0% / 11%) 0.989 (0.961; 4.6% / 10%)

Against main at #91, the head before #82 (quieter, load 8-10):

engine codec run 1 codec run 2 handoff run 1 handoff run 2
Chrome 155 1.012 (1.013; 3.2% / 11%) 1.011 (0.991; 3.1% / 14%) 0.971 (0.977; 4.3% / 4%) 1.028 (1.060; 3.1% / 11%)
Firefox 158 1.011 (1.032; 4.0% / 32%) 0.958 (1.004; 6.2% / 38%) 1.080 (0.982; 10.2% / 16%) 1.162 (1.013; 10.4% / 20%)

Chrome shows no loss beyond the A/A spread. The codec sits at 1.01-1.02, with the A/A median spread at 3-7%. Two earlier pairs, on the main before #87, gave Chrome codec 1.009-1.014 and handoff 0.94-0.96.

Firefox's handoff 1.08-1.18 is the bench, not this PR. In every Firefox run, the translated route is 10-17% slower than its own A/A copy, an identical build. To check for-of, I timed the PR with and without it in both directions:

  • Head against a no-for-of build: 1.191 (A/A 1.053).
  • No-for-of head against the for-of build: 1.168 (A/A 1.033).

Whichever build is translated looks slow by the same margin, and each A/A copy matches the other build.

Impact

  • Public API: the translator's output changes shape but not behavior. rt/int.ts changes only private helpers, and its exports are the same.
  • Tests:
    • Snapshots are regenerated.
    • lang's indexed gains two cases. In the first, a value is read before a call that changes it and used after that call. In the second, two operands can each panic. Removing the write rule or the panic rule from inline::folds fails the lang differential.
    • task's blocks gains a capture copied into a local on each pass of a loop. Without the run-once checks, 351 cases differ from native Rust.
    • Breaking sum64's carry fails codec and lang.
    • just check and just test pass.

Alternatives

  • Dropping for-of (77 bytes of the handoff, gzipped): measured as above, it costs no speed, so it stays.
  • Hand-measured cuts under 1% gzipped: hoisting the dispatch entry case, dropping dead unwind stores, and merging identical cleanup arms. They are listed in the quest and left out.

Follow-ups

  • The codec's remaining gap comes from runtime and drop-glue growth since THIR's numbers. The quest now targets it, and restating the goal against today's runtime is the alternative.
  • just ts bench times the first translated route 10-17% slower than its A/A copy in Firefox's handoff. This hides real changes of that size. It is suggested for /quest-plan, not filed.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 9 commits October 26, 2026 17:42
Quest-Claim: 4f881bb5b70f75e4
A bundle with only 64-bit sums, like the async handoff's u64Add, no
longer pulls in the 128-bit limbs. sum64 reads halves without boxing a
number into limbs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
for-of over an array took Firefox 1.19x as long on the async handoff
against the stepped loop (A/A 1.05), and Chrome no less time. Without
it the handoff is 3440 bytes gzipped, under its 3458 target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
The Firefox gap was the bench's first translated route, not for-of:
timed the other way round, the stepped loops were the slow ones by the
same margin, and each A/A copy matched the other build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Smaller output from MIR feat(ts): smaller output from MIR Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 02:33
@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-27T06:09:46.055109Z a427c3b New commits
🔒 Security Review ✅ Completed 2026-10-27T06:13:55.624285Z a427c3b 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: ee97a90

No actionable bugs found in the temporary-folding evaluation-order guards, narrowly matched array-loop rewrite, coroutine field sharing, or width-specific sum helpers. Direction looks sound: using rustc's storage-conflict information and splitting the runtime by width are simpler than broader liveness analysis or bundler-dependent specialization. Keeping the remaining codec-size gap explicit is appropriate.

The new optimizer rules would benefit from focused negative regression cases (lazy reads, mutation/panic ordering, and overlapping saved locals), beyond regenerated snapshots. Verification: source and generated-snapshot inspection only; I did not run the translator, differential tests, or benchmarks, and have not independently verified the reported size/speed results.

kixelated and others added 4 commits October 26, 2026 19:42
…ff2db0c3036

# Conflicts:
#	ts/fixtures/lang/expected/shapes.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Each case fails the lang differential when its rule in inline::folds is
removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@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: 829e4d2

No actionable bugs found in the substantive update since my previous review (accounting for the main merge). The new shapes.rs regression cases address the earlier mutation/panic-ordering coverage suggestion: the snapshot retains the pre-mutation read and evaluates division before the potentially overflowing addition. Direction is good—focused differential fixtures add coverage without expanding optimizer complexity; the remaining size gap is now documented against the newer baseline.

Verification: inspected the added Rust cases, generated TypeScript, harness reachability and relevant folding guards. I did not run tests or benchmarks, and have not independently verified the revised size measurements.

@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: 829e4d2c47

ℹ️ 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/coroutine.rs Outdated
Comment on lines +359 to +361
&& reads.reads.get(&upvar) == Some(&1)
{
moved.insert(layout.variant_fields[variant][field], upvar.index());

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 Keep repeatedly copied captures separate from saved locals

For an async move block that copies a capture inside a loop, one syntactic read does not mean the capture is read only once during execution. For example, async move { let mut total = 0; for _ in 0..2 { let mut x = n; x += 1; std::future::ready(()).await; total += x; } total } produces a saved-local assignment from the captured n that satisfies this predicate. Aliasing x with the capture makes x += 1 overwrite n, so the next iteration starts from the modified value: with n = 5, the result becomes 13 instead of native Rust's 12. Exclude these repeated copies unless their safety is proven, and add a differential case covering the loop.

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.

Agreed, and reproduced. A looped case in blocks copies a capture inside a while loop with an await, and it differed from native Rust in 351 task cases.

Fixed in 44f089d. The copy now keeps the upvar's field only if both of these hold:

  • It runs at most once. It is not reachable from a later poll's resume target, and not from itself.
  • Nothing the first poll runs before the copy, or instead of it, uses the local.

Removing both run-once checks brings the 351 differences back. The handoff's argument fields still qualify, so the sizes are unchanged.

(written by Opus 5.5)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Re the review, I broke each guard on purpose to see whether a test catches it:

  • Mutation and panic ordering: 829e4d2 adds both cases to lang's indexed. In the first, a value is read before a call that changes it and used after that call. In the second, two operands can each panic.
    • Removing the write rule from inline::folds makes 708 lang cases differ from native Rust.
    • Removing the panic rule makes 28 differ ("attempt to divide by zero" against "attempt to add with overflow").
  • Lazy reads: the existing fixtures already cover these. Folding into a lazy read fails task, codec, and lang.
  • Overlapping saved locals: sharing a field regardless of rustc's storage_conflicts makes 1894 task cases differ from native Rust.

(written by Opus 5.5)

kixelated and others added 2 commits October 26, 2026 20:27
A capture copied into a local on each pass of a loop shared the
capture's field, so changing the copy changed what the next pass
copied. The copy must now run at most once, in the first poll, before
any other use of the local there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…ff2db0c3036

# Conflicts:
#	ts/fixtures/codec/expected/coding/version.Versions.ts
#	ts/fixtures/lang/expected/keys.ts
#	ts/fixtures/lang/expected/shapes.ts

@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: e873acd

The independent P1 capture-aliasing finding, missed in my earlier review, is addressed in the inspected code: field reuse now excludes copies reachable from later polls or a cycle, and requires the copy to precede other saved-local uses. The added loop regression keeps base and x separate in generated TypeScript.

No new actionable bugs found in this update after accounting for upstream merges. Direction is sound: conservative reachability checks are justified here and avoid a broader alias analysis. Verification was source/CFG reasoning and snapshot inspection only; I did not run tests or independently verify the reported mutation-test results.

kixelated and others added 4 commits October 26, 2026 20:37
…ff2db0c3036

# Conflicts:
#	ts/fixtures/codec/expected/bench.ts
#	ts/fixtures/codec/expected/coding/decode.Decoder.ts
#	ts/fixtures/codec/expected/coding/encode.Encoder.ts
#	ts/fixtures/codec/expected/coding/varint.ts
#	ts/fixtures/codec/expected/harness.ts
#	ts/fixtures/lang/expected/chars.ts
#	ts/fixtures/lang/expected/collections.ts
#	ts/fixtures/lang/expected/fmt.ts
#	ts/fixtures/lang/expected/ints.ts
#	ts/fixtures/lang/expected/iters.ts
#	ts/fixtures/lang/expected/keys.ts
#	ts/fixtures/lang/expected/shapes.ts
#	ts/fixtures/lang/src/shapes.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated
kixelated merged commit e0e9c1d into main Oct 27, 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