From 2aa6d5d577c5bf5d40d95d6c0aa98154d952b908 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:05:57 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m1/ffi-speed/ready-await.md Quest-Claim: 796be38a62731c36 From a67e110be53924ced7ecb37d67d569d69511f001 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:34:39 -0800 Subject: [PATCH 2/4] perf(ffi): a ready await stays on its caller in Swift, and skips its call state in Dart Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 7 +++--- ffi/runtime/rt.dart | 58 +++++++++++++------------------------------- ffi/runtime/rt.swift | 6 ++--- ffi/src/dart.rs | 14 +++++++---- ffi/src/swift.rs | 8 +++++- ffi/tests/api.swift | 10 ++++++++ 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index b6744c81b..15ce717f4 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -841,8 +841,9 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `SeekFrom::Start` takes a `UInt64`, calls `seek(start:)`, which by default throws. Rust calls it from any thread, one call at a time, and reads a thrown error's `description` as its `io::Error`'s message. -- **Futures** are `async throws` functions, which resume on the task's executor, never - inside a wake. Cancelling the task frees the future, losing nothing, and throws +- **Futures** are `async throws` functions that run on their caller's executor + (`nonisolated(nonsending)`), so a call ready at its first poll never leaves the caller's + actor, and one that suspends resumes there, never inside a wake. Cancelling the task frees the future, losing nothing, and throws `CancellationError`. Each call is its own future, so it has one poller. The call makes and polls it in one crossing, `_start`, before suspending, and makes the signal it parks on only if that poll pends; the signal latches a wake that lands before it parks. @@ -1091,7 +1092,7 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass - **Futures** are `Cancelable`, a `Future` completed on the isolate that made it: an extension type on Dart's own future, which `await` takes its shortest path through. The call makes and polls it in one crossing, `_start`, returning it complete if that - poll finished, and makes a waker only if it pends. A wake, from any + poll finished, and makes the state that polls it again, and a waker, only if it pends. A wake, from any thread, posts to the isolate's port, and its event loop polls again, so a wake never polls inside Rust, and one that lands during a poll is kept. `cancel()` frees the future unless it finished, which cancels it in Rust and loses nothing: the next call diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 0b12fd6ed..c2e71f9a4 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -62,40 +62,33 @@ extension type Cancelable._(Future _future) implements Future { void cancel() => _n.calls[_future]?.cancel(); } -/// The future of `call`, which its start completed if its first poll finished, or else -/// left the future in Rust that pended, polled again here with a waker. +/// The future of `call`, whose start left the future in Rust that pended: polled again +/// here with a waker, which its first poll had no use for. Cancelable _call(_Call call) { - final first = call._first; - if (first != null) return Cancelable._(first); - final done = call._done = dasync.Completer(); - _n.calls[done.future] = call; - // Pending: poll again with a waker, which its first poll had no use for. + final future = call._done.future; + _n.calls[future] = call; call._token = _n.listen(call._wake); call._waker = _n.waker(call._token); call._step(call._waker!); - return Cancelable._(done.future); + return Cancelable._(future); } -/// A [Cancelable]'s future in Rust, until it finishes. +/// A [Cancelable]'s future in Rust, from when its start pended until it finishes. final class _Call { - /// The future in Rust, once its start pended, until it finishes. - ffi.Pointer _f = ffi.nullptr; + /// The future in Rust, until it finishes. + ffi.Pointer _f; final void Function(ffi.Pointer, ffi.Pointer) _free; /// Poll once, completing this if it finished: whether it did. final bool Function(ffi.Pointer, _Waker, _Call) _poll; - /// What its first poll finished with, which no one awaits yet. - Future? _first; - - /// What completes it once its first poll pended. - dasync.Completer? _done; + final _done = dasync.Completer(); _Waker? _waker; int _token = 0; bool _polling = false; bool _cancelled = false; - _Call._(this._free, this._poll); + _Call._(this._free, this._poll, this._f); void _wake() { if (_f != ffi.nullptr) _step(_waker!); @@ -120,33 +113,16 @@ final class _Call { } } - /// Complete it with what Rust returned, from its poll: synchronously at the first, as - /// nothing listens yet. - void _ready(T value) { - final done = _done; - if (done == null) { - _first = (dasync.Completer.sync()..complete(value)).future; - } else { - done.complete(value); - } - } + /// Complete it with what Rust returned, from its poll. + void _ready(T value) => _done.complete(value); - /// Complete it with the error Rust returned, from its poll: never synchronously, since a - /// future that fails with no listener reports its error as uncaught. - void _error(Object error, [StackTrace? stack]) { - final done = _done; - if (done == null) { - _first = Future.error(error, stack); - } else { - done.completeError(error, stack); - } - } + /// Complete it with the error Rust returned, from its poll. + void _error(Object error, [StackTrace? stack]) => _done.completeError(error, stack); void _finish() { _free(_n.home, _f); _f = ffi.nullptr; - final done = _done; - if (done != null) _n.calls.remove(done.future); + _n.calls.remove(_done.future); if (_token != 0) { _n.forget(_token); _token = 0; @@ -156,8 +132,8 @@ final class _Call { void _cancel() { _finish(); // Awaiting it is optional once cancelled. - _done!.future.ignore(); - _done!.completeError(const Cancelled()); + _done.future.ignore(); + _done.completeError(const Cancelled()); } void cancel() { diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index fc14a9461..b1d798ff6 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -993,7 +993,7 @@ extension Native { } /// Suspend until a wake since `reset`, at once if one already landed. - func park() async { + nonisolated(nonsending) func park() async { await withUnsafeContinuation { (continuation: UnsafeContinuation) in parked = continuation if swap(state, Signal.idle, Signal.parked) != Signal.idle { @@ -1011,7 +1011,7 @@ extension Native { /// Await the future `raw` an eager call made, unpolled, as `run` does, but take nothing, /// even what is ready, if the task is cancelled already, and poll first with no waker. - static func made( + nonisolated(nonsending) static func made( _ raw: OpaquePointer?, _ free: @convention(c) (OpaquePointer?) -> Void, _ poll: (OpaquePointer?, CWaker) throws -> T? ) async throws -> T { @@ -1031,7 +1031,7 @@ extension Native { /// Await the future `raw`, whose first poll pended: poll it until `poll` returns its /// output, then free it. A cancelled task frees it unfinished, which cancels it in Rust /// and loses nothing, and throws `CancellationError`. - static func run( + nonisolated(nonsending) static func run( _ raw: OpaquePointer?, _ free: @convention(c) (OpaquePointer?) -> Void, _ poll: (OpaquePointer?, CWaker) throws -> T? ) async throws -> T { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 2a448dc50..ac6c07240 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -2253,14 +2253,18 @@ impl<'a> Gen<'a> { _ => self.lift_outs(&function.ok, &ok), }; let mut cases = vec![format!("\t\tcase 1:\n\t\t\tc._ready({ready});\n\t\t\treturn true;")]; - let mut started = vec![format!("case 1:\n\tc_._ready({ready});")]; + // A start that finished returns a future of its own, complete already if ready: + // only one that pended makes a call to poll it again. + let mut started = vec![format!("case 1:\n\treturn Cancelable._(Future<{ok_dt}>.syncValue({ready}));")]; if let (Some(err), Some(thrown)) = (&err, &thrown) { let lifted = self.lift(err, &self.out_value("err", poll_outs.last().map(|(_, t)| t))); let error = thrown_value(thrown, &lifted); cases.push(format!("\t\tcase 2:\n\t\t\tc._error({error});\n\t\t\treturn true;")); - started.push(format!("case 2:\n\tc_._error({error});")); + // Never complete already: a future that fails with no listener reports its + // error as uncaught. + started.push(format!("case 2:\n\treturn Cancelable._(Future<{ok_dt}>.error({error}));")); } - started.push("default:\n\tc_._f = o_f_.value;".to_string()); + started.push("default:\n\tf_ = o_f_.value;".to_string()); let poll_name = format!("_p_{c_name}"); self.helpers.insert( poll_name.clone(), @@ -2295,12 +2299,12 @@ impl<'a> Gen<'a> { inner.push(check.clone()); } inner.push(format!("switch (started_) {{\n{started}\n}}")); - body.push(format!("final c_ = _Call<{ok_dt}>._({free_fn}, {poll_name});")); + body.push("final ffi.Pointer f_;".to_string()); body.push(format!( "final a_ = _Arena();\ntry {{\n{}\n}} finally {{\n\ta_.end();\n}}", indent(&inner.join("\n")) )); - body.push("return _call(c_);".to_string()); + body.push(format!("return _call(_Call<{ok_dt}>._({free_fn}, {poll_name}, f_));")); } else { ret_type = ok_dt.clone(); let mut inner = pre.clone(); diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index e57b4c3ef..92c5dfff5 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -2059,10 +2059,16 @@ impl<'a> Gen<'a> { member.clone(), ); + // A future runs on its caller's executor, as Swift 6.2's `nonisolated(nonsending)` + // says, so a call ready at its first poll never hops off an actor and back. + let isolation = match function.future { + true => "nonisolated(nonsending) ", + false => "", + }; let head = match var { true => format!("public {stat}var {member}{}", ret.replacen(" -> ", ": ", 1)), false => format!( - "public {stat}{mutation}func {member}({}){effects}{ret}", + "{isolation}public {stat}{mutation}func {member}({}){effects}{ret}", params.join(", ") ), }; diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index b381a21c1..2ac71d209 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -216,6 +216,13 @@ final class Queue: TaskExecutor, @unchecked Sendable { } } +/// An actor awaiting an eager future, whose closure Rust calls before the call returns. +actor Probe { + func count(_ n: UInt32) async throws -> UInt32 { + try await counted(n) { _ in self.preconditionIsolated("a future runs on its caller's actor") } + } +} + /// `body`'s result, waiting on this thread for it to finish on the global executor. func block(_ body: @escaping @Sendable () async throws -> T) -> T { let done = DispatchSemaphore(value: 0) @@ -1156,6 +1163,9 @@ func futures() { check(block { try await halved(4) } == 2, "a future ready with Some returns its value") } + // A future runs on its caller's actor, never hopping off it and back. + check(block { try await Probe().count(2) } == 4, "a future on an actor returns its value") + // Cancelled between its wake and its poll, a receive loses nothing: the payload stays // for the next. do { From 6f24218a0b44cddbd21aa1f070b14c49d364c927 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:54:23 -0800 Subject: [PATCH 3/4] docs(bench): where a ready read's await goes, per language Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 17 +++++++++++++++++ quest/m1/ffi-speed/ready-await.md | 20 -------------------- 2 files changed, 17 insertions(+), 20 deletions(-) delete mode 100644 quest/m1/ffi-speed/ready-await.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index bf791135d..dae7df791 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -398,6 +398,23 @@ frame path: Go 414, Dart 522, Python 445, and Kotlin 590 (from 381, 556, 868, 659, and 615). Go's and Dart's include the returned frame's ownership, about 190 and 215 ns over C's object return; the rest is each language's own await. + + Profiled 2026-11-15 on **nixos** in user-space instructions a read adds to a write, a + million reads alone in each language: C 1,360, Swift 2,248, Go 4,790, and Dart 5,776. + Swift's await spent most of its share in `Task.isCancelled`'s lookup of the current + task, the async frame's allocation, and `swift_task_switch`, the hop a `nonisolated` + async function makes onto the global executor; since kixel-ai/rust2#839 a future is + `nonisolated(nonsending)` and makes no hop (2,104), which on a caller's actor saves two + jobs a read (9.9 us a ready read on the main actor, then 0.32). Dart's start made a call + object and a completer before it knew it was ready; it now returns `Future.syncValue` + (5,298), leaving Dart's own await, which schedules a microtask even for a complete + future. Go's is the returned frame's ownership (`AddCleanup`, `Cleanup.Stop`, and a + close's two crossings), and cgo's check and heap allocation of each out pointer, which + [Go's pointers](../../quest/m1/ffi-speed/go-pointers.md) takes: what remains after both + is one crossing. Off the main thread, glibc's malloc arena for the thread grows and + trims with `mprotect`, which adds about 60 ns to a write and 20 to a read in C on a + pthread (`MALLOC_ARENA_MAX=1` removes it). Swift's ready read runs in a task on such a + thread and its write above on the main one, so its ready row carries it and C's does not. 5. **Kotlin's record codec.** A record return costs Kotlin 113 ns wall against C's 31, through the byte-array codec; Swift, which reads the C struct, costs 37. 6. **The handle borrow at the C ABI.** The u64 getter costs Rust 0.4 ns and C 10: the diff --git a/quest/m1/ffi-speed/ready-await.md b/quest/m1/ffi-speed/ready-await.md deleted file mode 100644 index 65f6ec52c..000000000 --- a/quest/m1/ffi-speed/ready-await.md +++ /dev/null @@ -1,20 +0,0 @@ -# [M] A cheaper ready await in Swift, Go, and Dart - -## 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, 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, 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. From 3d739995978ae335d64fc09303306900150e678f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:54:49 -0800 Subject: [PATCH 4/4] style(ffi): rustfmt Co-Authored-By: Claude Opus 5.5 --- ffi/src/dart.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index ac6c07240..106ba36da 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -2255,14 +2255,18 @@ impl<'a> Gen<'a> { let mut cases = vec![format!("\t\tcase 1:\n\t\t\tc._ready({ready});\n\t\t\treturn true;")]; // A start that finished returns a future of its own, complete already if ready: // only one that pended makes a call to poll it again. - let mut started = vec![format!("case 1:\n\treturn Cancelable._(Future<{ok_dt}>.syncValue({ready}));")]; + let mut started = vec![format!( + "case 1:\n\treturn Cancelable._(Future<{ok_dt}>.syncValue({ready}));" + )]; if let (Some(err), Some(thrown)) = (&err, &thrown) { let lifted = self.lift(err, &self.out_value("err", poll_outs.last().map(|(_, t)| t))); let error = thrown_value(thrown, &lifted); cases.push(format!("\t\tcase 2:\n\t\t\tc._error({error});\n\t\t\treturn true;")); // Never complete already: a future that fails with no listener reports its // error as uncaught. - started.push(format!("case 2:\n\treturn Cancelable._(Future<{ok_dt}>.error({error}));")); + started.push(format!( + "case 2:\n\treturn Cancelable._(Future<{ok_dt}>.error({error}));" + )); } started.push("default:\n\tf_ = o_f_.value;".to_string()); let poll_name = format!("_p_{c_name}");