Skip to content

perf(ts): share a Copy aggregate nothing in the crate writes into - #104

Merged
kixelated merged 13 commits into
mainfrom
quest/m2/ts-shared-read
Oct 27, 2026
Merged

kixelated merged 13 commits into
mainfrom
quest/m2/ts-shared-read

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#83 and #99 copy a Copy aggregate wherever Rust copies it and JS would share it. A copy is only observable if something writes into the shared object in place, so most of these copies are dead weight: Decoder::varint copies self.form on every read, which made varint/leading-ones decode 2-19% slower in Chromium.

Approach

copy::Writes reads every function, closure, and coroutine body in the crate once (optimized MIR, over its own type parameters) and records each type something writes into in place:

  • a projection write (p.x = 1 writes into p's type, a[i] = v into the array's),
  • a write through a reference or a box (*r = v, which lowers to Object.assign), or SetDiscriminant,
  • a mutable reference or pointer that leaves code the analysis reads: what a parameter takes of a function the crate names that doesn't resolve to its own (std, or a trait method only an instance resolves), whether called there or taken as a value that a pointer or a type parameter calls; a value stored anywhere but a whole local; a Yield; or the source of an unsizing coercion to a trait object. A &mut that stays in locals, is returned, or goes to a crate function is followed there, so match self { Group { group, .. } => &*group } on &mut self writes nothing.

An array is keyed as a slice of its element, since the two share storage, and matched against writes over type parameters both as itself and as that slice. A write over type parameters, such as *x = X::default() in fn reset<X: Default>(xs: &mut [X]), counts for every type that unifies with it and satisfies the body's where-clauses (trait solver, fresh inference variables); a projection on them is any type that satisfies its associated type's item bounds.

needs_copy now asks changes: a tuple, array, or local struct or enum the crate writes into, or any aggregate holding such a type by value. Option, Poll, Result, and ControlFlow count only through their payloads: they are the payload or no object, and since #100 a payload write replaces an rt.Result whole.

Results:

  • moq-net (just ts moq, pinned rev): all 6 copies go, and lib.ts with copy$Form: Decoder::new, Decoder::form, Decoder::sub, Decoder::varint, Encoder::new, Encoder::form.
  • codec fixture: copy$Form and copy$Version go (lib.ts is gone), so the shake test no longer keeps copy$Form.
  • lang fixture: copy$Clash, copy$Pair, copy$Pair$2, copy$Rank, copy$u8_str go; types written in place keep theirs.

