From 231334ef959a53fbdeed66ed0cf841ca545f7dd2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:50:30 -0800 Subject: [PATCH 01/13] chore(quest): claim quest/m1/ffi-speed/object-ownership.md Quest-Claim: 1d8c38851c77762d From a4d8f5f0600e81b072e5ae61dfd86225995045f7 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:16:29 -0800 Subject: [PATCH 02/13] perf(ffi): one allocation for a Go object and its handle, and no arena 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 --- bench/ffi/README.md | 16 +++++-- ffi/justfile | 3 ++ ffi/runtime/rt.go | 16 +++---- ffi/src/dart.rs | 2 +- ffi/src/go.rs | 9 ++-- quest/m1/ffi-speed/object-ownership.md | 59 ++++++++++++++++++++------ quest/m1/ffi-speed/ready-await.md | 18 ++++---- 7 files changed, 81 insertions(+), 42 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 198622533..6f2bf92f0 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -371,9 +371,19 @@ frame path: [Copy values](#copy-values): what remains is each language's call and, in Kotlin and Python, an object of its own. 2. **Ownership of a returned object.** An object return costs C 25 ns, but Go 263 and - Dart 242, ten times C: each new Go object registers `runtime.AddCleanup` and each Dart - one a `NativeFinalizer`, beside a handle allocation. Kotlin's 137 ns wall (210 CPU) is a phantom - reference linked and unlinked under one global lock. Every returned frame, group, and future pays it. + Dart 242, ten times C, and most of the difference is each runtime's per-object + registration with its collector. Profiled 2026-11-14 on **nixos** with `perf`, for an + object returned and closed: Go spends 39% of it in `runtime.AddCleanup` and the + `Cleanup.Stop` its `Close` makes, a third in its three cgo crossings (the call, the + value's drop, the handle's free), and a tenth in the collector's own threads. Dart's + `Dart_NewFinalizableHandle`, its crossing, and the collector finalizing it cost 1,600 + instructions an object, three quarters of it: without them, a return and close took + 62 to 65 ns against 176 to 245. Since kixel-ai/rust2#828, a Go object and its handle + are one allocation (70 fewer instructions) and a Dart close drops the value without + an `_Arena` (136 fewer). [Batched ownership](../../quest/m1/ffi-speed/object-ownership.md) + would take the registration off objects closed soon after their return. Kotlin's + 137 ns wall (210 CPU) is a phantom reference linked and unlinked under one global lock. + Every returned frame, group, and future pays it. 3. **A woken read.** C's 427 ns is Rust's 329 plus a future's crossings. Kotlin's 1.6 us and Dart's 1.8 us add their event loops; Go's 5.1 us and Swift's 5.7 us wall (16.6 us CPU) are a goroutine or `Task` and a cross-thread wake per read, and Python's 6.9 us is diff --git a/ffi/justfile b/ffi/justfile index da1468d63..6f514371a 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -468,6 +468,9 @@ moq-export checkout="": (moq-write checkout) *) lib=libmoq.so ;; esac cargo build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features python + # Cargo links the output to its build of the same features, which a rerun's last move + # left here, and mv refuses to move a file onto itself. + rm -f "$out/python/$lib" mv "$out/target/release/$lib" "$out/python/$lib" # The library the C programs link, and the shared one Kotlin and Dart load. cargo build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features kotlin,dart 2>&1 | tee target/moq-build.log diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index dd8a482c9..5e14c1257 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -75,12 +75,13 @@ type Handle_ struct { Cleanup runtime.Cleanup } -// NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends, and drop -// drops the value of, or nil for one Rust does not check. -func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) *Handle_ { - h := &Handle_{raw: raw, free: free, drop: drop, name: name} +// Init makes h a handle of raw, which free frees, or nil for one Rust lends, and drop +// drops the value of, or nil for one Rust does not check. The generated packages allocate +// it with the object owning it, which each call holding it then keeps reachable: so once +// the GC collects the object, no call holds the handle, and its cleanup frees it at once. +func (h *Handle_) Init(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) { + h.raw, h.free, h.drop, h.name = raw, free, drop, name h.state.Store(held_) - return h } // Hold is the handle, held until Release, or nil once closed. @@ -150,11 +151,6 @@ func (h *Handle_) Moved() { h.abandon() } -// Collected frees an owned handle the GC collected the object of, once no call holds it. -func (h *Handle_) Collected() { - h.abandon() -} - // abandon gives up the owner's hold, freeing it once no call holds it. func (h *Handle_) abandon() { for { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 919085536..400462ca4 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -1751,7 +1751,7 @@ impl<'a> Gen<'a> { let what = format!("{c}_drop"); let f = self.served_fn(&what, vec![owner.clone(), Ty::Out(Box::new(Ty::Status))], Ty::Void); body.push(format!( - "\t@override\n\tvoid _drop() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\tfinal s_ = _n.status;\n\t\t\t{f}(_n.home, _raw, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t@override\n\tvoid _drop() {{\n\t\tfinal s_ = _n.status;\n\t\t{f}(_n.home, _raw, s_);\n\t\t_status(s_.value, '{what}');\n\t}}\n" )); } self.traits(&ty, &c, object.traits, &mut body, &mut members); diff --git a/ffi/src/go.rs b/ffi/src/go.rs index fc738acc7..65e89706e 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -357,7 +357,6 @@ const TAKEN: &[&str] = &[ "ErrBorrowed", "ErrClosed", "Handle_", - "NewHandle_", "LiftBytes_", "BytesBuf_", "GoCall_", @@ -2037,7 +2036,6 @@ impl<'a> Gen<'a> { let (_, local) = self.named(&ty); let name = self.qualified(&ty); let handle = self.qualify(Packages::RT, "Handle_"); - let new = self.qualify(Packages::RT, "NewHandle_"); let (drop, dropping) = match self.abi.checked[i] { true => ( format!( @@ -2053,10 +2051,11 @@ impl<'a> Gen<'a> { format!( "func free_{c}(p unsafe.Pointer) {{\n\tC.{c}_free((*C.{c})(p))\n}}\n\n\ {drop}\ - // own_{c} owns a handle Rust returned, or is nil for none.\n\ - func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\tv := &{name}{{Handle_: {new}(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")}}\n\tv.Handle_.Cleanup = runtime.AddCleanup(v, (*{handle}).Collected, v.Handle_)\n\treturn v\n}}\n\n\ + // own_{c} owns a handle Rust returned, or is nil for none: the object and its handle\n\ + // in one allocation, whose cleanup frees the handle.\n\ + func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\to := &struct {{\n\t\tv {name}\n\t\th {handle}\n\t}}{{}}\n\to.h.Init(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")\n\to.v.Handle_ = &o.h\n\to.h.Cleanup = runtime.AddCleanup(&o.v, free_{c}, unsafe.Pointer(raw))\n\treturn &o.v\n}}\n\n\ // lend_{c} is a handle Rust lends for a call, which it frees itself.\n\ - func lend_{c}(raw *C.{c}) *{name} {{\n\treturn &{name}{{Handle_: {new}(unsafe.Pointer(raw), nil, nil, \"{local}\")}}\n}}\n\n\ + func lend_{c}(raw *C.{c}) *{name} {{\n\to := &struct {{\n\t\tv {name}\n\t\th {handle}\n\t}}{{}}\n\to.h.Init(unsafe.Pointer(raw), nil, nil, \"{local}\")\n\to.v.Handle_ = &o.h\n\treturn &o.v\n}}\n\n\ // holdOption_{c} is v's handle, held until the call returns, or nil for none.\n\ func holdOption_{c}(a_ *arena_, v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn (*C.{c})(a_.hold(v.Handle_, v))\n}}\n\n" ) diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index ebe67c0e5..77d47295b 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -1,21 +1,54 @@ -# [M] Cheaper returned-object ownership in Go and Dart +# [L] Returned objects closed early skip the collector in Go and Dart ## Goal A [rust2-ffi](/ffi/README.md) Go or Dart binding returns an object for close to what C -pays. [The binding benchmark](/bench/ffi/README.md) measures an object return at 263 ns -in Go and 242 in Dart against C's 25, and every returned frame, group, and future pays it. +pays when the caller closes it soon after. [The binding benchmark](/bench/ffi/README.md) +measures an object return at 263 ns in Go and 242 in Dart against C's 25, and every +returned frame, group, and future pays it. Ownership stays automatic: a dropped object is still freed without a call from the user. ## Plan -- Profile first: split the cost between the handle allocation, Go's `runtime.AddCleanup`, - Dart's `Dart_NewFinalizableHandle`, and the crossing, and fix what dominates. -- Two candidates this quest first listed are already how the code works, found by the - 2026-11-12 audit: Dart objects use `Dart_NewFinalizableHandle` with a size of 0 - (`ffi/runtime/dart.rs`), and Go registers one cleanup with the handle as its argument - (`ffi/src/go.rs`'s `own_`). So take candidates from the profile. One left: allocate the - wrapper and its handle together, so a return allocates once. -- Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, - since a missed `Close` would leak. -- Re-measure the bench's object return and stamped write. +- The profile ([the benchmark's README](/bench/ffi/README.md#what-a-call-spends), item 2) + puts most of the cost in each runtime's per-object registration with its collector: Go's + `runtime.AddCleanup` and the `Cleanup.Stop` its `Close` makes take 39% of a return and + close, and Dart's `Dart_NewFinalizableHandle`, its crossing, and its finalization take + three quarters (1,600 instructions). Without the registration, Dart's return and close + took 62 to 65 ns against 176 to 245. kixel-ai/rust2#828 took what it could without + deferring the registration: one allocation for a Go object and its handle, and no + `_Arena` in a Dart close. +- Rejected, measured: Dart's `NativeFinalizer.attach` without `detach` (770 to 1,070 ns + a return), Go's `runtime.SetFinalizer` (13% fewer instructions to register and clear + than `AddCleanup` and `Stop`, but it runs every free on one goroutine and keeps each + object a collection longer), and leaving Go's cleanup registered at `Close` (290 to 450 + ns a return against 120, in a microbenchmark). +- Recommended: a nursery of the last 64 objects returned, which only registers an object + with the collector once it leaves the nursery still open. A `close` of an object in the + nursery frees its handle at once, with a checked value's drop in the same crossing, and + it never registers. Explicit `Close` stays, and the registration stays as the backstop. + - Dart: one ring in the isolate's `_n`, holding its objects, whose oldest is registered + as today when a return pushes it out. A checked object's handle is then freed at + `close`, so its calls check a closed flag in Dart, as a counted one's do (one load a + call). One finalizable handle on the ring frees what it still holds at isolate exit. + - Go: a ring per P, through a `sync.Pool` as `lending_` pools byte batches. Each + object points at its ring and the ring at its objects, so one `AddCleanup` on the ring + frees its open slots once all of them are unreachable, as when the pool drops it. A + compare-and-swap on the object's slot decides between its `Close` and its eviction. +- Re-measure the bench's object return and ready read, and check by drop counts in + `ffi/tests` that an object dropped in the nursery is freed once evicted and collected. + +## Waiting + +- Maintainer: accept the later free the nursery brings? An object dropped without `Close` + is freed only after up to 64 more returns, or in Go once its ring's other objects are + unreachable too, rather than at the next collection. Options: (a) the nursery as + planned (recommended: it takes Dart's object return from about 200 ns to about 65, and + Go's by about 110 ns, while a closed object never waits); (b) keep today's per-object + registration and delete this quest; (c) flush the nursery on a timer each event-loop + turn instead of by count (rejected: a timer each turn costs more than it saves when a + turn returns one object, as a woken read does). + +## Related + +- [Shared borrows](/quest/m1/ffi-speed/shared-borrow.md) - rewrites the same handle word diff --git a/quest/m1/ffi-speed/ready-await.md b/quest/m1/ffi-speed/ready-await.md index a8b577aa2..65f6ec52c 100644 --- a/quest/m1/ffi-speed/ready-await.md +++ b/quest/m1/ffi-speed/ready-await.md @@ -3,20 +3,18 @@ ## Goal A [rust2-ffi](/ffi/README.md) Swift, Go, or Dart await of a future ready at its first -poll costs close to C's, once [returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) -is out of the way. Since `_start` made a ready call one crossing and one allocation, -[the binding benchmark](/bench/ffi/README.md)'s ready read costs C 126 ns over its write, +poll costs close to C's, apart from the returned object's ownership, which +[returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) owns. Since +`_start` made a ready call one crossing and one allocation, [the binding benchmark](/bench/ffi/README.md)'s ready read costs C 126 ns over its write, Rust 93, and Swift 289, Go 414, and Dart 522. Less the returned frame's ownership, Swift's await still adds about 150 ns over C's, Go's 100, and Dart's 180. ## Plan -- Profile each language's ready path from the generated call to the result, after - object-ownership lands so its share is gone: Swift's `async` entry and `Native.start`, - Go's escaped out-pointers and error check, and Dart's `_Call` and `_Arena`. +- Profile each language's ready path from the generated call to the result, setting the + returned frame's ownership apart (its split is in + [the benchmark's README](/bench/ffi/README.md#what-a-call-spends), item 2): Swift's + `async` entry and `Native.start`, Go's escaped out-pointers and error check, and Dart's + `_Call` and `_Arena`. - Fix what dominates in the generator or runtime, with no API change. - Re-measure the ready read interleaved with the commit before, CPU first. - -## Required - -- [Returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - removes Go's and Dart's ownership share of the ready read, so the profile shows only the await's From f291b8f301d6f80b060272ed4215fa8098344671 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:54:57 -0800 Subject: [PATCH 03/13] docs(quest): object-ownership records the nursery decision Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-speed/object-ownership.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index 77d47295b..c3ff744fb 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -23,7 +23,8 @@ Ownership stays automatic: a dropped object is still freed without a call from t than `AddCleanup` and `Stop`, but it runs every free on one goroutine and keeps each object a collection longer), and leaving Go's cleanup registered at `Close` (290 to 450 ns a return against 120, in a microbenchmark). -- Recommended: a nursery of the last 64 objects returned, which only registers an object +- Settled by the maintainer (2026-11-15, kixel-ai/rust2#828): a nursery of the last 64 + objects returned, which only registers an object with the collector once it leaves the nursery still open. A `close` of an object in the nursery frees its handle at once, with a checked value's drop in the same crossing, and it never registers. Explicit `Close` stays, and the registration stays as the backstop. @@ -35,20 +36,15 @@ Ownership stays automatic: a dropped object is still freed without a call from t object points at its ring and the ring at its objects, so one `AddCleanup` on the ring frees its open slots once all of them are unreachable, as when the pool drops it. A compare-and-swap on the object's slot decides between its `Close` and its eviction. + - The later free is accepted: an object dropped without `Close` is freed only after up + to 64 more returns, or in Go once its ring's other objects are unreachable too, + rather than at the next collection. Rejected: keeping today's per-object + registration and deleting this quest; flushing the nursery each event-loop turn (a + timer each turn costs more than it saves when a turn returns one object, as a woken + read does). - Re-measure the bench's object return and ready read, and check by drop counts in `ffi/tests` that an object dropped in the nursery is freed once evicted and collected. -## Waiting - -- Maintainer: accept the later free the nursery brings? An object dropped without `Close` - is freed only after up to 64 more returns, or in Go once its ring's other objects are - unreachable too, rather than at the next collection. Options: (a) the nursery as - planned (recommended: it takes Dart's object return from about 200 ns to about 65, and - Go's by about 110 ns, while a closed object never waits); (b) keep today's per-object - registration and delete this quest; (c) flush the nursery on a timer each event-loop - turn instead of by count (rejected: a timer each turn costs more than it saves when a - turn returns one object, as a woken read does). - ## Related - [Shared borrows](/quest/m1/ffi-speed/shared-borrow.md) - rewrites the same handle word From 6a62c29335fb61465dba3bb43b824e8e1d94cb74 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:05:55 -0800 Subject: [PATCH 04/13] chore(quest): claim quest/m1/ffi-speed/object-ownership.md Quest-Claim: 95ae5bfab8f203b1 From 354c24457a5d11dde804090be38729df57158ec6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:19:39 -0800 Subject: [PATCH 05/13] perf(ffi): Go objects closed soon after their return skip the GC's registration Co-Authored-By: Claude Opus 5.5 --- ffi/justfile | 4 +- ffi/runtime/rt.go | 99 ++++++++++++++++++++++++++++++++++++++++--- ffi/src/go.rs | 4 +- ffi/tests/api.go | 74 ++++++++++++++++++++++++++++++++ ffi/tests/fixtures.rs | 28 ++++++++++++ 5 files changed, 199 insertions(+), 10 deletions(-) diff --git a/ffi/justfile b/ffi/justfile index 6f514371a..b576fa26f 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -45,9 +45,9 @@ shapes: android: cargo test --locked --test fixtures -- --ignored --exact api_android -# Run the api fixture's C and C++ scripts under AddressSanitizer and ThreadSanitizer, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. +# Run the api fixture's C and C++ scripts under AddressSanitizer and ThreadSanitizer, its Go script under Go's race detector, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. sanitize: - cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_miri + cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_go_race api_miri # Apply formatters without changing program behavior. fix: diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 5e14c1257..0c78463ab 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -71,8 +71,12 @@ type Handle_ struct { // Non-nil for an owned one of a value Rust checks. drop func(unsafe.Pointer) error name string - // What frees an owned handle once the GC collects its object. - Cleanup runtime.Cleanup + // The nursery an owned handle was returned into, and its slot there. + nursery *nursery_ + slot int + // What frees an owned handle once the GC collects its object, from when it leaves its + // nursery still open. + cleanup runtime.Cleanup } // Init makes h a handle of raw, which free frees, or nil for one Rust lends, and drop @@ -84,6 +88,91 @@ func (h *Handle_) Init(raw unsafe.Pointer, free func(unsafe.Pointer), drop func( h.state.Store(held_) } +// Own puts h, which Rust returned and its object owns, in a nursery of the latest +// returned, where a Close frees it without the GC's registration, which costs more than +// the rest of a return and close together. One that leaves it open, pushed out by later +// returns or with its nursery once the pool drops that, is registered then. For the +// generated packages. +func (h *Handle_) Own() { + r, _ := nurseries_.Get().(*ring_) + if r == nil { + r = &ring_{n: new(nursery_)} + runtime.AddCleanup(r, (*nursery_).flush, r.n) + } + r.n.push(h) + nurseries_.Put(r) +} + +// nurseries_ holds the ring each P returns its objects into; a GC drops an idle one. +var nurseries_ sync.Pool + +// ring_ is what the pool holds of a nursery, which its handles point at: so its cleanup +// runs once the pool dropped it, whatever its handles do. +type ring_ struct { + n *nursery_ +} + +// nurseryLen_ is how many of the latest objects returned a nursery holds: an object +// dropped unclosed is freed only once this many more were returned on its P, or the pool +// dropped its nursery, so it bounds what a dropped object keeps of Rust's. +const nurseryLen_ = 64 + +// nursery_ is the latest handles returned, oldest at next once full. A slot's raw handle is +// cleared by whichever comes first, of its Close and its eviction, with a compare-and-swap. +type nursery_ struct { + // Kept reachable while here, so an eviction can register them. Only the ring's holder + // touches these two. + held [nurseryLen_]*Handle_ + next int + // Each slot's raw handle until closed or evicted, else nil. + raw [nurseryLen_]unsafe.Pointer +} + +func (n *nursery_) push(h *Handle_) { + i := n.next + n.next = (i + 1) % nurseryLen_ + if old := n.held[i]; old != nil { + n.evict(i, old) + } + n.held[i] = h + h.nursery, h.slot = n, i + atomic.StorePointer(&n.raw[i], h.raw) +} + +// evict registers h, in slot i, with the GC, unless a Close took the slot first. +func (n *nursery_) evict(i int, h *Handle_) { + if atomic.LoadPointer(&n.raw[i]) == nil { + return + } + // Registered before the slot is given up, so a Close that finds it given up finds the + // cleanup to stop. + c := runtime.AddCleanup(h, h.free, h.raw) + h.cleanup = c + if !atomic.CompareAndSwapPointer(&n.raw[i], h.raw, nil) { + c.Stop() + } +} + +// flush registers each handle still open in n, once the pool dropped its ring, so none +// waits on the others to be collected. +func (n *nursery_) flush() { + for i := range n.raw { + // Loaded first, as push stores it after the handle. + if atomic.LoadPointer(&n.raw[i]) != nil { + n.evict(i, n.held[i]) + } + } +} + +// leave takes h out of its nursery as it closes, before anything frees it, or else stops +// the cleanup its eviction registered. +func (h *Handle_) leave() { + if n := h.nursery; n != nil && atomic.CompareAndSwapPointer(&n.raw[h.slot], h.raw, nil) { + return + } + h.cleanup.Stop() +} + // Hold is the handle, held until Release, or nil once closed. func (h *Handle_) Hold() unsafe.Pointer { for { @@ -133,8 +222,7 @@ func (h *Handle_) Close() error { return checkError_{h.name, "a handle is already borrowed", 3} } if h.state.CompareAndSwap(n, closed_) { - // A cleanup left registered until the GC slows each one registered after it. - h.Cleanup.Stop() + h.leave() if h.free != nil { h.free(h.raw) } @@ -146,8 +234,6 @@ func (h *Handle_) Close() error { // Moved closes an owned handle a call moved the value out of, or Close dropped, freeing it // once no call holds it. func (h *Handle_) Moved() { - // A cleanup left registered until the GC slows each one registered after it. - h.Cleanup.Stop() h.abandon() } @@ -160,6 +246,7 @@ func (h *Handle_) abandon() { } next := (n | closed_) - held_ if h.state.CompareAndSwap(n, next) { + h.leave() if next < held_ && h.free != nil { h.free(h.raw) } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index f1015031d..e93615a97 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2169,8 +2169,8 @@ impl<'a> Gen<'a> { "func free_{c}(p unsafe.Pointer) {{\n\tC.{c}_free((*C.{c})(p))\n}}\n\n\ {drop}\ // own_{c} owns a handle Rust returned, or is nil for none: the object and its handle\n\ - // in one allocation, whose cleanup frees the handle.\n\ - func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\to := &struct {{\n\t\tv {name}\n\t\th {handle}\n\t}}{{}}\n\to.h.Init(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")\n\to.v.Handle_ = &o.h\n\to.h.Cleanup = runtime.AddCleanup(&o.v, free_{c}, unsafe.Pointer(raw))\n\treturn &o.v\n}}\n\n\ + // in one allocation, in the nursery Own puts it in.\n\ + func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\to := &struct {{\n\t\tv {name}\n\t\th {handle}\n\t}}{{}}\n\to.h.Init(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")\n\to.v.Handle_ = &o.h\n\to.h.Own()\n\treturn &o.v\n}}\n\n\ // lend_{c} is a handle Rust lends for a call, which it frees itself.\n\ func lend_{c}(raw *C.{c}) *{name} {{\n\to := &struct {{\n\t\tv {name}\n\t\th {handle}\n\t}}{{}}\n\to.h.Init(unsafe.Pointer(raw), nil, nil, \"{local}\")\n\to.v.Handle_ = &o.h\n\treturn &o.v\n}}\n\n\ // holdOption_{c} is v's handle, held until the call returns, or nil for none.\n\ diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 26a7735e5..d2b7fe392 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1378,9 +1378,83 @@ func owned() { forward() collected(before+1, "a forwarded payload drops once nothing reaches it") uncollected(before+1, "a forwarded payload drops once") + + nursed() fmt.Println("owned ok") } +// nursed checks the nursery of the latest objects returned, which a Close leaves at once +// and the GC frees only once later returns push one out unclosed: each object is freed +// once, however Close and the nursery race. +func nursed() { + // One P, so every return below shares one nursery, which each GC finds in use, so its + // pool never drops it: only the push out of it registers the dropped token. The race + // detector's pool drops some at random, which frees it as a dropped nursery does. + procs := runtime.GOMAXPROCS(1) + before := api.Dropped() + unclosed() + deadline := time.Now().Add(10 * time.Second) + for api.Dropped() < before+1 { + check(time.Now().Before(deadline), "the GC frees an object pushed out of its nursery unclosed") + fill() + runtime.GC() + runtime.Gosched() + } + + // Closed after the push out registered it, so its cleanup is stopped. + kept := api.NewToken(4) + fill() + kept.Close() + check(api.Dropped() == before+2, "closing an object pushed out of its nursery frees it at once") + runtime.GOMAXPROCS(procs) + uncollected(before+2, "the GC frees nothing a Close freed after a push out") + + // Returned on many goroutines, each closing some of its own and of another's, as + // others' returns push them out, and dropping the rest. + const workers, each = 8, 1000 + handoff := make(chan *api.Token, workers*each) + var wg sync.WaitGroup + for w := range workers { + wg.Add(1) + go func() { + defer wg.Done() + for i := range each { + t := api.NewToken(uint32(i)) + switch i % 3 { + case 0: + t.Close() + case 1: + handoff <- t + } + if i%3 != 0 { + select { + case other := <-handoff: + other.Close() + default: + } + } + if w == 0 && i%100 == 0 { + runtime.GC() + } + } + }() + } + wg.Wait() + close(handoff) + for t := range handoff { + t.Close() + } + collected(before+2+workers*each, "the GC frees every object a nursery held unclosed") + uncollected(before+2+workers*each, "each object is freed once, however its Close and its nursery race") +} + +// fill returns a nursery's worth of objects, each closed, pushing out every earlier one. +func fill() { + for range 64 { + api.NewTally().Close() + } +} + // forward reads a payload Rust returned and writes it back, whole, sliced, and in a // record: Rust takes a second owner of its own buffer each time, so the bytes stay where // Rust put them, in a frame of its own so nothing keeps them reachable after. diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 595de2352..4c27e8803 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -286,6 +286,34 @@ fn api_tsan() { sanitized("thread"); } +/// The Go script under Go's race detector, which catches a race between an object's +/// `Close` and the nursery that pushes it out on another goroutine. +#[test] +#[ignore = "slow: just ffi sanitize"] +fn api_go_race() { + let api = api(); + let dir = scratch().join("api").join("go-race"); + let module = dir.join("module"); + go_layout( + api, + &module, + &std::fs::read_to_string(manifest_dir().join("tests").join("api.go")).unwrap(), + ); + let exe = dir.join(format!("api-test{EXE_SUFFIX}")); + run(go(&module, api, &["build", "-race", "-o"]).arg(&exe)); + // Every report fails the run. + let env = [("GORACE", "halt_on_error=1")]; + let output = run(Command::new(&exe).envs(env)); + assert_eq!( + String::from_utf8_lossy(&output.stdout), + api.transcript, + "the Go transcript under the race detector differs from the native one" + ); + for case in ["futures", "owned", "closed", "bytes", "closures"] { + run(Command::new(&exe).arg(case).envs(env)); + } +} + /// Build the api fixture's glue, std included, under the `sanitizer`, and run the C and /// C++ scripts against it, built by clang under the same one. fn sanitized(sanitizer: &str) { From bdcb3aa12352946a810193bcceeb20b1ae58f109 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:26:48 -0800 Subject: [PATCH 06/13] perf(ffi): Dart objects closed soon after their return skip the collector's registration Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/dart.rs | 33 +++++++++++++++ ffi/runtime/rt.dart | 101 ++++++++++++++++++++++++++++++++++++++------ ffi/src/dart.rs | 86 +++++++++++++++++++++++++++++-------- ffi/tests/api.dart | 40 ++++++++++++++++++ 4 files changed, 229 insertions(+), 31 deletions(-) diff --git a/ffi/runtime/dart.rs b/ffi/runtime/dart.rs index c1ee55f26..f36cb272e 100644 --- a/ffi/runtime/dart.rs +++ b/ffi/runtime/dart.rs @@ -134,6 +134,39 @@ pub unsafe fn disown(own: *mut c_void, object: *mut c_void) { unsafe { (api().unfinalize)(own, object) } } +/// The slots of a nursery of `len` objects, which Dart fills: `len`, then each object's +/// handle, then the C function freeing it, the handle null while the slot is out. Once the +/// garbage collector finds `object`, the nursery, unreachable, as when its isolate exits, +/// each handle still in a slot is freed. +/// +/// # Safety +/// Called from Dart in its isolate, with `object` a handle of the Dart object, which writes +/// only handles the function beside each frees, and only while reachable. +pub unsafe fn nursery(object: *mut c_void, len: usize) -> *mut *mut c_void { + unsafe extern "C" fn finalize(_: *mut c_void, slots: *mut c_void) { + let slots = slots.cast::<*mut c_void>(); + // SAFETY: `nursery` made `slots`, whose first word is its length. + let len = unsafe { *slots } as usize; + // SAFETY: as above, nothing writes it once the nursery is unreachable. + let slots = unsafe { Box::from_raw(std::ptr::slice_from_raw_parts_mut(slots, 1 + 2 * len)) }; + for i in 0..len { + let raw = slots[1 + i]; + if !raw.is_null() { + // SAFETY: Dart's contract: each handle beside the function that frees it. + let free = unsafe { std::mem::transmute::<*mut c_void, unsafe extern "C" fn(*mut c_void)>(slots[1 + len + i]) }; + // SAFETY: as above, and this frees each at most once, as no Dart object owns it. + unsafe { free(raw) }; + } + } + } + let mut slots = vec![std::ptr::null_mut::(); 1 + 2 * len].into_boxed_slice(); + slots[0] = len as *mut c_void; + let slots = Box::into_raw(slots).cast::<*mut c_void>(); + // SAFETY: the caller's contract. + unsafe { own(object, slots.cast(), finalize) }; + slots +} + /// An isolate: its port, and the calls into it other threads wait on. One lives for each /// isolate that loads the library, and is never freed, since nothing tells Rust an /// isolate exited. diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 0b12fd6ed..f6cb903f1 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -360,10 +360,10 @@ final class _Callback extends ffi.Struct { /// What an object owns: its handle, or one Rust lends for a call, which Rust frees. An owned /// one of a value Rust checks is Rust's alone to account: [close] drops the value as Rust's -/// own drop does, failing while a call borrows it, a call after it throws as Rust's check -/// of a moved value does, and the handle is freed once the garbage collector finds this -/// unreachable. Any other counts its calls: [close] frees it then, throwing while a call -/// holds it, and a call after it throws. +/// own drop does and frees the handle, failing while a call borrows it. Any other counts its +/// calls: [close] frees it then, throwing while a call holds it. A call after either throws. +/// One returned waits in the isolate's [_Nursery], where a close frees it without the +/// garbage collector, which frees it only once pushed out unclosed. abstract base class _Object implements ffi.Finalizable { final ffi.Pointer _raw; @@ -375,40 +375,112 @@ abstract base class _Object implements ffi.Finalizable { /// Whether it counts its calls, rather than Rust. final bool _counted; - /// The garbage collector's hold of an owned one's handle, until freed. + /// The garbage collector's hold of an owned one's handle, from when the nursery pushed it + /// out until freed. ffi.Pointer _own = ffi.nullptr; + /// Its slot in the nursery while there, else -1. + int _slot = -1; + _Object(this._raw, bool owned, bool checked) : _owned = owned, _counted = !(owned && checked); String get _type; - /// Free the handle and detach its finalizer. - void _free(); + /// Free a counted one's handle, and take back the garbage collector's hold of it. + void _free() {} + + /// Give the garbage collector a hold of the handle, as the nursery pushes it out open. + void _register() {} - /// Drop the value of one Rust checks, throwing [StateError] if a call borrows it. - void _drop() {} + /// Drop the value of one Rust checks, then free its handle and take back the garbage + /// collector's hold of it, in one crossing: throwing [StateError], changing nothing, if a + /// call borrows it. + void _close() {} ffi.Pointer _hold() { - if (!_counted) return _raw; if (_closed) throw StateError('$_type is closed'); - _holds++; + if (_counted) _holds++; return _raw; } void _release() { - if (_counted && --_holds == 0 && _owned) _free(); + if (_counted && --_holds == 0 && _owned) { + _leave(); + _free(); + } + } + + /// Take it out of the nursery, returning whether it was there: before its free, which + /// may run Dart that returns objects into the nursery meanwhile. + bool _leave() { + final slot = _slot; + if (slot < 0) return false; + _slot = -1; + _n.nursery.leave(slot); + return true; } /// Close it as [_Object] says; again, it does nothing. void close() { - if (!_counted) return _drop(); if (_closed) return; + if (!_counted) { + final nursed = _leave(); + try { + _close(); + } catch (_) { + // Still open, and out of the nursery, so the garbage collector's. + if (nursed) _register(); + rethrow; + } + _closed = true; + return; + } if (_holds != 1) throw StateError('$_type: a handle is already borrowed'); _closed = true; _release(); } } +/// How many of the latest objects returned the nursery holds: one dropped unclosed is freed +/// only once this many more were returned, so it bounds what a dropped object keeps of +/// Rust's. +const _nurseryLen = 64; + +/// The latest objects returned, oldest at [_next] once full: a close takes one out without +/// the garbage collector's hold of its handle, which costs more than the rest of a return +/// and close together, and one pushed out still open takes that hold then. Rust keeps a copy +/// of each slot's handle and the C function freeing it, and frees what is still in them once +/// the garbage collector finds the nursery unreachable, as when the isolate exits. +final class _Nursery { + final List<_Object?> _held = List<_Object?>.filled(_nurseryLen, null); + int _next = 0; + + /// What Rust keeps: the length, then each slot's handle, then each one's free function, + /// null while the slot is out. + late final ffi.Pointer> _slots = _dartNursery(this, _nurseryLen); + + /// Hold `object`, owned, whose handle `free` frees. + void push(_Object object, ffi.Pointer free) { + final i = _next; + _next = i + 1 == _nurseryLen ? 0 : i + 1; + final old = _held[i]; + if (old != null) { + old._slot = -1; + old._register(); + } + _held[i] = object; + object._slot = i; + final slots = _slots; + slots[1 + i] = object._raw; + slots[1 + _nurseryLen + i] = free; + } + + void leave(int slot) { + _held[slot] = null; + _slots[1 + slot] = ffi.nullptr; + } +} + /// Native memory each call lowers its values into, as a stack: a call takes what it /// needs from the top and gives it back once it returns, so a call Rust makes into Dart /// meanwhile takes and gives back above it. @@ -633,6 +705,9 @@ final class _Native { /// queued meanwhile, and the call reads it before any other can run, and a failure /// clears it. late final ffi.Pointer status = _dartAlloc(4).cast()..value = 0; + + /// The latest objects returned. + late final _Nursery nursery = _Nursery(); late final isolate.RawReceivePort _port; late final ffi.Pointer home; diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 2a448dc50..c854f1d2e 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -230,6 +230,13 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result p0", "void", ), + ( + "dart_nursery", + "ffi.Handle, ffi.Size", + "ffi.Pointer>", + "Object p0, int p1", + "ffi.Pointer>", + ), ("dart_abort", "_Buf", "ffi.Void", "_Buf p0", "void"), ( "status_message", @@ -310,6 +317,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result *mut c_void {{\n\tlet ptr = unsafe {{ malloc(len) }};\n\tif ptr.is_null() {{\n\t\tstd::alloc::handle_alloc_error(std::alloc::Layout::array::(len).expect(\"a length\"));\n\t}}\n\tptr\n}}\n\n\ #[unsafe(no_mangle)]\npub extern \"C\" fn {space}_buf(len: usize) -> rt::Buf {{\n\trt::Buf::vec(vec![0; len])\n}}\n\n\ #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_free(ptr: *mut c_void) {{\n\tunsafe {{ free(ptr) }}\n}}\n\n\ + #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_nursery(object: *mut c_void, len: usize) -> *mut *mut c_void {{\n\tunsafe {{ runtime::nursery(object, len) }}\n}}\n\n\ #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_abort(text: rt::Buf) {{\n\trt::fail(\"dart\", unsafe {{ text.str(\"{space}_abort\") }})\n}}\n\n\ #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_reader_read(data: *mut c_void, buf: *mut u8, len: usize, n: *mut usize, err: *mut rt::Buf) -> bool {{\n\tunsafe {{ runtime::reader_read(data, buf, len, n, err) }}\n}}\n\n\ #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {space}_reader_seek(data: *mut c_void, whence: std::ffi::c_int, offset: i64, position: *mut u64, err: *mut rt::Buf) -> bool {{\n\tunsafe {{ runtime::reader_seek(data, whence, offset, position, err) }}\n}}\n\n{}", @@ -877,18 +885,18 @@ impl<'a> Gen<'a> { } /// The Dart functions owning a handle of the object `c`, whose C type is `handle`: one - /// giving the garbage collector a Dart object owning it to free it with, and one taking - /// that back and freeing it, serving the isolate meanwhile. - fn owned(&mut self, c: &str, handle: &Ty) -> (String, String) { + /// giving the garbage collector a Dart object owning it to free it with, the C function + /// freeing it, which the nursery keeps for the isolate's exit, and the one closing it, + /// serving the isolate meanwhile: of a checked value, its drop then its free, else its + /// free alone, each taking back the garbage collector's hold if it took one. + fn owned(&mut self, c: &str, handle: &Ty, checked: bool) -> Owned { let bare = self.bare(c); let space = &self.space; let own = format!("{space}_own_{bare}"); - let free = format!("{space}_free_{bare}"); let raw = rs(handle); let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {own}(object: *mut c_void, raw: *mut c_void) -> *mut c_void {{\n\t// The garbage collector's free runs with no isolate to serve.\n\tunsafe extern \"C\" fn finalize(_: *mut c_void, raw: *mut c_void) {{\n\t\tunsafe {{ crate::{c}_free(raw.cast()) }}\n\t}}\n\tunsafe {{ runtime::own(object, raw, finalize) }}\n}}\n\n\ - #[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {free}(home: *const c_void, own: *mut c_void, object: *mut c_void, raw: {raw}) {{\n\tunsafe {{ runtime::disown(own, object) }};\n\tlet home = unsafe {{ &*home.cast::() }};\n\tunsafe {{ home.serve(move || crate::{c}_free(raw)) }}\n}}\n\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {own}(object: *mut c_void, raw: *mut c_void) -> *mut c_void {{\n\t// The garbage collector's free runs with no isolate to serve.\n\tunsafe extern \"C\" fn finalize(_: *mut c_void, raw: *mut c_void) {{\n\t\tunsafe {{ crate::{c}_free(raw.cast()) }}\n\t}}\n\tunsafe {{ runtime::own(object, raw, finalize) }}\n}}\n\n" ); self.natives.insert( own.clone(), @@ -896,13 +904,46 @@ impl<'a> Gen<'a> { "@ffi.Native Function(ffi.Handle, ffi.Pointer)>(symbol: '{own}')\nexternal ffi.Pointer _{own}(Object p0, ffi.Pointer p1);\n\n" ), ); + let freeing = format!("{space}_freeing_{bare}"); + self.natives.insert( + freeing.clone(), + format!( + "@ffi.Native)>(symbol: '{c}_free')\nexternal void _{freeing}(ffi.Pointer p0);\n\n" + ), + ); + let (close, glue, status) = match checked { + true => ( + format!("{space}_close_{bare}"), + format!( + "let dropped = unsafe {{\n\t\thome.serve(move || {{\n\t\t\tcrate::{c}_drop(raw, status);\n\t\t\tlet dropped = *status == 0;\n\t\t\tif dropped {{\n\t\t\t\tcrate::{c}_free(raw);\n\t\t\t}}\n\t\t\tdropped\n\t\t}})\n\t}};\n\tif dropped && !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}" + ), + (", status: *mut i32", ", ffi.Pointer", ", ffi.Pointer p4"), + ), + false => ( + format!("{space}_free_{bare}"), + format!( + "if !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}\n\tunsafe {{ home.serve(move || crate::{c}_free(raw)) }}" + ), + ("", "", ""), + ), + }; + let _ = write!( + self.glue, + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {close}(home: *const c_void, own: *mut c_void, object: *mut c_void, raw: {raw}{}) {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{glue}\n}}\n\n", + status.0 + ); self.natives.insert( - free.clone(), + close.clone(), format!( - "@ffi.Native, ffi.Pointer, ffi.Handle, ffi.Pointer)>(symbol: '{free}')\nexternal void _{free}(ffi.Pointer p0, ffi.Pointer p1, Object p2, ffi.Pointer p3);\n\n" + "@ffi.Native, ffi.Pointer, ffi.Handle, ffi.Pointer{})>(symbol: '{close}')\nexternal void _{close}(ffi.Pointer p0, ffi.Pointer p1, Object p2, ffi.Pointer p3{});\n\n", + status.1, status.2 ), ); - (format!("_{own}"), format!("_{free}")) + Owned { + own: format!("_{own}"), + freeing: format!("_{freeing}"), + close: format!("_{close}"), + } } /// [`Self::served`] of a C function by name, parameters, and return. @@ -1753,19 +1794,18 @@ impl<'a> Gen<'a> { name: c.clone(), constant: false, }; - let (own, free) = self.owned(&c, &owner); let mut members = vec!["close".to_string()]; let checked = abi.checked[i]; + let Owned { own, freeing, close } = self.owned(&c, &owner, checked); + let close = match checked { + true => format!( + "\t@override\n\tvoid _close() {{\n\t\tfinal s_ = _n.status;\n\t\t{close}(_n.home, _own, this, _raw, s_);\n\t\t_status(s_.value, '{c}_drop');\n\t}}\n" + ), + false => format!("\t@override\n\tvoid _free() => {close}(_n.home, _own, this, _raw);\n"), + }; let mut body = vec![format!( - "\t{name}._(ffi.Pointer raw) : super(raw, true, {checked}) {{\n\t\t_own = {own}(this, raw);\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false, {checked});\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n" + "\t{name}._(ffi.Pointer raw) : super(raw, true, {checked}) {{\n\t\t_n.nursery.push(this, ffi.Native.addressOf)>>({freeing}).cast());\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false, {checked});\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _register() => _own = {own}(this, _raw);\n\n{close}" )]; - if checked { - let what = format!("{c}_drop"); - let f = self.served_fn(&what, vec![owner.clone(), Ty::Out(Box::new(Ty::Status))], Ty::Void); - body.push(format!( - "\t@override\n\tvoid _drop() {{\n\t\tfinal s_ = _n.status;\n\t\t{f}(_n.home, _raw, s_);\n\t\t_status(s_.value, '{what}');\n\t}}\n" - )); - } self.traits(&ty, &c, object.traits, &mut body, &mut members); self.methods(&object.methods, &owner, &ty, &mut body, &mut members); self.distinct(&rust, &members); @@ -2536,6 +2576,16 @@ impl<'a> Gen<'a> { } } +/// The Dart names of what owns an object's handle, as [`Gen::owned`] makes them. +struct Owned { + /// Gives the garbage collector a hold of it. + own: String, + /// The C function freeing it. + freeing: String, + /// Closes it. + close: String, +} + /// How a function throws its error. enum Thrown { Itself, diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index dfc0f5e53..5a4faa9ec 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -9,6 +9,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:ffi' as ffi; import 'dart:io'; +import 'dart:isolate'; import 'dart:typed_data'; import 'package:api/api.dart' as api; @@ -1164,17 +1165,41 @@ Future owned() async { check(api.dropped() == before + 1, 'closing again frees nothing'); closed(() => token.idGet, 'Token', 'a closed token refuses a read'); + // Freed only once later returns push it out of the nursery. unclosed(); + for (var i = 0; i < 3; i++) { + pressure(); + } + check(api.dropped() == before + 1, 'the nursery keeps what nobody closed'); + fill(); await collect(() => api.dropped() >= before + 2, 'the garbage collector frees what nobody closed'); for (var i = 0; i < 3; i++) { pressure(); } check(api.dropped() == before + 2, 'the garbage collector frees each once'); + // Closed once pushed out of the nursery, which gave the garbage collector its hold: the + // close takes it back, so the collector frees nothing more. + final kept = api.Token(4); + fill(); + kept.close(); + check(api.dropped() == before + 3, 'closing an object pushed out of the nursery frees it at once'); + for (var i = 0; i < 3; i++) { + pressure(); + } + await step(); + check(api.dropped() == before + 3, 'the garbage collector frees nothing a close freed'); + + // An isolate's nursery frees what it still holds once the isolate exits. + final exited = api.dropped(); + await Isolate.run(unclosed); + await collect(() => api.dropped() > exited, "an isolate's exit frees what its nursery held"); + // An implementation an object the garbage collector frees held is closed from the event // loop, as no drop in the collector may call into Dart. final log = CountLog(); unclosedLogger(log); + fill(); await collect(() => log.closes == 1, 'the garbage collector frees what an object it frees holds'); targets(); print('owned ok'); @@ -1206,6 +1231,13 @@ void targets() { lacks(() => booth == api.Booth(mark: booth.mark), 'api_Booth_eq'); } +/// Return a nursery's worth of objects, each closed, pushing out every earlier one. +void fill() { + for (var i = 0; i < 64; i++) { + api.Tally().close(); + } +} + @pragma('vm:never-inline') void unclosed() { api.Token(3); @@ -1255,6 +1287,12 @@ void closedObjects() { throws(() => api.logCounts(spared, 256, open, []), 'an integer out of range after an implementation'); closed(() => api.logCounts(spared, 0, c, []), 'Counter', 'a closed object after an implementation'); closed(() => api.logCounts(spared, 0, open, [c]), 'Counter', 'a closed object in a list after an implementation'); + check(spared.closes == 0, 'a call Dart refuses gives Rust no implementation'); + // Moved out by a call, which only Rust's check sees. + final gone = api.Counter('g', 0); + gone.intoLabel(null); + closed(() => api.logCounts(spared, 0, gone, []), 'Counter', 'a moved object after an implementation'); + closed(() => api.logCounts(spared, 0, open, [gone]), 'Counter', 'a moved object in a list after an implementation'); check(spared.closes == 2, 'a call whose check throws drops the implementation it gave Rust'); check(api.onFirstThread(), "a call after one that threw runs on the isolate's thread"); lacks(() => api.wasmLog(spared), 'api_wasm_log'); @@ -1329,6 +1367,7 @@ Future lent() async { pressure(); } canary.clear(); + fill(); await collect(() => api.dropped() > before, 'the garbage collector frees an old object'); check(api.dropped() == before + 1, "a view keeps Rust's buffer"); check(view.length == 10 && view.every((b) => b == 2), "a view reads Rust's buffer after its list is gone"); @@ -1356,6 +1395,7 @@ Future forwarded() async { pressure(); } canary.clear(); + fill(); await collect(() => api.dropped() > before, 'the garbage collector frees an old object'); // Collections enough to free every list the relay left, had one held a copy. for (var i = 0; i < 3; i++) { From 635663ad2e74f82fda10db3dfc2e30ed9ddb6fb1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:51:00 -0800 Subject: [PATCH 07/13] perf(ffi): a Go Close of a checked object no call holds drops and frees in one crossing Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.go | 72 ++++++++++++++++++++++++++++++++++------------- ffi/src/go.rs | 36 +++++++++++++----------- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 56d052edf..437281452 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -101,8 +101,9 @@ type Handle_ struct { state atomic.Int64 // Nil for a handle Rust lends, which it frees itself. free func(unsafe.Pointer) - // Non-nil for an owned one of a value Rust checks. - drop func(unsafe.Pointer) error + // Non-nil for an owned one of a value Rust checks: it drops the value, then frees the + // handle too if asked and the drop passed. + drop func(raw unsafe.Pointer, free bool) error name string // The nursery an owned handle was returned into, and its slot there. nursery *nursery_ @@ -116,7 +117,7 @@ type Handle_ struct { // drops the value of, or nil for one Rust does not check. The generated packages allocate // it with the object owning it, which each call holding it then keeps reachable: so once // the GC collects the object, no call holds the handle, and its cleanup frees it at once. -func (h *Handle_) Init(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) { +func (h *Handle_) Init(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer, bool) error, name string) { h.raw, h.free, h.drop, h.name = raw, free, drop, name h.state.Store(held_) } @@ -150,17 +151,21 @@ type ring_ struct { // dropped its nursery, so it bounds what a dropped object keeps of Rust's. const nurseryLen_ = 64 -// nursery_ is the latest handles returned, oldest at next once full. A slot's raw handle is -// cleared by whichever comes first, of its Close and its eviction, with a compare-and-swap. +// nursery_ is the latest handles returned, oldest at next once full. Whichever comes first +// of a handle's Close and its eviction takes its slot, with a compare-and-swap. type nursery_ struct { // Kept reachable while here, so an eviction can register them. Only the ring's holder // touches these two. held [nurseryLen_]*Handle_ next int - // Each slot's raw handle until closed or evicted, else nil. + // Each slot's raw handle until closed or evicted, evicting_ while an eviction registers + // it, else nil. raw [nurseryLen_]unsafe.Pointer } +// evicting_ marks a slot whose eviction is registering its handle, which no raw handle is. +var evicting_ = unsafe.Pointer(new(byte)) + func (n *nursery_) push(h *Handle_) { i := n.next n.next = (i + 1) % nurseryLen_ @@ -172,18 +177,14 @@ func (n *nursery_) push(h *Handle_) { atomic.StorePointer(&n.raw[i], h.raw) } -// evict registers h, in slot i, with the GC, unless a Close took the slot first. +// evict registers h, in slot i, with the GC, unless a Close took the slot first. A Close +// meanwhile waits until it is registered, so it stops the cleanup. func (n *nursery_) evict(i int, h *Handle_) { - if atomic.LoadPointer(&n.raw[i]) == nil { + if !atomic.CompareAndSwapPointer(&n.raw[i], h.raw, evicting_) { return } - // Registered before the slot is given up, so a Close that finds it given up finds the - // cleanup to stop. - c := runtime.AddCleanup(h, h.free, h.raw) - h.cleanup = c - if !atomic.CompareAndSwapPointer(&n.raw[i], h.raw, nil) { - c.Stop() - } + h.cleanup = runtime.AddCleanup(h, h.free, h.raw) + atomic.StorePointer(&n.raw[i], nil) } // flush registers each handle still open in n, once the pool dropped its ring, so none @@ -191,21 +192,38 @@ func (n *nursery_) evict(i int, h *Handle_) { func (n *nursery_) flush() { for i := range n.raw { // Loaded first, as push stores it after the handle. - if atomic.LoadPointer(&n.raw[i]) != nil { + if raw := atomic.LoadPointer(&n.raw[i]); raw != nil && raw != evicting_ { n.evict(i, n.held[i]) } } } // leave takes h out of its nursery as it closes, before anything frees it, or else stops -// the cleanup its eviction registered. +// the cleanup its eviction registered: once, as only a close claims h. func (h *Handle_) leave() { - if n := h.nursery; n != nil && atomic.CompareAndSwapPointer(&n.raw[h.slot], h.raw, nil) { - return + if n := h.nursery; n != nil { + slot := &n.raw[h.slot] + for { + raw := atomic.LoadPointer(slot) + if raw == h.raw && atomic.CompareAndSwapPointer(slot, raw, nil) { + return + } + if raw != evicting_ && raw != h.raw { + break + } + runtime.Gosched() + } } h.cleanup.Stop() } +// own registers h with the GC, as an eviction does, once a close that took it out of its +// nursery failed, leaving it open. +func (h *Handle_) own() { + h.nursery = nil + h.cleanup = runtime.AddCleanup(h, h.free, h.raw) +} + // Hold is the handle, held until Release takes the Slot_, or nil once closed. func (h *Handle_) Hold() (unsafe.Pointer, Slot_) { if raw := h.Count(); raw != nil { @@ -308,11 +326,25 @@ func (h *Handle_) Unlist(s Slot_) { // Close closes h as Handle_ says; again, it does nothing. func (h *Handle_) Close() error { if h.drop != nil { + // Held by no call, as soon after its return: claimed closed so none starts, then the + // value dropped and the handle freed in one crossing. A call meanwhile fails as + // closed, as one racing any Close may, and a Drop that calls back into Go finds it + // closed rather than waiting on itself. + if h.state.CompareAndSwap(held_, closed_|closing_) { + h.leave() + if err := h.drop(h.raw, true); err != nil { + h.own() + h.state.Store(held_) + return err + } + h.state.Store(closed_) + return nil + } raw, s := h.Hold() if raw == nil { return nil } - err := h.drop(raw) + err := h.drop(raw, false) h.Release(s) if err != nil { return err diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 73a86f747..6f1b8cd44 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -755,6 +755,22 @@ impl<'a> Gen<'a> { name } + /// The C shim dropping the value of the object `c`'s handle, which Rust checks, then + /// freeing the handle if asked and the drop passed: a close in one crossing. + fn close_c(&mut self, c: &str) -> String { + let name = format!("{}GoClose_{}", self.abi.prefix, self.bare(c)); + let handle = Ty::Handle { + name: c.to_string(), + constant: false, + }; + let decl = crate::c::decl(self.abi, &handle, "self"); + let status = crate::c::decl(self.abi, &Ty::Status, ""); + self.decls.push(format!( + "static inline {status} {name}({decl}, int free) {{\n\t{status} r = 0;\n\t{c}_drop(self, &r);\n\tif (r == 0 && free) {{\n\t\t{c}_free(self);\n\t}}\n\treturn r;\n}}\n" + )); + name + } + /// The call of `sig`, which takes a status, with `args`, every argument but the status: /// the statements binding its status to `c_status_`, and the expression of its value, if /// it returns one. @@ -2192,22 +2208,10 @@ impl<'a> Gen<'a> { let (drop, dropping) = match self.abi.checked[i] { true => ( format!( - "// drop_{c} drops the value of the handle p, which Rust checks.\n\ - func drop_{c}(p unsafe.Pointer) error {{\n\treturn {status_}(int32(C.{shim}((*C.{c})(p))), \"{c}_drop\")\n}}\n\n", - shim = self.checked_c( - &format!("{c}_drop"), - &[ - ( - "self".to_string(), - Ty::Handle { - name: c.clone(), - constant: false, - }, - ), - ("status".to_string(), Ty::Out(Box::new(Ty::Status))), - ], - &Ty::Void, - ), + "// drop_{c} drops the value of the handle p, which Rust checks, then frees p if free\n\ + // and the drop passed.\n\ + func drop_{c}(p unsafe.Pointer, free bool) error {{\n\tvar f C.int\n\tif free {{\n\t\tf = 1\n\t}}\n\treturn {status_}(int32(C.{shim}((*C.{c})(p), f)), \"{c}_drop\")\n}}\n\n", + shim = self.close_c(&c), status_ = self.qualify(Packages::RT, "Status_"), ), format!("drop_{c}"), From 29e47edeaf103311b692af8a938597d98925bda4 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:35:13 -0800 Subject: [PATCH 08/13] docs(ffi): the nursery in the Go and Dart object docs and the benchmark; finish the quest Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 11 ++++-- ffi/README.md | 34 ++++++++++++------- quest/m1/ffi-speed/README.md | 2 +- quest/m1/ffi-speed/object-ownership.md | 46 -------------------------- quest/m1/ffi-speed/ready-await.md | 2 +- quest/m2/wasm/backend.md | 4 +-- 6 files changed, 35 insertions(+), 64 deletions(-) delete mode 100644 quest/m1/ffi-speed/object-ownership.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index f486b4f01..95b271251 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -397,8 +397,15 @@ frame path: instructions an object, three quarters of it: without them, a return and close took 62 to 65 ns against 176 to 245. Since kixel-ai/rust2#828, a Go object and its handle are one allocation (70 fewer instructions) and a Dart close drops the value without - an `_Arena` (136 fewer). [Batched ownership](../../quest/m1/ffi-speed/object-ownership.md) - would take the registration off objects closed soon after their return. Kotlin's + an `_Arena` (136 fewer). Since kixel-ai/rust2#838, an object closed within 64 returns + of its own skips the registration: each Go P and each Dart isolate holds its last 64 + returned objects in a nursery, registering only one pushed out unclosed, and a checked + object's close drops and frees in one crossing. Measured 2026-11-15 on **nixos** at a + load average of 4 to 24, 8 rounds alternating with main at `72880f48`, median wall ns: + Go's object return fell from 254 to 174 and Dart's from 225 to 96, Go's ready read from + 833 to 728 and Dart's from 1,020 to 786, and Dart's woken read from 2,930 to 2,274. + What Go has left is mostly its two cgo crossings, the call and the close, and the pool + that finds its P's nursery. Kotlin's 137 ns wall (210 CPU) is a phantom reference linked and unlinked under one global lock. Every returned frame, group, and future pays it. 3. **A woken read.** C's 427 ns is Rust's 329 plus a future's crossings. Kotlin's 1.6 us and diff --git a/ffi/README.md b/ffi/README.md index 7a81c65e6..9c24592f9 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -936,8 +936,13 @@ reads a go.mod. would, and returns an error matching `rt.ErrBorrowed` instead while a call on another goroutine borrows the value, as Rust refuses that drop, leaving the value to a later `Close()`. Of any other type, `Close()` frees the handle, and returns an error matching - `rt.ErrBorrowed` while a call holds it. A second `Close()` does nothing, and one never - closed is freed once the GC collects it, through `runtime.AddCleanup`. A call that meets + `rt.ErrBorrowed` while a call holds it; one no call holds, as soon after its return, + drops and frees in one crossing. A second `Close()` does nothing. A returned object waits + in a nursery of the last 64 its P returned, where `Close()` frees it without the GC's + registration (`runtime.AddCleanup` and its `Stop`), which costs more than the rest of a + return and close. One never closed is registered once pushed out, by 64 later returns or + with its nursery once the GC drops that from its `sync.Pool`, and freed once the GC + collects it after. A call that meets a closed object, as its receiver or anywhere in its arguments, returns an error matching `rt.ErrClosed`, so every such call returns an `error`; `String()` and `GoString()`, which return none, return the error's text. A `self` method, or passing the object by @@ -1071,12 +1076,16 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass declares or a data enum's variant class, are kept apart as Kotlin's are. The glue's C names are `_dart_`, with a `_` more for each C name of the export already there. - **Objects** are final classes owning their handle. Of a checked type, `close()` drops - the value through Rust's own `_drop`, as Rust's `drop` would: a call after it throws a - `StateError` as for a value moved out, and a `close()` while a call borrows the value, - from a closure that call runs, throws one instead, as Rust refuses that drop. Of any - other type, `close()` frees the handle, and throws while a call holds it. A second - `close()` does nothing, and the handle itself is freed once the garbage collector finds - the object unreachable, through a finalizable handle of Dart's C API. Any call that meets + the value through Rust's own `_drop`, as Rust's `drop` would, and frees the handle in + the same crossing, and a `close()` while a call borrows the value, from a closure that + call runs, throws a `StateError` instead, as Rust refuses that drop. Of any other type, + `close()` frees the handle, and throws while a call holds it. A second `close()` does + nothing. A returned object waits in the isolate's nursery of the last 64 returned, where + `close()` frees it without the garbage collector's registration (a finalizable handle of + Dart's C API), which costs more than the rest of a return and close. One never closed is + registered once 64 later returns push it out, and freed once the garbage collector finds + it unreachable after; one still in the nursery when the isolate exits is freed then. Any + call that meets a closed object, as its receiver or anywhere in its arguments, throws a `StateError` before Rust sees it. A `self` method, or passing the object by value, moves its value out. `Clone` is `clone()`, and a `pub` @@ -1225,7 +1234,7 @@ just ffi shapes # round-trip every sampled type shape in every language but KN nix develop .#android --command just ffi android # the Kotlin test, shrunk by R8, on the Android device adb reaches nix develop .#bench --command just ffi bench # the binding benchmark: bench/ffi/README.md's cases from Rust, and through the export from C, C++, Kotlin, Python, Swift, Go, and Dart nix develop .#bench --command just ffi codspeed # after bench-build, each single-threaded case once under CodSpeed's CPU simulation, as the CodSpeed workflow runs it -nix develop .#sanitize --command just ffi sanitize # api's C and C++ scripts under ASan and TSan, and its C ABI under Miri +nix develop .#sanitize --command just ffi sanitize # api's C and C++ scripts under ASan and TSan, its Go under the race detector, and its C ABI under Miri ``` The `api` fixture covers each rule once, and re-exports the `dep` fixture's types, which @@ -1261,7 +1270,8 @@ It runs with nixpkgs' Swift on Linux, and nightly with Apple's on macOS. way, plus a context done before the call, a deadline, and a handle closed during a call, checks that bytes cross both ways, that a `Bytes` keeps Rust's buffer while only a sub-view or a reader reaches it and that the GC frees it once none does, that the GC frees an object -never closed, and that each kind of call meeting a closed object, including one racing +never closed once 64 later returns push it out of its nursery, that each object of many +goroutines' returns, closes, and drops is freed once, and that each kind of call meeting a closed object, including one racing `Close`, returns `rt.ErrClosed`, and breaks each runtime check, which must panic with its message; a runtime package from another build must fail its contract check. The `target` test exports a package for the host, for wasm32, and for both into one @@ -1285,8 +1295,8 @@ feature breaks the API twice for each kind of break, once in an item only wasm32 error matching `fixtures/lock/expected.txt`. `just ffi sanitize`, run nightly, rebuilds the `api` export with std under AddressSanitizer and then ThreadSanitizer, runs `tests/api.c` and `tests/api.cpp` against it, and matches -their output to the native transcript; it then drives the C ABI from `tests/miri/api.rs` -under Miri with strict provenance. +their output to the native transcript; it runs `tests/api.go` under Go's race detector; +and it drives the C ABI from `tests/miri/api.rs` under Miri with strict provenance. moq's crates export through `target/moq-ffi`, an empty library on each that `just ffi moq` writes in place of moq-ffi's remainder. The export serves the host and each target `just ffi moq-api` staged in `target/moq-api` from the same moq sources, and fails on a diff --git a/quest/m1/ffi-speed/README.md b/quest/m1/ffi-speed/README.md index a4773c3bf..3f5d269ce 100644 --- a/quest/m1/ffi-speed/README.md +++ b/quest/m1/ffi-speed/README.md @@ -13,7 +13,7 @@ Nothing here holds [moq's pin](/quest/m1/ffi-export/ship.md) open. Decided with the maintainer on 2026-11-12, in the quest audit: ✅ this epic, beside [the exporter epic](/quest/m1/ffi-export/README.md), which keeps only what breaks the C ABI or a generated API. Kotlin records, -[returned-object ownership](/quest/m1/ffi-speed/object-ownership.md), and the woken read +returned-object ownership, and the woken read moved here from it. Rejected: keeping them there, where three quests that change no API held the pin open. diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md deleted file mode 100644 index f206fcfd1..000000000 --- a/quest/m1/ffi-speed/object-ownership.md +++ /dev/null @@ -1,46 +0,0 @@ -# [L] Returned objects closed early skip the collector in Go and Dart - -## Goal - -A [rust2-ffi](/ffi/README.md) Go or Dart binding returns an object for close to what C -pays when the caller closes it soon after. [The binding benchmark](/bench/ffi/README.md) -measures an object return at 263 ns in Go and 242 in Dart against C's 25, and every -returned frame, group, and future pays it. -Ownership stays automatic: a dropped object is still freed without a call from the user. - -## Plan - -- The profile ([the benchmark's README](/bench/ffi/README.md#what-a-call-spends), item 2) - puts most of the cost in each runtime's per-object registration with its collector: Go's - `runtime.AddCleanup` and the `Cleanup.Stop` its `Close` makes take 39% of a return and - close, and Dart's `Dart_NewFinalizableHandle`, its crossing, and its finalization take - three quarters (1,600 instructions). Without the registration, Dart's return and close - took 62 to 65 ns against 176 to 245. kixel-ai/rust2#828 took what it could without - deferring the registration: one allocation for a Go object and its handle, and no - `_Arena` in a Dart close. -- Rejected, measured: Dart's `NativeFinalizer.attach` without `detach` (770 to 1,070 ns - a return), Go's `runtime.SetFinalizer` (13% fewer instructions to register and clear - than `AddCleanup` and `Stop`, but it runs every free on one goroutine and keeps each - object a collection longer), and leaving Go's cleanup registered at `Close` (290 to 450 - ns a return against 120, in a microbenchmark). -- Settled by the maintainer (2026-11-15, kixel-ai/rust2#828): a nursery of the last 64 - objects returned, which only registers an object - with the collector once it leaves the nursery still open. A `close` of an object in the - nursery frees its handle at once, with a checked value's drop in the same crossing, and - it never registers. Explicit `Close` stays, and the registration stays as the backstop. - - Dart: one ring in the isolate's `_n`, holding its objects, whose oldest is registered - as today when a return pushes it out. A checked object's handle is then freed at - `close`, so its calls check a closed flag in Dart, as a counted one's do (one load a - call). One finalizable handle on the ring frees what it still holds at isolate exit. - - Go: a ring per P, through a `sync.Pool` as `lending_` pools byte batches. Each - object points at its ring and the ring at its objects, so one `AddCleanup` on the ring - frees its open slots once all of them are unreachable, as when the pool drops it. A - compare-and-swap on the object's slot decides between its `Close` and its eviction. - - The later free is accepted: an object dropped without `Close` is freed only after up - to 64 more returns, or in Go once its ring's other objects are unreachable too, - rather than at the next collection. Rejected: keeping today's per-object - registration and deleting this quest; flushing the nursery each event-loop turn (a - timer each turn costs more than it saves when a turn returns one object, as a woken - read does). -- Re-measure the bench's object return and ready read, and check by drop counts in - `ffi/tests` that an object dropped in the nursery is freed once evicted and collected. diff --git a/quest/m1/ffi-speed/ready-await.md b/quest/m1/ffi-speed/ready-await.md index 65f6ec52c..80f64e428 100644 --- a/quest/m1/ffi-speed/ready-await.md +++ b/quest/m1/ffi-speed/ready-await.md @@ -4,7 +4,7 @@ A [rust2-ffi](/ffi/README.md) Swift, Go, or Dart await of a future ready at its first poll costs close to C's, apart from the returned object's ownership, which -[returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) owns. Since +kixel-ai/rust2#838 cut to a nursery of the last 64 returned objects. Since `_start` made a ready call one crossing and one allocation, [the binding benchmark](/bench/ffi/README.md)'s ready read costs C 126 ns over its write, Rust 93, and Swift 289, Go 414, and Dart 522. Less the returned frame's ownership, Swift's await still adds about 150 ns over C's, Go's 100, and Dart's 180. diff --git a/quest/m2/wasm/backend.md b/quest/m2/wasm/backend.md index 7dd4236c7..5bd3c7861 100644 --- a/quest/m2/wasm/backend.md +++ b/quest/m2/wasm/backend.md @@ -42,8 +42,8 @@ measurement on [the benchmark](/quest/m2/wasm/routes.md): - A handle is an integer in an object whose only method is `[Symbol.dispose]`, and a `FinalizationRegistry` frees one never disposed, as the other languages' garbage collectors do. Measure a returned object with and without the registry: the same - pattern is what [object ownership](/quest/m1/ffi-speed/object-ownership.md) found - costing ten times a C return in Go and Dart. + pattern cost ten times a C return in Go and Dart until kixel-ai/rust2#838 registered + only the objects left open past 64 later returns. - A future is a `Promise`: the binding polls once at the call, and an imported `wake` schedules the next poll on a microtask, never inside the wake. Cancelling frees the future and loses nothing. JSPI is not needed, since no Rust code blocks on JS. From 4cdf11798fa9fcf8269907176d9d970075e139df Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:36:23 -0800 Subject: [PATCH 09/13] style(ffi): rustfmt Co-Authored-By: Claude Opus 5.5 --- driver/pub/src/cargo.rs | 3 +-- driver/pub/src/collect.rs | 12 ++++-------- ffi/src/collect.rs | 34 ++++++++++++---------------------- ffi/src/dart.rs | 12 +++++++----- ffi/src/go.rs | 3 +-- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/driver/pub/src/cargo.rs b/driver/pub/src/cargo.rs index 20d4adc3c..5d31aa768 100644 --- a/driver/pub/src/cargo.rs +++ b/driver/pub/src/cargo.rs @@ -150,8 +150,7 @@ impl Workspace { // whose API is the point. let tests = dep.dep_kinds.iter().all(|k| k.kind.as_deref() == Some("dev")); if let Some(to) = dirs.get(&dep.pkg) - && to != from - && !tests + && to != from && !tests { local .get_mut(to) diff --git a/driver/pub/src/collect.rs b/driver/pub/src/collect.rs index f437a12bf..50cf077cf 100644 --- a/driver/pub/src/collect.rs +++ b/driver/pub/src/collect.rs @@ -249,23 +249,20 @@ impl<'tcx> Collector<'tcx> { DefKind::Mod | DefKind::Struct | DefKind::Union - | DefKind::Enum - | DefKind::Variant + | DefKind::Enum | DefKind::Variant | DefKind::Trait | DefKind::TyAlias | DefKind::ForeignTy | DefKind::TraitAlias | DefKind::AssocTy - | DefKind::Fn - | DefKind::Const + | DefKind::Fn | DefKind::Const | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn | DefKind::AssocConst | DefKind::Macro(_) | DefKind::ExternCrate - | DefKind::Use - | DefKind::ForeignMod + | DefKind::Use | DefKind::ForeignMod | DefKind::Impl { .. } | DefKind::Field ) { @@ -285,8 +282,7 @@ impl<'tcx> Collector<'tcx> { .flags .intersects(CodegenFnAttrFlags::USED_COMPILER | CodegenFnAttrFlags::USED_LINKER); if symbol - || used - || matches!(kind, DefKind::Const) && tcx.opt_item_name(def) == Some(kw::Underscore) + || used || matches!(kind, DefKind::Const) && tcx.opt_item_name(def) == Some(kw::Underscore) || proc_macro && exported { roots.push(hash); diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 652e5254c..bbf946d6e 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2426,8 +2426,7 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) - || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) + }) || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if !used { self.api.enums.pop(); @@ -2454,12 +2453,10 @@ impl<'tcx> Collector<'tcx> { let used = api.functions.iter().any(signature) || (api.records.iter().enumerate()).any(|(i, r)| { (!value || i != gone) && (args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) - }) - || (api.enums.iter()).any(|e| { - args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) - || (api.objects.iter().enumerate()) - .any(|(i, o)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) + }) || (api.enums.iter()).any(|e| { + args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) + }) || (api.objects.iter().enumerate()) + .any(|(i, o)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; @@ -3072,8 +3069,7 @@ impl<'tcx> Collector<'tcx> { // is, takes it from the future, as it would a parameter. ty::Ref(_, inner, _) if Some(*inner) == self_ty - && polled.is_some() - && let Some(kind) = self.task(ty, true) => + && polled.is_some() && let Some(kind) = self.task(ty, true) => { tasks.push(Task { at: 0, kind }); None @@ -4203,8 +4199,7 @@ impl<'tcx> Collector<'tcx> { .inputs() .iter() .filter(|&&input| input == param) - .count() - > 1 + .count() > 1 { return Some(Err(why("closure type taken twice", param.to_string()))); } @@ -4558,16 +4553,11 @@ impl<'tcx> Collector<'tcx> { let plain = matches!( ty.kind(), ty::Adt(..) - | ty::Bool - | ty::Char - | ty::Int(_) - | ty::Uint(_) - | ty::Float(_) - | ty::Str - | ty::Array(..) - | ty::Slice(_) - | ty::Ref(..) - | ty::Tuple(_) + | ty::Bool | ty::Char + | ty::Int(_) | ty::Uint(_) + | ty::Float(_) | ty::Str + | ty::Array(..) | ty::Slice(_) + | ty::Ref(..) | ty::Tuple(_) ); let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) { (true, _) => (next, item), diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 459d0f11c..2cd36b6c2 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -660,13 +660,11 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 - | Ty::Duration + | Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) - | Ty::Waker - | Ty::ForeignFuture + | Ty::Waker | Ty::ForeignFuture | Ty::ForeignReader ) } @@ -930,7 +928,11 @@ impl<'a> Gen<'a> { format!( "let dropped = unsafe {{\n\t\thome.serve(move || {{\n\t\t\tcrate::{c}_drop(raw, status);\n\t\t\tlet dropped = *status == 0;\n\t\t\tif dropped {{\n\t\t\t\tcrate::{c}_free(raw);\n\t\t\t}}\n\t\t\tdropped\n\t\t}})\n\t}};\n\tif dropped && !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}" ), - (", status: *mut i32", ", ffi.Pointer", ", ffi.Pointer p4"), + ( + ", status: *mut i32", + ", ffi.Pointer", + ", ffi.Pointer p4", + ), ), false => ( format!("{space}_free_{bare}"), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 6f1b8cd44..2422f099c 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2546,8 +2546,7 @@ impl<'a> Gen<'a> { let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.fails(ty)) || function.params.iter().any(|p| self.fails(&p.ty)) - || status - || !everywhere; + || status || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } From f0b1f081fbcee8835ee63c292158863f9b2b4d43 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:36:54 -0800 Subject: [PATCH 10/13] style(ffi): rustfmt, and restore what another rustfmt touched Co-Authored-By: Claude Opus 5.5 --- driver/pub/src/cargo.rs | 3 ++- driver/pub/src/collect.rs | 12 ++++++++---- ffi/src/collect.rs | 34 ++++++++++++++++++++++------------ ffi/src/dart.rs | 6 ++++-- ffi/src/go.rs | 3 ++- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/driver/pub/src/cargo.rs b/driver/pub/src/cargo.rs index 5d31aa768..20d4adc3c 100644 --- a/driver/pub/src/cargo.rs +++ b/driver/pub/src/cargo.rs @@ -150,7 +150,8 @@ impl Workspace { // whose API is the point. let tests = dep.dep_kinds.iter().all(|k| k.kind.as_deref() == Some("dev")); if let Some(to) = dirs.get(&dep.pkg) - && to != from && !tests + && to != from + && !tests { local .get_mut(to) diff --git a/driver/pub/src/collect.rs b/driver/pub/src/collect.rs index 50cf077cf..f437a12bf 100644 --- a/driver/pub/src/collect.rs +++ b/driver/pub/src/collect.rs @@ -249,20 +249,23 @@ impl<'tcx> Collector<'tcx> { DefKind::Mod | DefKind::Struct | DefKind::Union - | DefKind::Enum | DefKind::Variant + | DefKind::Enum + | DefKind::Variant | DefKind::Trait | DefKind::TyAlias | DefKind::ForeignTy | DefKind::TraitAlias | DefKind::AssocTy - | DefKind::Fn | DefKind::Const + | DefKind::Fn + | DefKind::Const | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn | DefKind::AssocConst | DefKind::Macro(_) | DefKind::ExternCrate - | DefKind::Use | DefKind::ForeignMod + | DefKind::Use + | DefKind::ForeignMod | DefKind::Impl { .. } | DefKind::Field ) { @@ -282,7 +285,8 @@ impl<'tcx> Collector<'tcx> { .flags .intersects(CodegenFnAttrFlags::USED_COMPILER | CodegenFnAttrFlags::USED_LINKER); if symbol - || used || matches!(kind, DefKind::Const) && tcx.opt_item_name(def) == Some(kw::Underscore) + || used + || matches!(kind, DefKind::Const) && tcx.opt_item_name(def) == Some(kw::Underscore) || proc_macro && exported { roots.push(hash); diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index bbf946d6e..652e5254c 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2426,7 +2426,8 @@ impl<'tcx> Collector<'tcx> { || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) + }) + || (api.objects.iter()).any(|o| args(&o.args) || o.methods.iter().any(signature)) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if !used { self.api.enums.pop(); @@ -2453,10 +2454,12 @@ impl<'tcx> Collector<'tcx> { let used = api.functions.iter().any(signature) || (api.records.iter().enumerate()).any(|(i, r)| { (!value || i != gone) && (args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) - }) || (api.enums.iter()).any(|e| { - args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) - }) || (api.objects.iter().enumerate()) - .any(|(i, o)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) + }) + || (api.enums.iter()).any(|e| { + args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) + }) + || (api.objects.iter().enumerate()) + .any(|(i, o)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; @@ -3069,7 +3072,8 @@ impl<'tcx> Collector<'tcx> { // is, takes it from the future, as it would a parameter. ty::Ref(_, inner, _) if Some(*inner) == self_ty - && polled.is_some() && let Some(kind) = self.task(ty, true) => + && polled.is_some() + && let Some(kind) = self.task(ty, true) => { tasks.push(Task { at: 0, kind }); None @@ -4199,7 +4203,8 @@ impl<'tcx> Collector<'tcx> { .inputs() .iter() .filter(|&&input| input == param) - .count() > 1 + .count() + > 1 { return Some(Err(why("closure type taken twice", param.to_string()))); } @@ -4553,11 +4558,16 @@ impl<'tcx> Collector<'tcx> { let plain = matches!( ty.kind(), ty::Adt(..) - | ty::Bool | ty::Char - | ty::Int(_) | ty::Uint(_) - | ty::Float(_) | ty::Str - | ty::Array(..) | ty::Slice(_) - | ty::Ref(..) | ty::Tuple(_) + | ty::Bool + | ty::Char + | ty::Int(_) + | ty::Uint(_) + | ty::Float(_) + | ty::Str + | ty::Array(..) + | ty::Slice(_) + | ty::Ref(..) + | ty::Tuple(_) ); let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) { (true, _) => (next, item), diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 2cd36b6c2..d7e7966da 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -660,11 +660,13 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 | Ty::Duration + | Ty::U128 + | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) - | Ty::Waker | Ty::ForeignFuture + | Ty::Waker + | Ty::ForeignFuture | Ty::ForeignReader ) } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 2422f099c..6f1b8cd44 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2546,7 +2546,8 @@ impl<'a> Gen<'a> { let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.fails(ty)) || function.params.iter().any(|p| self.fails(&p.ty)) - || status || !everywhere; + || status + || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } From 591c7b5ec248863601bbb2d1d044927ecea1388e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:42:57 -0800 Subject: [PATCH 11/13] test(ffi): a Dart u64 since lossless types Co-Authored-By: Claude Opus 5.5 --- ffi/tests/api.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 1c8fe5b0b..065645e27 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -1349,7 +1349,7 @@ void closedObjects() { closed(() => api.logCounts(spared, 0, open, [c]), 'Counter', 'a closed object in a list after an implementation'); check(spared.closes == 0, 'a call Dart refuses gives Rust no implementation'); // Moved out by a call, which only Rust's check sees. - final gone = api.Counter('g', 0); + final gone = api.Counter('g', api.U64(0)); gone.intoLabel(null); closed(() => api.logCounts(spared, 0, gone, []), 'Counter', 'a moved object after an implementation'); closed(() => api.logCounts(spared, 0, open, [gone]), 'Counter', 'a moved object in a list after an implementation'); From 262472fcc12904d836cf3b521edaa765bb3e8e2b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:04:46 -0800 Subject: [PATCH 12/13] fix(ffi): a Dart checked object counts its call holds and is closed before Rust drops it; a Go flush forgets its slots Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/src/lib.rs | 21 ++++++++++ ffi/runtime/rt.dart | 54 ++++++++++++++++---------- ffi/runtime/rt.go | 5 ++- ffi/src/dart.rs | 76 +++++++++++++++++++------------------ ffi/tests/api.dart | 62 ++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 59 deletions(-) diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index eb1215991..31b76386f 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -1794,6 +1794,27 @@ impl Logger { } } +/// A [`Logger`] its `&mut self` method makes checked, so a binding's close drops it through +/// Rust, running its implementation's close inside that drop. +pub struct Journal { + sink: Box, +} + +impl Journal { + pub fn new(sink: Box) -> Self { + Self { sink } + } + + /// Log to `sink` from now on, dropping the implementation before it. + pub fn redirect(&mut self, sink: Box) { + self.sink = sink; + } + + pub fn write(&self, line: &str) -> bool { + self.sink.log(0, line) + } +} + /// A thread logging a line to a foreign implementation, which [`LogThread::join`] waits on. pub struct LogThread { thread: std::thread::JoinHandle, diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 75a112d2f..c18f98b86 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -806,22 +806,23 @@ final class _Callback extends ffi.Struct { external ffi.Pointer data; } -/// What an object owns: its handle, or one Rust lends for a call, which Rust frees. An owned -/// one of a value Rust checks is Rust's alone to account: [close] drops the value as Rust's -/// own drop does and frees the handle, failing while a call borrows it. Any other counts its -/// calls: [close] frees it then, throwing while a call holds it. A call after either throws. -/// One returned waits in the isolate's [_Nursery], where a close frees it without the -/// garbage collector, which frees it only once pushed out unclosed. +/// What an object owns: its handle, or one Rust lends for a call, which Rust frees. Each +/// call holds it, so the handle is freed only once no call does. An owned one of a value +/// Rust checks is Rust's to account: [close] drops the value as Rust's own drop does, +/// failing while a call borrows it, and frees the handle with it unless a call holds it. +/// Any other: [close] frees it then, throwing while a call holds it. A call after either +/// throws. One returned waits in the isolate's [_Nursery], where a close frees it without +/// the garbage collector, which frees it only once pushed out unclosed. abstract base class _Object implements ffi.Finalizable { final ffi.Pointer _raw; - /// The owner's hold until closed, and one for each call, of a counted one. + /// The owner's hold until closed, and one for each call. int _holds = 1; bool _closed = false; final bool _owned; - /// Whether it counts its calls, rather than Rust. - final bool _counted; + /// Whether Rust checks its borrows, so its close drops the value through Rust. + final bool _checked; /// The garbage collector's hold of an owned one's handle, from when the nursery pushed it /// out until freed. @@ -830,29 +831,29 @@ abstract base class _Object implements ffi.Finalizable { /// Its slot in the nursery while there, else -1. int _slot = -1; - _Object(this._raw, bool owned, bool checked) : _owned = owned, _counted = !(owned && checked); + _Object(this._raw, bool owned, bool checked) : _owned = owned, _checked = owned && checked; String get _type; - /// Free a counted one's handle, and take back the garbage collector's hold of it. + /// Free an owned one's handle, and take back the garbage collector's hold of it. void _free() {} /// Give the garbage collector a hold of the handle, as the nursery pushes it out open. void _register() {} - /// Drop the value of one Rust checks, then free its handle and take back the garbage - /// collector's hold of it, in one crossing: throwing [StateError], changing nothing, if a - /// call borrows it. - void _close() {} + /// Drop the value of one Rust checks, then, if `free`, free its handle and take back the + /// garbage collector's hold of it, in one crossing: throwing [StateError], changing + /// nothing, if a call borrows it. + void _close(bool free) {} ffi.Pointer _hold() { if (_closed) throw StateError('$_type is closed'); - if (_counted) _holds++; + _holds++; return _raw; } void _release() { - if (_counted && --_holds == 0 && _owned) { + if (--_holds == 0 && _owned) { _leave(); _free(); } @@ -871,16 +872,27 @@ abstract base class _Object implements ffi.Finalizable { /// Close it as [_Object] says; again, it does nothing. void close() { if (_closed) return; - if (!_counted) { - final nursed = _leave(); + if (_checked) { + // Closed before Rust drops the value, which may run Dart that reaches it meanwhile. + _closed = true; + // A call holding it, from a callback or one waiting on a worker, may not have + // borrowed the value in Rust yet: the handle outlives its hold, so Rust's check + // refuses it. + final alone = _holds == 1; + final nursed = alone && _leave(); try { - _close(); + _close(alone); } catch (_) { + _closed = false; // Still open, and out of the nursery, so the garbage collector's. if (nursed) _register(); rethrow; } - _closed = true; + if (alone) { + _holds = 0; + } else { + _release(); + } return; } if (_holds != 1) throw StateError('$_type: a handle is already borrowed'); diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 437281452..0b8e148ac 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -187,14 +187,15 @@ func (n *nursery_) evict(i int, h *Handle_) { atomic.StorePointer(&n.raw[i], nil) } -// flush registers each handle still open in n, once the pool dropped its ring, so none -// waits on the others to be collected. +// flush registers each handle still open in n, once the pool dropped its ring, and lets go +// of them all, so none waits on the others to be collected while any keeps n. func (n *nursery_) flush() { for i := range n.raw { // Loaded first, as push stores it after the handle. if raw := atomic.LoadPointer(&n.raw[i]); raw != nil && raw != evicting_ { n.evict(i, n.held[i]) } + n.held[i] = nil } } diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index d7e7966da..cb8024306 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -899,9 +899,9 @@ impl<'a> Gen<'a> { /// The Dart functions owning a handle of the object `c`, whose C type is `handle`: one /// giving the garbage collector a Dart object owning it to free it with, the C function - /// freeing it, which the nursery keeps for the isolate's exit, and the one closing it, - /// serving the isolate meanwhile: of a checked value, its drop then its free, else its - /// free alone, each taking back the garbage collector's hold if it took one. + /// freeing it, which the nursery keeps for the isolate's exit, the one freeing it, and of + /// a checked value, the one dropping it then freeing it if asked; the last two serve the + /// isolate meanwhile and take back the garbage collector's hold if it took one. fn owned(&mut self, c: &str, handle: &Ty, checked: bool) -> Owned { let bare = self.bare(c); let space = &self.space; @@ -924,42 +924,38 @@ impl<'a> Gen<'a> { "@ffi.Native)>(symbol: '{c}_free')\nexternal void _{freeing}(ffi.Pointer p0);\n\n" ), ); - let (close, glue, status) = match checked { - true => ( - format!("{space}_close_{bare}"), - format!( - "let dropped = unsafe {{\n\t\thome.serve(move || {{\n\t\t\tcrate::{c}_drop(raw, status);\n\t\t\tlet dropped = *status == 0;\n\t\t\tif dropped {{\n\t\t\t\tcrate::{c}_free(raw);\n\t\t\t}}\n\t\t\tdropped\n\t\t}})\n\t}};\n\tif dropped && !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}" - ), - ( - ", status: *mut i32", - ", ffi.Pointer", - ", ffi.Pointer p4", - ), - ), - false => ( - format!("{space}_free_{bare}"), - format!( - "if !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}\n\tunsafe {{ home.serve(move || crate::{c}_free(raw)) }}" - ), - ("", "", ""), - ), - }; + let free = format!("{space}_free_{bare}"); let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {close}(home: *const c_void, own: *mut c_void, object: *mut c_void, raw: {raw}{}) {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{glue}\n}}\n\n", - status.0 + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {free}(home: *const c_void, own: *mut c_void, object: *mut c_void, raw: {raw}) {{\n\tif !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}\n\tlet home = unsafe {{ &*home.cast::() }};\n\tunsafe {{ home.serve(move || crate::{c}_free(raw)) }}\n}}\n\n" ); self.natives.insert( - close.clone(), + free.clone(), format!( - "@ffi.Native, ffi.Pointer, ffi.Handle, ffi.Pointer{})>(symbol: '{close}')\nexternal void _{close}(ffi.Pointer p0, ffi.Pointer p1, Object p2, ffi.Pointer p3{});\n\n", - status.1, status.2 + "@ffi.Native, ffi.Pointer, ffi.Handle, ffi.Pointer)>(symbol: '{free}')\nexternal void _{free}(ffi.Pointer p0, ffi.Pointer p1, Object p2, ffi.Pointer p3);\n\n" ), ); + let close = checked.then(|| { + let close = format!("{space}_close_{bare}"); + // The status stays the call's own while the isolate serves others meanwhile, as a + // served call's does. + let _ = write!( + self.glue, + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {close}(home: *const c_void, own: *mut c_void, object: *mut c_void, raw: {raw}, free: bool, out: *mut i32) {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\tlet status = unsafe {{\n\t\thome.serve(move || {{\n\t\t\tlet mut status = 0;\n\t\t\tcrate::{c}_drop(raw, &raw mut status);\n\t\t\tif status == 0 && free {{\n\t\t\t\tcrate::{c}_free(raw);\n\t\t\t}}\n\t\t\tstatus\n\t\t}})\n\t}};\n\tif status != 0 {{\n\t\tunsafe {{ *out = status }};\n\t}} else if free && !own.is_null() {{\n\t\tunsafe {{ runtime::disown(own, object) }};\n\t}}\n}}\n\n" + ); + self.natives.insert( + close.clone(), + format!( + "@ffi.Native, ffi.Pointer, ffi.Handle, ffi.Pointer, ffi.Bool, ffi.Pointer)>(symbol: '{close}')\nexternal void _{close}(ffi.Pointer p0, ffi.Pointer p1, Object p2, ffi.Pointer p3, bool p4, ffi.Pointer p5);\n\n" + ), + ); + format!("_{close}") + }); Owned { own: format!("_{own}"), freeing: format!("_{freeing}"), - close: format!("_{close}"), + free: format!("_{free}"), + close, } } @@ -1843,13 +1839,19 @@ impl<'a> Gen<'a> { }; let mut members = vec!["close".to_string()]; let checked = abi.checked[i]; - let Owned { own, freeing, close } = self.owned(&c, &owner, checked); - let close = match checked { - true => format!( - "\t@override\n\tvoid _close() {{\n\t\tfinal s_ = _n.status;\n\t\t{close}(_n.home, _own, this, _raw, s_);\n\t\t_status(s_.value, '{c}_drop');\n\t}}\n" + let Owned { + own, + freeing, + free, + close, + } = self.owned(&c, &owner, checked); + let close = match close { + Some(close) => format!( + "\n\t@override\n\tvoid _close(bool free) {{\n\t\tfinal s_ = _n.status;\n\t\t{close}(_n.home, _own, this, _raw, free, s_);\n\t\t_status(s_.value, '{c}_drop');\n\t}}\n" ), - false => format!("\t@override\n\tvoid _free() => {close}(_n.home, _own, this, _raw);\n"), + None => String::new(), }; + let close = format!("\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n{close}"); let mut body = vec![format!( "\t{name}._(ffi.Pointer raw) : super(raw, true, {checked}) {{\n\t\t_n.nursery.push(this, ffi.Native.addressOf)>>({freeing}).cast());\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false, {checked});\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _register() => _own = {own}(this, _raw);\n\n{close}" )]; @@ -2629,8 +2631,10 @@ struct Owned { own: String, /// The C function freeing it. freeing: String, - /// Closes it. - close: String, + /// Frees it. + free: String, + /// Of a checked value, drops it, then frees it if asked. + close: Option, } /// How a function throws its error. diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 065645e27..90f00a229 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -6,6 +6,7 @@ // contract, which must fail. import 'dart:async'; +import 'dart:collection'; import 'dart:convert'; import 'dart:ffi' as ffi; import 'dart:io'; @@ -997,6 +998,41 @@ final class CountLog extends api.Log { void close() => closes++; } +/// A [CountLog] whose close, which Rust's drop of it runs, then runs `closed`. +final class ClosingLog extends CountLog { + final void Function() closed; + + ClosingLog(this.closed); + + @override + void close() { + super.close(); + closed(); + } +} + +/// A list of objects whose length, read as a call lowers it, first runs `read`. +final class ReadingList extends ListBase { + final void Function() read; + + ReadingList(this.read); + + @override + int get length { + read(); + return 0; + } + + @override + set length(int n) => throw UnsupportedError('a fixed list'); + + @override + api.Counter operator [](int i) => throw RangeError.index(i, this); + + @override + void operator []=(int i, api.Counter value) => throw UnsupportedError('a fixed list'); +} + /// The futures' semantics beyond the transcript, each checked by an assertion. Future futures() async { final channel = api.Channel(); @@ -1360,6 +1396,32 @@ void closedObjects() { check(api.logCounts(spared, 0, open, []) == 1, 'an implementation a call that threw gave none of'); check(spared.lines == 1 && spared.closes == 3, 'an implementation given once is closed once'); + // Closed by Dart a call runs as it lowers its arguments, after it held the object: the + // handle outlives the call's hold, so Rust's check refuses the value the close dropped. + final early = api.Counter('early', api.U64(1)); + closed( + () => api.worth([early], ReadingList(early.close)), + 'Counter', + 'an object closed while its call lowers the rest', + ); + closed(early.get, 'Counter', 'an object closed during a call, after it'); + + // Closed again by Dart its drop runs, which finds it closed, so it is freed once. + late api.Journal journal; + var during = ''; + final nested = ClosingLog(() { + try { + journal.write('x'); + } on StateError catch (e) { + during = e.message; + } + journal.close(); + }); + journal = api.Journal(nested); + journal.close(); + check(nested.closes == 1, 'a close from inside its own drop closes nothing more'); + check(during == 'Journal is closed', 'an object is closed in Dart while Rust drops it: $during'); + // An async call. final channel = api.Channel(); channel.close(); From b7d50fd22efe72c2ae43046c6d5ff80a571c78cb Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:36:25 -0800 Subject: [PATCH 13/13] fix(ffi): Go reads the header's contract through C 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 --- ffi/src/go.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 85491396f..3ec24d173 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -620,6 +620,9 @@ impl<'a> Gen<'a> { extern bool {go}_read(void *data, uint8_t *buf, size_t len, size_t *n, {p}_buf *err);\n\ extern bool {go}_seek(void *data, int whence, int64_t offset, uint64_t *position, {p}_buf *err);\n\n\ static void {go}_bufs_free({p}_buf *bufs, size_t n) {{\n\tfor (size_t i = 0; i < n; i++) {{\n\t\t{p}_buf_free(bufs[i]);\n\t}}\n}}\n\n\ + // The header's contract, read through C: cgo built by clang reads a uint64_t macro\n\ + // with its top bit set as a negative constant.\n\ + static uint64_t {go}_header_contract(void) {{\n\treturn {upper}_CONTRACT;\n}}\n\n\ static void {go}_waker_wake({p}_waker w) {{\n\tif (w.wake != NULL) {{\n\t\tw.wake(w.data);\n\t}}\n}}\n\n\ static void {go}_waker_drop({p}_waker w) {{\n\tif (w.drop != NULL) {{\n\t\tw.drop(w.data);\n\t}}\n}}\n\ */\nimport \"C\"\n\n\ @@ -637,7 +640,7 @@ impl<'a> Gen<'a> { func cWakerDrop(w cWaker) {{\n\tC.{go}_waker_drop(w)\n}}\n\n\ // Go packages and a library from different builds must not mix: checked when the\n\ // runtime loads, before any other call.\n\ - func init() {{\n\tif C.{p}_contract() != 0x{contract:016x} || C.{upper}_CONTRACT != 0x{contract:016x} {{\n\t\tpanic(\"the Go of {p} and the library are from different builds\")\n\t}}\n\ttarget_ = uint64(C.{p}_target())\n}}\n\n\ + func init() {{\n\tif C.{p}_contract() != 0x{contract:016x} || C.{go}_header_contract() != 0x{contract:016x} {{\n\t\tpanic(\"the Go of {p} and the library are from different builds\")\n\t}}\n\ttarget_ = uint64(C.{p}_target())\n}}\n\n\ // target_ is the index of the library's target among those the packages serve.\n\ var target_ uint64\n\n\ // On_ panics unless the library's target is among those of on, as the item what is.\n\