Skip to content

perf(ffi): a ready await stays on its caller in Swift and skips its call state in Dart - #839

Merged
kixelated merged 4 commits into
mainfrom
quest/m1/ffi-speed/ready-await
Nov 16, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m1/ffi-speed/ready-await

Conversation

@kixelated

@kixelated kixelated commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • Swift: every generated async function is nonisolated(nonsending) (SE-0461, Swift 6.2), as are the runtime's Native.run, Native.made, and Signal.park. A nonisolated async function hops onto the global executor at its entry (SE-0338). That is a swift_task_switch even 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.
  • Dart: a start that finishes returns Future<T>.syncValue(value) (Dart 3.10, which native assets already need), or Future<T>.error(e), straight from the call. The _Call<T> and its Completer are made only once the start pends. _Call loses _first and its two-way _ready and _error.
  • Go: no change. The profile puts all of Go's share in the two parallel quests (below).
  • 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 with mprotect, which adds about 60 ns to a write and 20 to a read in C on a pthread too (MALLOC_ARENA_MAX=1 removes it).
  • The quest file is deleted.

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 with perf record -e instructions:u, against C's loop on a pthread. Instructions a read adds to a write:

C Swift Go Dart
before 1,360 2,248 4,790 5,776
after 1,360 2,104 4,790 5,298
  • Swift's extra, by symbol: swift_task_switch (the entry hop, now gone), Task.isCancelled plus its TLS-descriptor lookup of the current task, the async frame's swift_task_alloc/dealloc, and the returned FrameFrame class.
  • Dart's extra: generic allocations through AllocateObjectParameterized (_Call<T>, _SyncCompleter<T>, _Future<T>, now only _Future<T>), Dart's own await (a closure and a microtask even for a complete future), the arena, and Dart_NewFinalizableHandle with its finalization (ownership's).
  • Go's extra: AddCleanup/addspecial/Cleanup.Stop and a close's two extra crossings (ownership's), and cgoCheckPointer/findObject/inHeapOrStack plus the heap allocations of c_out_, c_err_, and f_ (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 @MainActor cost 9,868 ns a read before and 318 after.

The benchmark, just ffi bench-build programs before (main at 06efe2d) and after, interleaved for 5 rounds, median ns of CPU per frame (wall within 1%):

ready write object
C 261 133 17
Swift before → after 526 → 529 137 → 136 21 → 21
Dart before → after 965 → 904 271 → 274 190 → 179

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.swift gains an actor whose method awaits the eager counted, whose closure (which Rust calls during the call) runs preconditionIsolated. With the generator's nonisolated(nonsending) removed, it fails: Incorrect actor executor assumption; expected 'main.Probe' executor. With it, it passes.
  • Dart's existing futures tests cover ready, error-at-start, pending, and cancel paths through the new _call.
  • The generated Swift typechecks in Swift 5 mode and builds in Swift 6 mode (the fixture's two builds).
  • just ci passed on 3d73999.

Decisions

Taken without the maintainer, since this ran unattended:

  • Swift: 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 (the NonisolatedNonsendingByDefault upcoming feature).
    • Risk: a callback Rust makes during a poll that synchronously waits on the caller's actor (say DispatchQueue.main.sync from a main-actor caller) would now deadlock.
    • Rejected: keeping nonisolated (two jobs a call on any actor).
  • Swift: no @inlinable entry. I measured an @inlinable wrapper over a @usableFromInline sync 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 prelude defers (arena, &mut write-backs, a value receiver's c_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.
  • Dart: return Future.syncValue, not Future.value (which completes in a microtask and costs a closure and a listener) or a Completer.sync (two objects).
  • Go: nothing in this PR, rather than duplicating perf(ffi): Go's calls pass C no pointer to a Go local #837 or perf(ffi): returned objects closed early skip the collector in Go and Dart #838.

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 review after 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 medium

  • Classified: medium by default

  • Reviewer: Codex, gpt-6.1-sol, high effort

  • Session: 01a1e0cd-5a60-7690-a837-10a57b11167d, round 1

  • Score: 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

  • Improvements
    • Swift future calls now stay on the caller’s executor, including when they resume after suspending.
    • Dart future calls now return completed results or errors immediately when available, and set up pending-call handling only when needed.
  • Documentation
    • Added a profile of read-instruction costs across Swift, Dart, Go, and C, including notes on runtime overhead and measurement differences.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Dart async startup now returns completed or error futures when Rust finishes during the initial poll, and uses a completion future for pending calls. Swift future declarations and selected runtime functions now use nonisolated(nonsending). The changes also add an actor-isolation test and update FFI read-cost documentation.

Changes: FFI future behavior

Layer / File(s) Summary
Dart async startup and completion
ffi/src/dart.rs, ffi/runtime/rt.dart, ffi/README.md
Generated Dart methods return completed futures for ready or failed starts. Pending starts use a Rust future handle and a completion future. The runtime registers that future before polling and completes it directly.
Swift caller-executor behavior
ffi/src/swift.rs, ffi/runtime/rt.swift, ffi/tests/api.swift, ffi/README.md
Generated future declarations and selected runtime functions use nonisolated(nonsending). The tests check callback actor isolation and an awaited result of 4. The README describes caller-executor behavior.
Read-cost profile and planning document
bench/ffi/README.md, quest/m1/ffi-speed/ready-await.md
The benchmark README adds a per-language read-cost profile. The ready-await planning document is removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant GeneratedDartMethod
  participant RustFuture
  participant DartCall
  Caller->>GeneratedDartMethod: invoke async method
  GeneratedDartMethod->>RustFuture: start and poll
  alt Rust reports ready or failed
    RustFuture-->>GeneratedDartMethod: ready or failed result
    GeneratedDartMethod-->>Caller: completed or error Future
  else Rust reports pending
    RustFuture-->>GeneratedDartMethod: pending result and future handle
    GeneratedDartMethod->>DartCall: create _Call with future handle
    DartCall-->>Caller: return completion Future
  end
Loading
Merge Risk: Minimal · up to 3d739

This change speeds up ready awaits in Swift and Dart without changing the public API. No concrete merge-blocking risk was identified, and the author reports the CI run as passing.

Security architecture risk: Minimal · up to 3d739

The inspected changes preserve per-call ownership, cancellation, wake delivery and cleanup while avoiding unnecessary execution hops and allocations. No introduced security or failure-containment regression was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed behavior applies to consumers of regenerated async bindings, not merely the measured ready-await call. The inspected scope is in-process execution context and native future lifecycle; the changed declarations and startup branches do not introduce an additional externally callable service interface.

Trust Boundaries and Controls

  • observed — Foreign wakeups remain notifications rather than direct poll execution. Swift uses an atomic signal to latch early wakeups and resume the parked task; its cancellation handler only wakes that signal. Dart routes native wakeups through the isolate port and ignores wake tokens after their listeners are removed.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two changes the PR makes: Swift ready awaits stay on the caller via nonisolated(nonsending), and Dart ready awaits skip creating call state. The raw summary and objectives confirm …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two changes the PR makes: Swift ready awaits stay on the caller via nonisolated(nonsending), and Dart ready awaits skip creating call state. The raw summary and objectives confirm both changes, and the perf(ffi) prefix fits a performance change. The title is specific and accurate, and it does not cover the README, deleted quest file, or benchmark docs, which is acceptable.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac8ceef8-6aa9-40fb-be18-d6c60d551a4b
Commits

Reviewing files that changed from the base of the PR and between 06efe2d and 3d73999.

Files selected for processing: 8

  • bench/ffi/README.md
  • ffi/README.md
  • ffi/runtime/rt.dart
  • ffi/runtime/rt.swift
  • ffi/src/dart.rs
  • ffi/src/swift.rs
  • ffi/tests/api.swift
  • quest/m1/ffi-speed/ready-await.md
Files with no reviewable changes: 1

  • quest/m1/ffi-speed/ready-await.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

kixelated and others added 3 commits November 15, 2026 11:34
…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 kixelated changed the title A cheaper ready await in Swift, Go, and Dart perf(ffi): a ready await stays on its caller in Swift and skips its call state in Dart Nov 15, 2026
@kixelated
kixelated marked this pull request as ready for review November 15, 2026 20:03

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 generated async member and the runtime's Native.run, Native.made, and Signal.park now run on their caller's executor. A ready call never leaves the caller's actor, and the new actor Probe test asserts this with preconditionIsolated. Futures always throw, so they never take the var head, and the prefix lands only on func declarations.
  • 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 returns Future<T>.error(...). _Call and its Completer are built only once the start pends, with _f passed to the constructor and _done no longer nullable. The order inside the arena's try/finally is unchanged, and a ready future is still never registered in _n.calls, so cancel() on it does nothing as before.
  • Docs. bench/ffi/README.md item 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.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@kixelated
kixelated added this pull request to the merge queue Nov 16, 2026
Merged via the queue into main with commit 4a7344a Nov 16, 2026
14 checks passed
@kixelated
kixelated deleted the quest/m1/ffi-speed/ready-await branch November 16, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant