Repository navigation
perf(ffi): a ready await stays on its caller in Swift and skips its call state in Dart - #839
Merged
Merged
Conversation
Quest-Claim: 796be38a62731c36
…call state in Dart Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
marked this pull request as ready for review
November 15, 2026 20:03
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
I reviewed the whole diff: the Swift isolation change, Dart's ready path that skips the call state, and the docs and benchmark notes that go with them.
- Swift futures are
nonisolated(nonsending). Every generatedasyncmember and the runtime'sNative.run,Native.made, andSignal.parknow run on their caller's executor. A ready call never leaves the caller's actor, and the newactor Probetest asserts this withpreconditionIsolated. Futures always throw, so they never take thevarhead, and the prefix lands only onfuncdeclarations. - Dart skips the call state on a finished start. A ready start returns
Future<T>.syncValue(...)(@Since("3.10"), and the binding is tested on Dart 3.13). An error at start returnsFuture<T>.error(...)._Calland itsCompleterare built only once the start pends, with_fpassed to the constructor and_doneno longer nullable. The order inside the arena'stry/finallyis unchanged, and a ready future is still never registered in_n.calls, socancel()on it does nothing as before. - Docs.
bench/ffi/README.mditem 4 now gives the per-language profile and the glibc-arena caveat,ffi/README.md's Futures bullets describe the new behaviour, and the quest file is deleted.
The deliberate trade-off is that Rust's start and every poll now run on the caller's executor (for example the main actor) rather than the global pool. A long synchronous poll can therefore block that actor, and a callback that does DispatchQueue.main.sync would deadlock. The PR's Decisions section already records this, and it matches Swift's NonisolatedNonsendingByDefault direction.
claude-opus-5-5 | 𝕏
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Finishes quest/m1/ffi-speed/ready-await.md: a ready await in Swift, Go, and Dart, profiled from the generated call to the result, with what dominates fixed in the generator and runtime and no API change. The returned object's ownership is out of scope (the object-ownership quest, #838), and so are Go's escaped out-pointers (the go-pointers quest, #837).
Changes
asyncfunction isnonisolated(nonsending)(SE-0461, Swift 6.2), as are the runtime'sNative.run,Native.made, andSignal.park. Anonisolatedasync function hops onto the global executor at its entry (SE-0338). That is aswift_task_switcheven on its fast path, and on a caller's actor it costs two jobs a call: off the actor, then back. Now a future runs on its caller's executor, so a call ready at its first poll never leaves the caller's actor, and one that suspends resumes there.Future<T>.syncValue(value)(Dart 3.10, which native assets already need), orFuture<T>.error(e), straight from the call. The_Call<T>and itsCompleterare made only once the start pends._Callloses_firstand its two-way_readyand_error.bench/ffi/README.md, "What a call spends" item 4: where each language's ready await goes, and a measurement artifact. The bench's Swift ready loop runs in a task on a global-pool thread, but its write row runs on the main thread. glibc's arena for a non-main thread grows and trims withmprotect, which adds about 60 ns to a write and 20 to a read in C on a pthread too (MALLOC_ARENA_MAX=1removes it).Validation
All on nixos (AMD Ryzen 9 5900XT, 32 threads), shared with other agents' builds and benchmarks at a load average of 10 to 17 (one spike to 45, whose runs I discarded).
Profile. I wrote drivers that run only the ready loop (write, then read) or only the write, a million frames each, in C, Swift, Go, and Dart. I counted user-space instructions with
perf stat -e instructions:u, which load doesn't move, and attributed them per symbol withperf record -e instructions:u, against C's loop on a pthread. Instructions a read adds to a write:swift_task_switch(the entry hop, now gone),Task.isCancelledplus its TLS-descriptor lookup of the current task, the async frame'sswift_task_alloc/dealloc, and the returnedFrameFrameclass.AllocateObjectParameterized(_Call<T>,_SyncCompleter<T>,_Future<T>, now only_Future<T>), Dart's ownawait(a closure and a microtask even for a complete future), the arena, andDart_NewFinalizableHandlewith its finalization (ownership's).AddCleanup/addspecial/Cleanup.Stopand a close's two extra crossings (ownership's), andcgoCheckPointer/findObject/inHeapOrStackplus the heap allocations ofc_out_,c_err_, andf_(go-pointers'). What remains for Go once both land is one cgo crossing (entersyscall/exitsyscall) and the handle's hold, nothing await-specific.Wall time, Swift drivers, interleaved, median of 25 rounds: C 134 ns a read, Swift 227 before and 217 after.
Actor callers, interleaved, median of 5: the same Swift ready loop on
@MainActorcost 9,868 ns a read before and 318 after.The benchmark,
just ffi bench-buildprograms before (main at 06efe2d) and after, interleaved for 5 rounds, median ns of CPU per frame (wall within 1%):So Dart's ready read is 61 ns (6%) cheaper. Swift's nonisolated case is within noise, since its fast-path hop was only 144 instructions; the win is on actors.
Tests.
tests/api.swiftgains an actor whose method awaits the eagercounted, whose closure (which Rust calls during the call) runspreconditionIsolated. With the generator'snonisolated(nonsending)removed, it fails:Incorrect actor executor assumption; expected 'main.Probe' executor. With it, it passes._call.just cipassed on 3d73999.Decisions
Taken without the maintainer, since this ran unattended:
nonisolated(nonsending)on every generated future. This is semantics, not API. Call sites compile unchanged (and more of them compile: an actor can now pass its own non-Sendable values). What changes is where the synchronous part runs: the Rust start and each poll run on the caller's executor, not the global pool. This matches Swift's direction (theNonisolatedNonsendingByDefaultupcoming feature).DispatchQueue.main.syncfrom a main-actor caller) would now deadlock.nonisolated(two jobs a call on any actor).@inlinableentry. I measured an@inlinablewrapper over a@usableFromInlinesync start plus a pending-path run: it saves the async frame (146 more instructions, 15 ns, and 78 ns on the main actor). Rejected: a call's preludedefers (arena,&mutwrite-backs, a value receiver'sc_self_, which a pending future points at) span the whole await. Moving the start into a helper changes when they run, so keeping them right needs a second code shape for calls with such a prelude, and 15 ns doesn't pay for that.Future.syncValue, notFuture.value(which completes in a microtask and costs a closure and a listener) or aCompleter.sync(two objects).Public API and wire
None. Generated Swift declarations gain
nonisolated(nonsending)(source-compatible, Swift 6.2 already required). Generated Dart signatures are unchanged.Follow-ups
None.
Models
Size: M
Implementer: Claude Code, claude-opus-5-5, high effort
(Written by Claude Opus 5.5 with high effort)
🤖 Generated with Claude Code
Review
Rewritten by
quest reviewafter each round.Head: 3d73999
Base: origin/main at 72880f4
Risk: medium,
the base has no RISK.md to name what is always high or customer-facing, so every change is mediumClassified: medium by default
Reviewer: Codex, gpt-6.1-sol, high effort
Session:
01a1e0cd-5a60-7690-a837-10a57b11167d, round 1Score: 9/10,
No actionable defects were found; the changes appear consistent with existing completion, cancellation, and executor behavior, but builds and tests were not run as instructed.Rounds: 1; first round 0; findings 0: fixed 0, refuted 0, open 0
Findings: none
Summary by CodeRabbit