builtin: add closure lifetime reclamation#27483
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3d974672df
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4c3bdbd8f3
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Ready. |
|
Looks like one of your tests is failing in CI... https://github.com/vlang/v/actions/runs/27787819942/job/82228925824?pr=27483#step:17:2053 |
|
I checked it. This is not a closure lifetime API failure. The failing sub-test is a freestanding/no-std smoke test for Our test reveals it because it forces this specific compile mode: Today V’s Windows was already skipped. In my opinion this is not blocking for merging the PR and there is no need to restart a Codex review for this one. Only a tiny part of the regression test was adjusted to match the current limits of the freestanding path, but of course we can wait for the CI tests to finish. |
Summary
This is a recovery of the closure leak work discussed in #27446.
It fixes #27445 by adding the compiler/runtime foundation for captured closure reclamation. Captured closure contexts now use collectable
memdupinstead ofmemdup_uncollectable, while the closure runtime keeps live contexts reachable through a scanned table.Non-escaping local captured closures are cleaned up when their scope is left, including normal scope exit,
return, and labeled/non-labeledbreak/continue.The cleanup remains conservative. Closures are not destroyed when they escape through return values, copies, function arguments, struct fields, globals, defer blocks,
spawn, orgo.This PR also adds an explicit, reusable closure lifetime/reclaim API that frame-based or immediate-mode systems can use without exposing raw closure destruction.
Public API added:
closure.new_lifetime()Lifetime.frame(work fn ()) !Lifetime.reclaim(retain int) !Lifetime.reclaim_all() !Lifetime.dispose() !Lifetime.suspend(work fn ()) !Lifetime.untracked(work fn ()) !The API lets a caller create a lifetime owner, run one transient frame under that owner, reclaim old frame-owned closure contexts, and explicitly keep persistent callbacks outside reclaim when needed.
Important contract:
frame,suspend, anduntrackedborrow theirworkcallback. The lifetime owns only the closure contexts created while a frame is active.Changes
closure_create_with_data.Follow-up
This PR prepares the ground for vlang/gui#58, but does not claim to fully fix it by itself.
The remaining work should be done in vlang/gui by using the new lifetime API when replacing frame/layout trees in
Window.update().Tests
All tests below were run on the current PR head
3d974672d.