Bench (quest gate just ts bench --route main=<origin/main's translation>, after merging #95, two runs, shared machine). Translated ÷ main, run 1 / run 2:

engine A/A median geomean, all workloads leading-ones decode 1B / 2B / 4B / 8B
Chromium 2.0% / 2.0% 0.993 / 0.986 1.10 / 0.96 / 0.97 / 0.96, then 0.91 / 0.92 / 0.87 / 0.98
Node 7.0% / 4.1% 1.007 / 0.949 1.03 / 0.99 / 0.98 / 0.96, then 0.95 / 0.90 / 1.00 / 0.98
Bun 3.1% / 3.7% 0.979 / 0.990 0.75 / 1.01 / 0.85 / 0.93, then 0.86 / 0.96 / 0.97 / 0.94
Firefox 4.7% / 8.5% 0.976 / 0.975 0.99 / 0.92 / 0.90 / 0.83, then 0.86 / 0.78 / 1.21 / 0.85

In Chromium, 2B to 8B leading-ones decode is 2-13% faster in both runs; 1B flips between runs, within its ±10% spread. Before #95 a single run gave a Chromium geomean of 0.981 and 0.83 / 0.98 / 0.89 / 0.95. Bundle: 30617 B raw / 9790 B gzip, against main's 30722 / 9819.

Tests: lang's shapes::unwritten copies a Mark whose only write is generics::advance, in another module; a Gauge written only by generics::reset<X: Default> behind a type parameter; and a Level written only through a box. A new share fixture holds a byte array with a single write in the crate, where lang's own byte writes would hide a miss, translated once per kind: *dst = value through dst: &mut T; with feature call, copy_from_slice called directly; with pointer, copy_from_slice through fn invoke<A, B>(f: fn(A, B), ..). Each was caught with the analysis broken for it (16003 of 16273 cases differ): keying the array as a slice before matching it against writes over type parameters, ignoring std functions the crate names, and reading only direct calls' arguments. #83's argument-aliasing and #99's store cases are unchanged and pass. Breaking the analysis on purpose, each caught by the lang differential (916-1480 differences): ignoring writes over type parameters, ignoring projection writes, and ignoring writes through a reference or box.

Checks: just check, just test, just ts moq, just ts bench.

Impact

  • Translator output: a Copy aggregate the crate never writes into in place is shared, not copied, and its copy$ function is no longer emitted. A caller must not change in place what it passes to or gets from the translation (README).
  • No Rust API change.

Alternatives

  • Treat any write over a type parameter as a write into every type. Simplest fail-closed reading, but moq-net's unbounded &mut V and &mut <P as IntoIterator>::IntoIter then keep every copy. Bounds and item bounds keep it sound and drop them.
  • Count every &mut a local holds. Same result for moq-net's Requests::join (Option<&mut V>) and Rank::options: they kept copy$Form and added a Result copy.
  • Analyze only the instances translation reaches, translating twice. Precise and concrete, but doubles translation time and isn't the whole-crate analysis the quest asked for.

Follow-ups

  • The std rule holds while the runtime writes in place only through a std function's own &mut parameter (today copy_from_slice). A std trait impl that writes through a reference its Self is, reached through a type parameter (W: Write as &mut [u8]), would need its own rule; none is mapped.
  • It stays conservative: keys::Hop and Horizon keep their copies because derived Debug hands &&mut Hop to std. Known std functions that never write through their arguments (formatting) could be exempt.
  • Fresh values are still copied where an operand is moved into a slot, as copy$Gauge(Gauge.default$Default()).

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated changed the title Share what the crate never changes in place perf(ts): share a Copy aggregate nothing in the crate writes into Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 06:10
@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-27T17:29:02.033125Z 6f0435d New commits
🔒 Security Review ✅ Completed 2026-10-27T17:31:23.777733Z 6f0435d 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: 61998d5fd9

ℹ️ 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/README.md Outdated
Comment on lines +158 to +159
at. A write over a type parameter counts for every type its bounds allow. A caller must
not change in place what it passes to or gets from the translation either.

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 Add migration guidance for the new caller immutability contract

Existing TypeScript callers that mutate an object after passing it to translated code now observe different behavior. For example, Decoder.new previously copied Form, but now retains the caller's object, so changing its seven field also changes subsequent decoding. The README introduces this new caller restriction, but ts/MIGRATE.md has no corresponding entry. Add an ## Unreleased entry explaining the ownership change and how callers should avoid mutating shared values. AGENTS.md explicitly requires migration entries for breaking changes.

AGENTS.md reference: AGENTS.md:L46-L46

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.

Not adding one: rust2 is pre-release, and the maintainer asked for no MIGRATE.md entries in this phase. The caller contract is documented in the README next to the copy rule, and the PR's Impact section names it.

(written by Opus 5.5)

@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: 61998d5fd925694fed9e68b52fe83d3dd306a313

[P1] Preserve array types when checking generic write bounds (ts/src/lower/copy.rs:260–271). written turns [u8; 2] into [u8] before asking whether a generic write to T can instantiate to it. For #[inline(never)] fn overwrite<T: Copy>(dst: &mut T, value: T) { *dst = value; }, the actual array satisfies Copy and Sized, but the slice used by instantiates does not, so this write is missed. In a crate without another [u8] write, let mut b = a; overwrite(&mut b, [3, 4]); (a, b) for an array argument a can share a and b, while the translated overwrite uses .set, changing both. Keep the original concrete type for generic-bound matching (and distinguish it in the result cache), while separately handling array/slice storage aliases. Add a small isolated differential fixture: the existing lang crate's byte writes would mask this case.

The independent caller-immutability migration finding also remains applicable; no duplicate inline comment.

Direction: whole-crate sharing is a useful targeted optimization, but the storage-equivalence key must not replace Rust type identity when proving trait bounds. A conservative fallback is preferable to eliding a potentially observable copy.

Verification: reviewed the analysis, copy generation, place stores/reconstruction, and changed fixtures. Source analysis only; no tests or benchmarks executed.

(Written by OpenAI)

kixelated and others added 2 commits October 26, 2026 23:22
`Writes::written` keyed the queried array as a slice before matching it
against writes over type parameters, so `*dst = value` with `dst: &mut T`
never matched `[u8; 2]`: a slice is neither `Sized` nor `Copy`. The new
`share` fixture holds that write alone, where lang's own byte writes hid it.

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

Copy link
Copy Markdown
Collaborator Author

Re the automated review's "Preserve array types when checking generic write bounds": agreed, and reproduced. written keyed [u8; 2] as [u8] before matching it against writes over type parameters, so *dst = value with dst: &mut T never matched, since a slice is neither Sized nor Copy. Fixed in 134ed8d: the query is matched both as the array and as its slice, memoized on the array type. The new share fixture holds that write alone, as the review suggested, since lang's byte writes hid it; with the old analysis its differential reports 16003 differences in 16273 cases.

(written by Opus 5.5)

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

Compared with my previous review, separating the merged base's comparison changes from this fix.

  • P1 fixed: generic write matching now preserves the actual array type and caches by it, while separately checking the slice storage form. The isolated share fixture exercises the previously missed T: Copy overwrite; its snapshot retains the copy before .set, and it is wired into snapshot, typecheck, and differential tests.
  • The independent migration-entry finding was answered by the author, citing a pre-release policy exception. The entry remains absent, not a code fix or a new finding.

No new actionable bugs found in this update. Direction: the small correction keeps Rust type identity separate from storage equivalence without adding a second translation pass; the isolated fixture avoids unrelated byte writes masking the regression.

Verification: source and generated-output review only. I did not execute tests or benchmarks; the author's reported reproduction results were not independently rerun.

(Written by OpenAI)

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

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

ℹ️ 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/copy.rs
Comment on lines +388 to +391
ty::Adt(_, args) => stack.extend(args.types()),
ty::Tuple(tys) => stack.extend(tys),
ty::Array(elem, _) | ty::Slice(elem) => stack.push(elem),
_ => {}

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 Account for mutable references hidden behind type parameters

When an indirect call receives a bare type parameter, pointees silently ignores it even though an instance can supply a mutable reference. For example, fn invoke<A, B>(f: fn(A, B), a: A, b: B) { f(a, b) } can invoke invoke::<&mut [u8], &[u8]>(<[u8]>::copy_from_slice, &mut copy, &src) after copying a byte array. The caller's local-function call is excluded from escape analysis, and the generic body's indirect call records neither A nor its eventual [u8] pointee. With no other byte-array writes, needs_copy therefore shares the original array with copy, so the runtime's copy_from_slice changes both, unlike Rust. Track these parameterized escapes conservatively or resolve their pointees at instantiated call sites, and cover this with a differential fixture.

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: both invoke(<[u8]>::copy_from_slice, &mut b, ..) with f: fn(A, B) and the F: FnOnce(A, B) form shared the array. Fixed in 6594df2 at the source the pointer comes from rather than at the generic call: every std function the crate names, called or taken as a value, counts what its parameters take (&mut [u8] here). That replaces the old per-call argument rule, which it covers, so no copies change elsewhere and moq-net still has none. A const fn pointer is refused (no value tree), so a pointer to std always starts at such a mention. share gained call and pointer features, one write each; with the old rule the pointer run differs in 16003 of 16273 cases. The remaining assumption, that the runtime writes in place only through a std function's own &mut parameter and not in a trait impl a type parameter reaches, is in the comment and the PR body.

(written by Opus 5.5)

A std function a pointer or a type parameter calls, as in
`invoke(<[u8]>::copy_from_slice, &mut copy, &src)` with `f: fn(A, B)`,
writes where no call to std shows, so the copy was shared. Every std
function the crate names, called or taken as a value, now counts what
its parameters take, which also covers the direct calls the old rule
read from their arguments. `share` gains `call` and `pointer` features,
one write each, and its test runs all three.

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: 6594df2b73704b9947a221edf079fddc0faab0ca

Compared with my last published review at b8e5ae6e44e47fa22f22168cd5c0b3e39dd17d81, excluding the intervening base merge and equivalent regenerated output.

The independent generic indirect-call finding is addressed for the reported copy_from_slice case: visiting the function item records its mutable slice parameter before generic forwarding hides it. The direct-call and pointer feature variants isolate that write and are both wired into typechecking and native differential tests. The earlier array/type-bound P1 remains fixed.

No new actionable bugs found in this update. Direction: recording external function signatures at their source is simpler than tracing every generic forwarding path, within the documented supported-runtime assumption. That assumption matters when adding std trait implementations with hidden mutation; this is not a general escape-analysis proof. The migration discussion is unchanged and not raised again.

Verification: source review of the visitor replacement, fixture variants, and test invocation plumbing only. I did not execute tests or independently reproduce the reported differential results.

(Written by OpenAI)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated
kixelated merged commit 8dc7b3b into main Oct 27, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-shared-read 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