Skip to content

A generator body's temporaries are released before the yield - #619

Merged
ASDAlexander77 merged 1 commit into
mainfrom
generator-temporaries-release
Oct 19, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
generator-temporaries-release

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

OwnedReturnConsumptionPass skipped whole generators when it released discarded owned temporaries (spec 18.5), so under -mm=rc and -mm=own every temporary in a generator body leaked. yield ("a" + i).length, iterated 500000 times, grew by about 165 MB. The same code in a plain function stays flat.

Fix

A temporary in a generator is now released right before the first ts.YieldReturnVal after it in its block. That release runs before the state machine's cut, so it stays dominated by the definition. It applies only when:

  • no ts.StateLabel is nested in a statement between the temporary and that yield,
  • every user of the temporary comes before the yield,
  • neither the temporary nor a view of it is the value of a ts.Store. The yielded {value, done} record's field store takes no count, so yield "s" + i must keep its +1.

Every other case still leaks, as before.

Results

  • Repro peak (measure.ps1): rc 165.3 → 4.4 MB, own 165.3 → 4.4 MB (gc 5.9 MB).
  • New tests/00generator_temporaries.ts (default jit/compile, rc/none corpus, own jit/compile) and the IR check test-rc-generator-temporaries. The IR check fails on the old compiler, and it also runs --verify-ownership under own.
  • Full ctest -C Release -j 16: 4757/4757 pass, including own_generator*.

🤖 Generated with Claude Code

OwnedReturnConsumptionPass skipped whole generators when releasing discarded owned
temporaries, so under rc and own `yield ("a" + i).length` leaked the number's string
and the concatenation on every call (500000 runs: 165 MB; now 4.4 MB).

A temporary in a generator is now released right before the first yield after it in
its block, when no resume point is nested between it and that yield, every user comes
before it, and neither it nor a view of it is stored (the yielded record's field store
takes no count, so `yield "a" + i` stays as it was). Everything else still leaks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit c09e3c2 into main Oct 19, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the generator-temporaries-release branch October 19, 2026 21:38
ASDAlexander77 added a commit that referenced this pull request Oct 19, 2026
…ven back (#620)

* A generator's yielded value and its temporaries before a yield are given back

Two leaks left after #619:

- A temporary made in front of a statement with a yield inside it (`const n =
  ("b" + i).length; if (n > 0) { yield n; }`) was never released, under rc and own:
  165 MB over 500000 runs, now 4.4 MB. The release now goes in front of the first
  yield, resume point or statement holding one, where the definition still dominates.

- Under rc a yielded value (`yield "s" + i`, `yield new C(i)`) kept the reference it
  was born with: 50 MB over 500000 runs, now 4.4 MB. A yield now retains its
  `{ value, done }` record the way a return does, a generator's `next` is classified
  as returning owned, and its caller takes the record over: `next` is resolved through
  the state object's field that only ever holds it (own_facts::FieldFunctions, moved
  out of OwnershipSignaturePass). own already moved the value into the record.

A temporary used after a yield cannot occur: MLIRGen keeps a generator's locals in
its state object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Generator temporaries test: the caller's takeover is checked through the field, not the closed world

A declared C function in the file keeps a call through a plain value from being
taken as owned by the whole-module rule, so the main check fails when the field
resolution is turned off (checked). Comments and the own spec's found-not-fixed
list updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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