Repository navigation
perf(ffi): returned objects closed early skip the collector in Go and Dart - #838
Conversation
Quest-Claim: 1d8c38851c77762d
…a in a Dart close Profile Go's and Dart's object return: most of it is each runtime's per-object registration with its collector. Allocate a Go object with its handle, whose cleanup then frees the raw handle directly, and drop the `_Arena` a Dart checked close made for nothing. Rewrite the quest to the nursery that would skip the registration, pending a maintainer decision, and unblock ready-await. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Quest-Claim: 95ae5bfab8f203b1
…/ffi-speed/object-ownership
…gistration Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ctor's registration Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es in one crossing Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rk; finish the quest Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…efore Rust drops it; a Go flush forgets its slots Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This review covered the full PR: a 64-slot nursery of returned objects in the Go and Dart runtimes, the one-crossing checked close, and the tests and docs for both.
- Go nursery (
ffi/runtime/rt.go):Handle_.Ownpushes each returned handle into a per-Pnursery_reached throughsync.Pool. A CAS onraw[i]decides betweenCloseand eviction, andevicting_makes a racingleave()wait untilAddCleanuphas writtenh.cleanup. I traced the slot protocol. A slot is rewritten only bypushafter it evicts the old occupant, and a live handle'srawcan't be reused, so I found no ABA.heldandnextare touched only bypush(while the ring is checked out) or byflush(once the ring is unreachable), so those two never overlap. - Go checked
Closefast path:CAS(held_, closed_|closing_), thenleave, then a single C shim (GoClose_) that drops and frees. On failure,own()re-registers the handle andheld_is restored. Re-entrant calls fromDropseeErrClosedand don't deadlock. Every close and move path (uncheckedClose, the fast path,abandon) now goes throughleave()before anything frees the handle. - Dart nursery (
ffi/runtime/rt.dart,ffi/runtime/dart.rs):_Nurserylives in the isolate's_n. Rust keeps a copy of each slot's handle and its free function, and frees whatever is still in a slot once the nursery is finalized. A pushed-out object takes its finalizable handle only at eviction, so no handle is ever both in a slot and registered. - Dart checked objects now count holds:
_closedis set before Rust drops the value. A close with_holds == 1drops and frees in one crossing, anddisownruns after the free whilethiskeeps the object reachable. Otherwise the last_releasefrees. A failed close reopens the object and re-registers it if it had been taken out of the nursery. - Codegen (
ffi/src/go.rs,ffi/src/dart.rs): addsown_<c>→Own(), thedrop_<c>(p, free)shim,<prefix>_dart_nursery, per-object_close_<bare>, and_freeing_<bare>bound to<c>_free. - Tests:
- Go
nursed(): push-out, close after eviction, and an 8×1000 race of returns, closes, hand-offs and drops. - New nightly
api_go_raceunder-race. - Dart: push-out, isolate exit, and the
Journalre-entrant close. - Dart
ReadingList: an object closed while its call is still lowering arguments.
- Go
claude-opus-5-5 | 𝕏
…ject-ownership # Conflicts: # ffi/src/go.rs
cgo built by clang, as the sanitize shell's is, reads a uint64_t macro whose top bit is set as a negative constant, so the runtime's init panicked that the Go and the library are from different builds whenever the contract's top bit was set. A static C function returns the macro instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 262472fcc12904d836cf3b521edaa765bb3e8e2b.
No actionable new defect found in this focused static review.
Traced the Go nursery's slot CAS, eviction/Close synchronization, ring cleanup and flush, the failed checked-close re-registration path, and generated close/free shims. Traced Dart's nursery slots and finalizer ownership, checked-object call holds, closed-before-drop ordering, failed-close recovery, and deferred freeing while argument lowering still holds a handle. The new late-argument-close and reentrant-drop regressions address important lifetime boundaries; the Go race harness covers concurrent return, eviction, close and handoff.
The delayed collection of objects left in the nursery is explicitly documented rather than silently preserving the former per-object collection timing. No additional issue found in the corresponding generator changes.
Limits: GitHub-only source and test inspection; no builds, race detector, Dart isolate execution, sanitizer runs, or benchmark reruns performed. This is not an independent verification of the reported performance results or a proof of race freedom. COMMENT only, not approval.
…ject-ownership # Conflicts: # quest/m1/ffi-speed/ready-await.md
…ject-ownership # Conflicts: # ffi/fixtures/api/src/lib.rs # quest/m2/wasm/backend.md
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Since the last Pullfrog review at 262472fc, the branch merged main through c27427f and added one fix to how Go reads the header's contract.
- Resolved #837's conflict in
ffi/src/go.rs: the head keeps the one-crossingclose_cshim (<prefix>GoClose_<bare>) over main'sself.call-baseddrop_<c>. The shim's status is a C local and it takes only a C handle pointer and anint, so #837's pointer rule still holds.checked_chas no remaining callers. - Read the header's contract through C (
b7d50fd2): the Go runtime preamble now hasstatic uint64_t <go>_header_contract(void), andinitcompares its result instead ofC.<UPPER>_CONTRACT. This avoids clang's cgo reading a top-bit-setuint64_tmacro as a negative constant. The Swift and C++ checks still compare the macro directly, which is correct in those compilers. - Resolved #847's fixture conflict:
Journalsits ahead of thecfg(not(target_arch = "wasm32"))LogThreadand stays ungated. The PR reports thatapi_webpasses with it.
claude-opus-5-5 | 𝕏
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 8127c4bc74725036391bbf3caa4cd31408229cff, incrementally from 262472fcc12904d836cf3b521edaa765bb3e8e2b.
No new actionable issue found in the Go contract-check change and relevant merge integration.
The header contract now crosses through a C function returning uint64_t, rather than cgo's macro-constant representation. Both comparisons remain present: the generated Go contract is checked against the linked library and the included header. This preserves mismatch detection while avoiding signed interpretation of a top-bit-set macro.
The merged Go generator retains the one-crossing checked-close shim with its status stored in C, not a Go-local out parameter; freeing still occurs only when the drop status is zero and freeing was requested. The Go nursery, Dart hold-count/closed-before-drop logic, and Rust Dart-nursery changes are unchanged from the previously reviewed PR patches apart from context positions. The fixture retains Journal separately from the native-only LogThread gate.
Limits: GitHub-only focused inspection. The large comparison includes merged main work, which this is not a fresh audit of. No clang/cgo reproduction, generation, builds, race detector, Dart/wasm tests, or benchmark reruns performed; reported validation is not independently confirmed. COMMENT only, not approval.
Its other children landed in #837, #838, and #849, so deleting shared-borrow.md would leave the epic with no children. Per the maintainer's decision, delete its README, rewrite the links to it, and replace bench/ffi's link to the deleted go-pointers.md with #837. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Problem
A Go or Dart object return cost ten times C's (Go 263 ns, Dart 242, C 25 on the binding benchmark), and most of it was each runtime's per-object registration with its collector: Go's
runtime.AddCleanupand theCleanup.StopitsClosemakes, Dart'sDart_NewFinalizableHandleand its finalization. Every returned frame, group, and future pays it.Finishes quest/m1/ffi-speed/object-ownership.md, as settled in #828.
Approach
A nursery of the last 64 returned objects. A close of an object still in it frees the handle at once and never registers; one pushed out still open registers then, as before. Explicit
Closestays, and the registration stays the backstop, so ownership stays automatic.ffi/runtime/rt.go): a nursery per P, found through async.Poolaslending_finds byte batches. Each handle points at its nursery and slot. A compare-and-swap on the slot decides between itsCloseand its eviction; an eviction claims the slot with a sentinel before it registers, so aClosethat lost waits until the cleanup it must stop is written. The pool's wrapper around each nursery carries oneAddCleanupthat registers each still-open occupant on its own once the pool drops the nursery, so a dropped object never waits on its nursery-mates being collected. A checked object'sClosethat no call holds claims the handle closed, then drops the value and frees the handle in one crossing (a C shim in the cgo preamble).ffi/runtime/rt.dart,ffi/runtime/dart.rs): one nursery in the isolate's_n. Rust keeps a copy of each slot's handle and free function, and one finalizable handle on the nursery frees whatever is still in it once it is unreachable, as at isolate exit. A checked object'sclose()that no call holds now drops and frees in one crossing. Its calls check a closed flag in Dart and count their holds, as a counted one's do. The flag is set before Rust drops the value, so Dart that the drop runs sees the object closed, and a close that fails under a borrow reopens it and re-registers the object it took out of the nursery.Validation
just cipasses.just moqpasses (moq's own API through every language).Journalfixture (a checked type holding aBox<dyn Log>): a DartLog.closerun by the journal's drop sees the journal closed and closing it again closes nothing more. An object closed by a Dart implementation while a call is lowering its arguments is still read by that call and freed after it.-race. It is nowapi_go_raceinjust ffi sanitize(nightly). The sanitize shell has Go.c27427f02, which brings #837 (go-pointers), #839 (ready-await), #844 (not-Send), and #847 (the web target).ffi/src/go.rs. The head keeps perf(ffi): Go's calls pass C no pointer to a Go local #837'scall_sigand this branch's one-crossing close shim. That shim's status is a C local, which perf(ffi): Go's calls pass C no pointer to a Go local #837's rule allows.ready-await.md, which this branch had edited only to drop a link to this quest. The head takes the deletion._freeor_dropthat runs on another thread to the value's owner thread, inside the glue. So the nursery's frees from Go's GC goroutine and at Dart's isolate exit stay correct for a value that is notSend. The api tests cover this withLocal.quest/m2/wasm/backend.md. The head keepsJournalbeside feat(ffi): the web target, TypeScript over a wasm32-wasip1 build #847'scfg(not(target_arch = "wasm32"))onLogThread. It takes feat(ffi): the web target, TypeScript over a wasm32-wasip1 build #847's rewrite ofbackend.md, since this branch had only reworded a line in the block feat(ffi): the web target, TypeScript over a wasm32-wasip1 build #847 replaced.api_webpasses withJournalin the fixture.api_goandapi_go_racein the sanitize shell,just ffi moq, andjust ciall pass.CC=clang, cgo reads#define <P>_CONTRACT ((uint64_t)0x…ULL)as a negative constant whenever its top bit is set. The runtime'sinitthen panicked with "the Go of api and the library are from different builds". The merged head's contract,0xf2005c…, has that bit set, soapi_go_racefailed, andapi_gofailed in that shell too. gcc reads the constant correctly. The runtime's preamble now returns the macro from a static C function,<p>Go_header_contract(). The bug was latent on main; a contract without the top bit set hides it.track.Info()from a Go local into the shim's result struct. 8 rounds of the Go and Dart programs alternate with main at80ad1c352(perf(ffi): Go's calls pass C no pointer to a Go local #837 included), on nixos (Ryzen 9 5900XT) at a load average of 12 to 17. Median wall ns:Dart's throughput rows rose 19 to 28%, in MB/s. Go's plain 100 B write read +15% in this batch and -14% in a second batch of 8 Go-only rounds, so it is noise; that second batch also held the object return at -38%. The first measurement, against
72880f48, before #837, had Go at -31% and Dart at -57%. #839 and #844 leave the object return's path as measured: aSendhandle's thread check is one branch onNone.Impact
Closeis freed later. In Dart that happens once 64 more objects were returned on its isolate, or at isolate exit. In Go it happens once 64 more were returned on its P, or once an idle P's nursery is dropped by the GC (two collections). Previously this happened at the next collection.StateError('<Type> is closed')from Dart, not Rust's "moved out" check.Closewith no other call holding the handle may seeErrClosedeven if thatClosethen fails, as calls racing main's uncheckedClosealready may.rt.Handle_loses its exportedCleanupfield and gainsOwn(), and itsdroptakes afreeflag. These are internal to the generated packages. Dart's glue gains<prefix>_dart_nurseryand per-object_close_(checked) entry points.Journal.Decisions
The Go nursery's cleanup registers its occupants rather than freeing them. The Plan had the ring's one
AddCleanupfree its open slots once all its objects were unreachable. That would let one long-lived object keep up to 63 dropped ones (Go and Rust memory) alive for its whole life once the pool retires the nursery. Instead, the cleanup arg holds the nursery's handles, and the handles point at the nursery, not the pool's wrapper. So the wrapper's cleanup runs when the pool drops it, and registers each still-open object on its own. Rejected: the Plan's ring-wide free.Go's checked
Closedrops and frees in one crossing. It does this when no call holds the handle, which the Plan's "a checked value's drop in the same crossing" covers. Calls during that crossing fail as closed rather than wait, because aDropthat calls back into Go and uses the same object would otherwise wait on itself. Rejected: waiting calls (deadlock on re-entry), and keeping two crossings (about 30 ns more a close).The eviction handshake. An eviction claims the slot with a sentinel, then registers, then clears the slot, and a
Closethat meets the sentinel yields until it clears. This lets a failed one-crossing close re-register the object without racing a stale eviction. Rejected: registering before the compare-and-swap (the first draft), which can write the handle's cleanup after a failed close re-registered it.The Dart api test now moves an object out to reach Rust's check.
closedObjects's "Rust's check drops the implementation" case used a closed checked object, which Dart now refuses before Rust takes the implementation. It now uses an object moved out by a call, which only Rust sees, and asserts that a call Dart refuses gives Rust nothing.A Dart checked object counts its call holds. Review found that a checked object closed by Dart code a call runs while lowering (an implementation's method) would free the handle the call was about to use. Its calls now count holds as a counted object's do, an integer increment and decrement in Dart, and
close()frees in the same crossing only when it is the sole holder; otherwise it drops and the last release frees. Rejected: refusing a close while held (a behavior change from main, where the close succeeds) and keeping one load a call (unsound).Go reads the header's contract through a C function. The merge with main surfaced cgo-under-clang misreading a
uint64_tmacro whose top bit is set (see Validation). Rejected: comparinguint64(C.<P>_CONTRACT), which clang's cgo refuses to compile as an overflowing constant, and dropping the header check, which would let a header from another build mix in unnoticed.Alternatives
As the Plan records: keeping per-object registration (rejected by the maintainer), flushing the nursery each event-loop turn (a timer per turn), Dart's
NativeFinalizer.attachwithoutdetach, and Go'sruntime.SetFinalizer.Follow-ups
None.
Models
Size: L
Implementer: Claude Code, claude-opus-5-5, high effort
Implementer: Claude Code, claude-opus-5-5 (iterate, S)
Review
Rewritten by
quest reviewafter each round.Head: 8127c4b
Base: origin/main at c27427f
Risk: high,
The branch changes native-handle ownership in downstream Dart and Go bindings, where lifetime defects can cause crashes or memory corruption that reverting cannot undo.Classified: medium by default
Reviewer: Codex, gpt-6.1-sol, xhigh effort
Session:
01a1e0f6-a7de-7f20-9257-21f3ba871dfc, round 4Score: 10/10,
Static review found no regressions in the merge integration, and the previously reviewed fixes remain intact; builds and tests were not run.Raised the risk to high:
The branch changes native-handle ownership in downstream Dart and Go bindings, where lifetime defects can cause crashes or memory corruption that reverting cannot undo.Rounds: 6; first round 0; findings 4 (high 3, medium 1): fixed 4, refuted 0, open 0
Findings: none
Reviewer: Codex, gpt-6.1-sol, medium effort, shadow
Session:
01a1dea9-4f07-7fb0-a930-8b724c4bf5f6Rounds: 1; first round 0; findings 0: fixed 0, refuted 0, open 0
Summary by CodeRabbit
Summary by CodeRabbit
Performance
Bug Fixes
Documentation