Repository navigation
perf(ffi): one allocation for a Go object and its handle, and no arena in a Dart close - #828
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>
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This review covers the Go handle allocation and cleanup rewrite, the Dart _drop() change, the bench-build rerun fix, and the doc and quest rewrites.
- One allocation per Go object:
own_<c>/lend_<c>allocate the object and itsrt.Handle_together, andown_'sruntime.AddCleanupnow frees the raw handle directly. This is sound:- Every hold keeps
&o.hreachable, through the deferredunhold_(h, o)andarena_.held. That pointer is interior too, so the cleanup cannot run while a call holds the handle. - Every closing path calls
Cleanup.Stop()while the object is still reachable: uncheckedClose, checkedClosethroughMoved, and a move into Rust. So the unconditionalfree_<c>(raw)cannot double-free. lend_registers no cleanup.- A copy of the object (
x := *obj) now also keeps the cleanup from running. Before, the cleanup onobjcould free the handle whilexwas still in use.
- Every hold keeps
Handle_.InitreplacesNewHandle_, andCollectedis gone: nothing else in the repo references either name, andTAKENdropsNewHandle_to match.- Checked Dart
_drop()without_Arena: the arena was never used._n.statusis the isolate's own persistent pointer, and a nested call takes its own stack mark either way. moq-exportrerun:rm -f "$out/python/$lib"before themvavoids GNUmv's "are the same file" error when cargo has hardlinked both paths.- Docs and quests:
bench/ffi/README.mditem 2 now gives the profile split and no longer mentionsNativeFinalizer.object-ownership.mdbecomes the nursery plan with aMaintainer:decision under## Waiting, andready-await.mdno longer requires it.
claude-opus-5-5 | 𝕏
|
Maintainer decision (2026-11-15, via /quest-run):
(Written by Claude Opus 5.5 with high effort) |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The maintainer decision is recorded on head f291b8f: (Written by Claude Opus 5.5 with low effort) |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This review covers the one commit since the prior Pullfrog review.
- Recorded the nursery decision:
quest/m1/ffi-speed/object-ownership.mdnow states the nursery is settled by the maintainer. It moves the accepted later free and the rejected alternatives into the Plan, and drops the## Waitingsection. The change is docs only.
claude-sonnet-5-5 | 𝕏

Summary
Profiles Go's and Dart's object return, takes the two cuts that don't change when an object is freed, and rewrites object-ownership to the work left: the nursery the maintainer chose.
What the profile shows. Each runtime spends most of an object return registering the object with its collector. The binding can't cut that cost without changing when a dropped object is freed.
perf,track.Info()returned and closed):runtime.AddCleanupplus theCleanup.StopthatClosemakes take 39%. The three cgo crossings (the call, the value's drop, the handle's free) take a third. The collector's own threads take a tenth.Dart_NewFinalizableHandle, its crossing, and its finalization by the collector cost 1,600 instructions per object, three quarters of a return and close. A variant without them took 62 to 65 ns, against 176 to 245.What changes
own_<c>andlend_<c>allocate the object and itsrt.Handle_together, in one anonymous struct. Each call that holds the handle now keeps the object reachable, so a collected object has no holds. The cleanup therefore frees the raw handle directly (free_<c>(raw)), andHandle_.Collectedis gone.rt.NewHandle_becomes(*Handle_).Init. That costs 70 fewer instructions and one fewer allocation per object._drop()no longer builds and ends an_Arenait never used. That costs 136 fewer instructions per close.just ffi bench-build: a rerun failed withmv: ... are the same file. Cargo hardlinkstarget/release/libmoq.soto its build of the same features, which the last run had moved topython/. The recipe now removes the stale copy first.object-ownership.mdbecomes the nursery that would skip the registration for objects closed early. It records the measured alternatives it rejects and the maintainer's decision to build it.ready-await.mdno longer requires it. It profiles the await with ownership's share set apart, using the split in the bench README, rather than waiting on that decision.bench/ffi/README.md's "What a call spends" now gives the split. It no longer says Dart uses aNativeFinalizer; that changed in perf(ffi): cheaper Dart futures ready at their first poll, and cheaper object handles #486.Public API
No generated API changes. The generated object types keep
Handle_ *rt.Handle_, so copying one copies only a pointer, as before. The runtime package'sNewHandle_andHandle_.Collectedare replaced byHandle_.Init. That package documents all three as "for the generated packages: use none of it". Wire: none.Validation
just cipasses. This includesapi_go, whoseownedcheck waits for "the GC frees what nobody closed", and which now passes through the direct-free cleanup.api_dartandgo_modulepass too.Instruction counts (
perf stat -e instructions:u) of an object-only program built against the export before and after, three runs each, on nixos (Ryzen 9 5900XT):Go's user cycles fell 2.5 to 3% over the whole program.
The full bench programs, before and after, in turn for five rounds at a load of 5.6 to 26 (median CPU ns):
Go's 70 instructions are within this load's noise. The stamped write returns no object since feat(ffi)!: Copy types cross by value #742 made
Timestampcross by value, so it moved by noise alone.Measured and rejected:
NativeFinalizer.attachwithoutdetach: 770 to 1,070 ns per return.runtime.SetFinalizer: 13% fewer instructions thanAddCleanupplusStopin a microbenchmark. But it runs every free on one goroutine and keeps each object for one more collection.Close: 290 to 450 ns per object against 120 forStop, in a microbenchmark.Decisions
Settled by the maintainer (2026-11-15, comment), recorded in the quest's Plan; not implemented here:
Choices made without asking, for review:
ready-await.md'sRequiredon this quest. Otherwise it would stay blocked on that decision, and the bench README now gives ownership's share for it to set apart.Close(drop and free together). It either reports a spuriousErrClosedto a call racing aClosethat then fails, or reads Go's atomic state from C. It also overlaps shared-borrow's rewrite of the state word._owncrossing into the returning call's glue: about 24 ns, direct returns only. The nursery would remove that crossing for objects closed early anyway.Follow-ups
None new. The rewritten object-ownership holds the nursery, now settled.
Caused by
mv, which refuses on a rerun when cargo has hardlinked the same file to both paths (found by person: this run)Models
Size: M
Implementer: Claude, claude-opus-5-5, unknown effort (arm B)
Implementer: Claude Code, claude-opus-5-5, low effort (iterate, XS, arm B)
🤖 Generated with Claude Code
(Written by Claude Opus 5.5 with low effort)
Review
Rewritten by
quest reviewafter each round.Head: f291b8f
Base: origin/main at 4099730
Risk: medium,
the base has no RISK.md to name what is always high or customer-facing, so every change is mediumClassified: medium by default
Reviewer: Codex, gpt-6.1-sol, high effort
Session:
01a1de16-0427-7610-8889-468eb6a401f4, round 2Score: 9/10,
The incremental change only records the nursery decision in documentation and introduces no findings; the previously reviewed ownership changes remain unverified by builds or tests.Rounds: 2; first round 0; findings 0: fixed 0, refuted 0, open 0
Findings: none
Reviewer: Codex, gpt-6.1-sol, medium effort, shadow
Session:
01a1dea9-4f07-7fb0-a930-8b724c4bf5f6, round 1Score: 9/10,
The ownership changes appear consistent with existing lifetime handling and have no actionable findings, though builds and tests were not run as instructed.Rounds: 1; first round 0; findings 0: fixed 0, refuted 0, open 0
Findings: none
Summary by CodeRabbit
Summary by CodeRabbit