Skip to content

fix(ts): copy what a container stores - #99

Merged
kixelated merged 7 commits into
mainfrom
quest/m2/ts-store-alias
Oct 27, 2026
Merged

kixelated merged 7 commits into
mainfrom
quest/m2/ts-store-alias

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Storing a by-value Copy aggregate through a std call shared the JS object instead of copying it. After v.push(pair); v.push(pair); v[0].0 ^= 1, v[1] and pair changed too. This was a silent miscompile. The share analysis in ts/src/lower/mir/mod.rs only counted in-place changes made through a local. It missed changes made later through a container element, a box, or a &mut that std returns.

The same gap let a value with no copy function share a Copy aggregate it holds. Holder { pair } (not Copy) pushed into a vector, passed to a function that changes it, or returned to a caller all shared pair's object.

Approach

Everything is in the analysis. No library mapping changed.

  • keeps: a std call keeps a Copy aggregate it takes by value if its result holds it behind a box, reference, or collection, or if another argument can take it in (&mut, interior mutability, a raw pointer). This also covers a part std takes apart, such as each element of v.extend([pair, x]). A value with no copy that holds a Copy aggregate counts as kept by any callee, the crate's own included. A return place of such a type is kept by the caller.
  • What a call keeps is copied with fix(ts): copy Copy aggregates by type #83's per-type copy functions (copy_fn). The exception is a value moved out of a local's own value: that local is marked changed in place (mutated), so its value is already its own, and the move hands it over without a copy.
  • Ownership propagates. A value moved into a local that is changed in place marks its source too, so it is copied once at its source (or not at all, for a fresh literal). This keeps Ok([a, b]) and push((a, b)) copy-free.
  • vec![x; n] no longer copies x in its mapping, because the call site now copies it.
  • The crate's own functions are found with a new Translator::translated, shared with call_def's existing check.

For ts-shared-read: the decision is made in one place, keeps plus moved_from, and it is recorded on mutated. A new kind of store adds a case to keeps. A new way to hand over a value adds a case to moved_from.

Reproduced first: the new lang case gives 1070 differences against native Rust with main's translator and 0 with this one. It covers Vec::push (from a local, a parameter, a call result, a local changed before and after), Vec::extend of an array, mem::replace, VecDeque::push_back/push_front/insert, HashMap::insert, entry().or_insert, BTreeMap::insert, Box::new, a non-Copy holder pushed, passed to a function that changes it, and returned. Vec::insert has no mapping yet (it is refused), so it is not covered.

Impact

  • Translator output: calls that keep a Copy aggregate now copy it, for example v.push(copy$u8_u8(pair)) and rt.hashInsert(m, copy$Pair(key), ...). A value moved into a field, a local changed in place, or a kept call is no longer copied again.
  • moq-net at the pinned revision gains two copies: Decoder::new and Encoder::new now copy their form into the struct they return (copy$Form). The shake test's kept set adds copy$Form for the same reason.
  • No public Rust API change.

Codec bench (quest gate nix develop .#bench --command just ts bench --route main=..., main at 62a8a59), Chrome:

median max
translated ÷ main 1.001 1.076 (leading-ones/1B encode)
A/A spread 1.9% 9.3%

No change beyond the A/A noise. Bundle size: 30534 raw / 9801 gzip, against main's 30526 / 9798. An earlier run showed btree/* 14-30% slower on identical btree code. Both bundles are byte-identical there, and the rerun above matched main (73.3 vs 71.4, 117 vs 117, 273 vs 267), so that gap was noise.

Alternatives

  • Copy in each library mapping (push, hashInsert, ...), as vec![x; n] and into_vec did. This is what the quest ruled out: every new mapping would have to remember to do it.
  • Treat every by-value argument to std as kept. This is simpler, but it copies Into::into, Ord::max, u32::from_be_bytes([..]), and Option::map_or arguments for nothing. holds_behind and stores avoid that.
  • Leave the non-Copy holder hole for a later quest. It is the same flaw in the same analysis, and fixing it costs two copies in moq-net, so it is fixed here.

Follow-ups

Suggested, not recorded as quests (for /quest-plan to decide):

  • Skip copies of Copy types that nothing in the crate changes in place. A whole-crate scan for &mut T, field writes, and SetDiscriminant of T would drop the new copy$Form here, fix(ts): copy Copy aggregates by type #83's self.form copy that ts-shared-read targets, and map-key copies. It may subsume ts-shared-read.
  • Map and set keys are copied like values, though std never hands out a &mut to a key. Only a key whose source changes later needs a copy.
  • A call result stored by std is copied (copy$Rank(rank(a, b))), since a translated callee may return a shared parameter. Knowing which functions return fresh values would drop this copy.
  • Vec::insert has no mapping.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated changed the title Copy what a container stores fix(ts): copy what a container stores Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 02:26
@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-27T03:00:28.100674Z f827fef Manual request
🔒 Security Review ✅ Completed 2026-10-27T03:02:53.271202Z f827fef Manual request
ℹ️ 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: 7e93a72

No actionable bugs found. The analysis-level retention check and ownership propagation address the previously identified container-store aliasing gap, including non-Copy holders and values returned to callers. The generated cases preserve separation for repeated stores, mutable container reads, boxes, and held fields.

Direction looks sound: centralizing this in the MIR analysis is safer than requiring each library mapping to remember a copy. The extra retention/type analysis is justified by avoiding blanket copies at every call; keep future store forms covered by differential regressions before further copy-elision optimizations.

Verification: source and generated-snapshot inspection only. I did not run the translator, native/wasm differential suite, or benchmarks, and have not independently verified the reported performance results.

# Conflicts:
#	ts/fixtures/lang/expected/keys.ts
kixelated added a commit that referenced this pull request Oct 27, 2026
Replaces ts-shared-read's narrow fix, as #99 suggested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Give #96's strs and this branch's stores their own ops (% 44), and regenerate the harness snapshot.

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

@codex review

(written by Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: f827fef930

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: f827fef930

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@kixelated
kixelated merged commit e969db9 into main Oct 27, 2026
1 check passed
kixelated added a commit that referenced this pull request Oct 27, 2026
Keep both sides' reject cases (copy_within and Arc<[u8]> beside #96's String and str patterns), and regenerate the snapshots over #96's and #99's lowering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated added a commit that referenced this pull request Oct 27, 2026
* chore(quest): default comparison methods and generic instances

Follow-ups from #98 and #94.

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

* chore(quest): Cow<str> and str slicing from #96

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

* chore(quest): share Copy types the crate never changes in place

Replaces ts-shared-read's narrow fix, as #99 suggested.

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

* chore(quest): an A/A control that measures noise

From #87's and #97's bench findings.

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

---------

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated deleted the quest/m2/ts-store-alias 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