From c59dbe245b965f135fbd161edee91b2b664bf0d3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 23:40:37 -0800 Subject: [PATCH 1/7] chore(quest): claim quest/m1/ffi-speed/woken-read.md Quest-Claim: 50d5261dfe157cd7 From 037820450ddc2175ccb125abf2decd67e78f5954 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 00:35:39 -0800 Subject: [PATCH 2/7] perf(ffi): measure long-lived readers across bindings --- bench/ffi/README.md | 40 ++++++++++++++++++++++++++++++++ bench/ffi/report.py | 1 + ffi/moq/bench/export.c | 4 ++-- ffi/moq/bench/export.cpp | 4 ++-- ffi/moq/bench/export.dart | 32 +++++++++++++++++++++++++ ffi/moq/bench/export.go | 30 ++++++++++++++++++++++++ ffi/moq/bench/export.kt | 30 ++++++++++++++++++++++++ ffi/moq/bench/export.py | 23 ++++++++++++++++++ ffi/moq/bench/export.rs | 4 +++- ffi/moq/bench/export.swift | 30 ++++++++++++++++++++++++ quest/m1/ffi-speed/woken-read.md | 22 ++++++++++++------ 11 files changed, 208 insertions(+), 12 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 957020807..6a6365e83 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -27,6 +27,7 @@ the run's summary; nothing fails on a number. | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | | ready read | a 100 B frame written, then read through a new future ready at its first poll | `await noop_async()`, plus a write | | woken read | a read started before its frame is written, as the language starts work concurrently, so it suspends and the write's wake resumes it | `await yield_once()`, plus a write | +| reader loop | one reader and one writer live for all groups, with one permit per frame; includes the permit handoff | none | | payload | `frame.payload` of 100 B, handed to the language | none | | throughput | groups of 100 frames written and read, each payload's first byte touched, about 64 MiB | throughput | @@ -36,6 +37,45 @@ coroutine in Kotlin; and an eager call in Dart. So it measures a task's start an wake, and in Swift, Python, and Go, a read the scheduler had not yet started is ready instead of woken. +The `loop` case removes task creation per frame: one reader loops over all groups and a +single writer waits for a permit before each write. The reader grants the next permit +only after consuming the previous frame, so there is at most one outstanding frame. +The permit's channel or stream handoff is included. In Swift and Go the writer can race +the read's first poll, so this is a streaming measurement, not proof that every read +suspended. C, C++, and Rust already poll in one loop without creating tasks; their +`loop` row repeats their `woken` measurement as the manual-poll floor. + +## Long-lived reader measurement + +Measured 2026-11-13 on fedora, at `a37b3951` plus the reader-loop harness, moq +`ffcb7cd9`. Built with `just ffi bench-build`, rebuilt the changed language harnesses, then ran +`just ffi bench-run 3`, all through `quest gate nix develop .#bench`. Medians below are +microseconds per 100 B frame (write plus read). The writer waits for the reader to +acknowledge consuming the entire group before advancing, so a slow reader cannot lose it. + +| language | task per read, wall / CPU | reader loop, wall / CPU | loop load | +|---|---|---|---| +| rust | 0.38 / 0.38 | 0.38 / 0.38 | 28 | +| c | 0.59 / 0.57 | 0.59 / 0.57 | 28 | +| cpp | 0.64 / 0.58 | 0.64 / 0.58 | 28 | +| kotlin | 2.14 / 2.18 | 1.94 / 1.98 | 31 | +| python | 11.99 / 11.25 | 9.68 / 9.21 | 35 | +| swift | 5.59 / 14.61 | 5.58 / 7.30 | 40 | +| go | 4.91 / 6.10 | 3.71 / 3.62 | 35 | +| dart | 1.96 / 1.94 | 2.11 / 2.10 | 31 | + +The loop reduces Go's wall cost and roughly halves Swift's CPU cost, but Swift's wall +cost remains similar. The loop still costs more than Kotlin's and Dart's on this loaded +host. These results do not establish that task creation alone explains the gap, or that +changing the runtime would close it: the permit handoff is included and the languages +ran sequentially under different loads. Profile the remaining wake and handoff costs +before changing the generated runtimes. The quest stays open. + +The async-start design in [kixel-ai/rust2#741](https://github.com/kixel-ai/rust2/pull/741) +was still unmerged during this run. It removes ready-call crossings; a pending call +still polls again to register its real waker. This harness uses stock public bindings +and will measure that pending path once the design lands. + ## rust2-ffi Measured 2026-11-10 at rust2 `7383790d` plus this harness, moq `ffcb7cd9`, on an Intel diff --git a/bench/ffi/report.py b/bench/ffi/report.py index e40463174..63f0162c4 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -36,6 +36,7 @@ ("stamped", "the same, with a new `Timestamp` each"), ("ready", "a 100 B frame written and read, ready"), ("woken", "the same, read after a wake"), + ("loop", "the same, one reader and writer for all frames"), ("payload", "`frame.payload`, 100 B"), ] diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 01e679f46..bb7d7387f 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -282,8 +282,8 @@ static void round_(int print) { moq_broadcast_Producer_free(broadcast); if (print) { - const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; - span values[] = {call, getter, record, object, stamped, write, ready, woken, payload_}; + const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "loop", "payload"}; + span values[] = {call, getter, record, object, stamped, write, ready, woken, woken, payload_}; for (size_t i = 0; i < sizeof names / sizeof names[0]; i++) { printf("%s\t%f\t%f\n", names[i], values[i].wall, values[i].cpu); } diff --git a/ffi/moq/bench/export.cpp b/ffi/moq/bench/export.cpp index bba2680ee..2de423ebd 100644 --- a/ffi/moq/bench/export.cpp +++ b/ffi/moq/bench/export.cpp @@ -211,8 +211,8 @@ void round(bool print) { sink = sum; if (print) { - const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; - span values[] = {call, getter, record, object, stamped, write, read, woken, payload}; + const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "loop", "payload"}; + span values[] = {call, getter, record, object, stamped, write, read, woken, woken, payload}; for (std::size_t i = 0; i < std::size(names); i++) { std::printf("%s\t%f\t%f\n", names[i], values[i].wall, values[i].cpu); } diff --git a/ffi/moq/bench/export.dart b/ffi/moq/bench/export.dart index 5dd1bb90d..0b9d85185 100644 --- a/ffi/moq/bench/export.dart +++ b/ffi/moq/bench/export.dart @@ -2,6 +2,7 @@ // the Dart wrapper add. Each round runs twice, the first to warm the JIT. Each case prints // its wall time and the process's CPU time, the VM's own threads included. +import 'dart:async'; import 'dart:ffi' as ffi; import 'dart:io' show Platform; import 'dart:typed_data'; @@ -173,6 +174,37 @@ Future> round() async { } out['woken'] = per(start); + // One writer future and one reader; one outstanding permit bounds the stream. + start = now(); + final permits = StreamController(); + Future writeLoop() async { + final next = StreamIterator(permits.stream); + for (var g = 0; g < groups; g++) { + final group = track.appendGroup(); + for (var f = 0; f < frames; f++) { + if (!await next.moveNext()) throw StateError('missing permit'); + group.writeFrame(at, frame); + } + if (!await next.moveNext()) throw StateError('missing group acknowledgement'); + group.finish(); + group.close(); + } + await next.cancel(); + } + final writer = writeLoop(); + for (var g = 0; g < groups; g++) { + final reader = (await subscriber.recvGroup())!; + for (var f = 0; f < frames; f++) { + permits.add(null); + (await reader.readFrame())!.close(); + } + reader.close(); + permits.add(null); + } + await writer; + await permits.close(); + out['loop'] = per(start); + for (final size in sizes) { out['mbps:$size'] = await throughput(track, subscriber, at, size); } diff --git a/ffi/moq/bench/export.go b/ffi/moq/bench/export.go index 02de24577..9990a730d 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -204,6 +204,36 @@ func round() []result { } out = append(out, result{"woken", per(start, n)}) + // One writer and the calling reader live for the whole case. A permit bounds + // the writer to one frame ahead; it can race the reader's first poll. + start = now() + permits := make(chan struct{}) + done := make(chan struct{}) + go func() { + defer close(done) + for range groups { + group := must(track.AppendGroup()) + for range frames { + <-permits + check(group.WriteFrame(at, frame)) + } + <-permits // Keep this group until its reader has consumed every frame. + check(group.Finish()) + group.Close() + } + }() + for range groups { + reader := must(subscriber.RecvGroup(ctx)) + for range frames { + permits <- struct{}{} + must(reader.ReadFrame(ctx)).Close() + } + reader.Close() + permits <- struct{}{} + } + <-done + out = append(out, result{"loop", per(start, n)}) + for _, size := range sizes { out = append(out, result{fmt.Sprintf("mbps:%d", size), throughput(ctx, track, subscriber, at, size)}) } diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 03b2a7d5c..fa1465c4c 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -5,6 +5,8 @@ import java.lang.management.ManagementFactory import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.async +import kotlinx.coroutines.launch +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.runBlocking import moq.* @@ -143,6 +145,34 @@ fun round(): Map { } out["woken"] = per(t0) + // One writer coroutine and one reader, with at most one frame permitted. + t0 = now() + val permits = Channel() + val writer = launch { + repeat(GROUPS) { + track.appendGroup().use { group -> + repeat(FRAMES) { + permits.receive() + group.writeFrame(at, frame) + } + permits.receive() + group.finish() + } + } + } + repeat(GROUPS) { + subscriber.recvGroup()!!.use { reader -> + repeat(FRAMES) { + permits.send(Unit) + reader.readFrame()!!.close() + } + } + permits.send(Unit) + } + writer.join() + permits.close() + out["loop"] = per(t0) + for (size in SIZES) out["mbps:$size"] = throughput(track, subscriber, at, size) subscriber.close() } diff --git a/ffi/moq/bench/export.py b/ffi/moq/bench/export.py index c76bde11b..0605ad1d7 100644 --- a/ffi/moq/bench/export.py +++ b/ffi/moq/bench/export.py @@ -127,6 +127,29 @@ async def reads(): group.finish() out["woken"] = per(start) + # One writer task, with one outstanding permit, for the whole reader loop. + start = now() + permits = asyncio.Queue(maxsize=1) + + async def write_loop(): + for _ in range(GROUPS): + group = track.append_group() + for _ in range(FRAMES): + await permits.get() + group.write_frame(at, frame) + await permits.get() + group.finish() + + writer = asyncio.create_task(write_loop()) + for _ in range(GROUPS): + reader = await subscriber.recv_group() + for _ in range(FRAMES): + permits.put_nowait(None) + await reader.read_frame() + permits.put_nowait(None) + await writer + out["loop"] = per(start) + for size in SIZES: out[f"mbps:{size}"] = await throughput(track, subscriber, at, size) return last diff --git a/ffi/moq/bench/export.rs b/ffi/moq/bench/export.rs index baa1e5d5c..d75b983cb 100644 --- a/ffi/moq/bench/export.rs +++ b/ffi/moq/bench/export.rs @@ -159,7 +159,9 @@ fn round() -> Vec<(String, (f64, f64))> { } group.finish().unwrap(); } - out.push(("woken".into(), per(start, n))); + let woken = per(start, n); + out.push(("woken".into(), woken)); + out.push(("loop".into(), woken)); // The payload as a binding hands it over: a new owner of the frame's bytes. let last = last.unwrap(); diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index e95a8935e..320bd7d61 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -172,6 +172,36 @@ func round() throws -> ([(String, Span)], Int) { } out.append(("woken", per(start))) + // A single writer task serves the calling reader for the whole case. + // Only one permit is outstanding. The write can race the first poll. + start = now() + let (permits, send) = AsyncStream.makeStream() + let writer = Task { + var next = permits.makeAsyncIterator() + for _ in 0.. Date: Fri, 13 Nov 2026 01:32:28 -0800 Subject: [PATCH 3/7] perf(ffi): reduce Go wake waits and prove Swift executor cost --- bench/ffi/README.md | 100 +++++++++++++++++++---------- bench/ffi/report.py | 1 + ffi/moq/bench/export.swift | 61 +++++++++++++++++- ffi/runtime/ffi_cgo.go | 12 +++- quest/m1/ffi-export/async-start.md | 4 -- quest/m1/ffi-speed/README.md | 5 +- quest/m1/ffi-speed/woken-read.md | 25 -------- 7 files changed, 139 insertions(+), 69 deletions(-) delete mode 100644 quest/m1/ffi-speed/woken-read.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 6a6365e83..2d43fcd3a 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -27,7 +27,8 @@ the run's summary; nothing fails on a number. | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | | ready read | a 100 B frame written, then read through a new future ready at its first poll | `await noop_async()`, plus a write | | woken read | a read started before its frame is written, as the language starts work concurrently, so it suspends and the write's wake resumes it | `await yield_once()`, plus a write | -| reader loop | one reader and one writer live for all groups, with one permit per frame; includes the permit handoff | none | +| reader loop | one reader lives for all groups; Swift is drained to suspension on its preferred executor, other managed languages include one permit handoff per frame | none | +| Swift global loop | the same long-lived reader on Swift's global pool, with a permit handoff per frame | none | | payload | `frame.payload` of 100 B, handed to the language | none | | throughput | groups of 100 frames written and read, each payload's first byte touched, about 64 MiB | throughput | @@ -37,44 +38,77 @@ coroutine in Kotlin; and an eager call in Dart. So it measures a task's start an wake, and in Swift, Python, and Go, a read the scheduler had not yet started is ready instead of woken. -The `loop` case removes task creation per frame: one reader loops over all groups and a -single writer waits for a permit before each write. The reader grants the next permit -only after consuming the previous frame, so there is at most one outstanding frame. -The permit's channel or stream handoff is included. In Swift and Go the writer can race -the read's first poll, so this is a streaming measurement, not proof that every read -suspended. C, C++, and Rust already poll in one loop without creating tasks; their -`loop` row repeats their `woken` measurement as the manual-poll floor. +The `loop` case removes task creation per frame. In Go, Kotlin, Python, and Dart one +writer waits for a permit before each write, and for acknowledgement before advancing +groups, so a slow reader cannot lose its group. The channel or stream handoff is included; +Go's writer can race the first poll. Swift uses one task on a preferred executor that +this thread drains until its queue is empty before each write, then drains again until +the frame is consumed. Its per-frame count assertions prove that the read suspended and +that the write woke it. This is the same executor mechanism the Swift correctness tests +use. C, C++, and Rust already manually poll in one loop; `loop` repeats their `woken` floor. + +Swift also keeps `loop-global`: one long-lived reader and writer on the default global +pool, with one permit handoff per frame. The write can race the first poll there. Neither +this case nor the old task-per-read case guarantees every read suspended. ## Long-lived reader measurement -Measured 2026-11-13 on fedora, at `a37b3951` plus the reader-loop harness, moq -`ffcb7cd9`. Built with `just ffi bench-build`, rebuilt the changed language harnesses, then ran -`just ffi bench-run 3`, all through `quest gate nix develop .#bench`. Medians below are -microseconds per 100 B frame (write plus read). The writer waits for the reader to -acknowledge consuming the entire group before advancing, so a slow reader cannot lose it. +Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores, 32 threads), after a +loaded fedora run proved inconclusive. Baseline: rust2 `03782045`, moq `ffcb7cd9`, Swift +6.4.0, Go 1.27.3, Kotlin 2.4.21 and coroutines 1.10.2, Dart 3.13.0. The binaries and their +Nix runtime closures were copied from fedora; builds and measurements on nixos ran +through `quest gate` over SSH. No CPU affinity or scheduler settings were changed. -| language | task per read, wall / CPU | reader loop, wall / CPU | loop load | -|---|---|---|---| -| rust | 0.38 / 0.38 | 0.38 / 0.38 | 28 | -| c | 0.59 / 0.57 | 0.59 / 0.57 | 28 | -| cpp | 0.64 / 0.58 | 0.64 / 0.58 | 28 | -| kotlin | 2.14 / 2.18 | 1.94 / 1.98 | 31 | -| python | 11.99 / 11.25 | 9.68 / 9.21 | 35 | -| swift | 5.59 / 14.61 | 5.58 / 7.30 | 40 | -| go | 4.91 / 6.10 | 3.71 / 3.62 | 35 | -| dart | 1.96 / 1.94 | 2.11 / 2.10 | 31 | - -The loop reduces Go's wall cost and roughly halves Swift's CPU cost, but Swift's wall -cost remains similar. The loop still costs more than Kotlin's and Dart's on this loaded -host. These results do not establish that task creation alone explains the gap, or that -changing the runtime would close it: the permit handoff is included and the languages -ran sequentially under different loads. Profile the remaining wake and handoff costs -before changing the generated runtimes. The quest stays open. +Five rounds, each program warming up once before printing, in microseconds per 100 B +frame (write plus read); CPU includes every process thread: + +| language / executor | task per read, wall / CPU | reader loop, wall / CPU | +|---|---|---| +| Kotlin, runBlocking | 1.88 / 1.94 | 1.80 / 1.83 | +| Dart, isolate | 2.41 / 2.40 | 2.78 / 2.77 | +| Swift, global pool | 5.14 / 18.88 | 4.30 / 9.50 | +| Swift, drained preferred executor | no task per frame | 1.22 / 1.21 | +| Go, before the nil-context wait change | 4.87 / 6.68 | 3.07 / 3.57 | + +The preferred-executor Swift loop was measured separately with the new harness. Its +contemporaneous global-loop median was 4.40 / 9.63 us, consistent with the baseline. +A separate counted run recorded 1,010,000 initial pending polls and 1,010,000 polls that +pended with a real waker: all one million reads plus the 10,000 group receives. The +counted run did not supply timing results. By contrast, the counted global-loop run had +only about 40% of calls pend with a real waker; instrumentation can affect this ratio. +The structural suspension check in the drained loop needs no instrumentation. + +`perf record -F 499 -g` on the original full Swift program attributed 55.5% of sampled +cycles to the dispatch-worker subtree, including 43.4% under its semaphore-wait path. +Kernel addresses were restricted, so kernel function names are unresolved. A separate +language-only control using the same permit stream, an NSLock-protected continuation +mailbox, and no Rust measured 3.78 / 8.87 us (median of five after warmup). This is evidence +that the global executor and the permit handoff dominate that case, not a precise +subtraction of binding costs: the mailbox uses a lock, while the binding uses atomics. +Swift's binding already supports the caller's preferred executor and needs no runtime +change. Forcing a different executor would change caller behavior. + +Go's profile showed scheduling, cgo transitions, and `selectgo`. A context whose `Done` +is nil now waits directly on the wake channel, avoiding a select with a permanently nil +case. Cancellable contexts keep the select and the cancellation check before repolling. +Five alternating before/after runs on nixos, with the same Go compiler and Rust library: + +| Go case | before, wall / CPU | after, wall / CPU | +|---|---|---| +| ready | 0.860 / 0.880 | 0.855 / 0.878 | +| task per read | 4.910 / 6.741 | 4.739 / 6.508 | +| reader loop | 3.096 / 3.616 | 2.914 / 3.397 | + +The loop's wall and CPU medians drop about 6%. Four paired rounds improved; one had a +scheduler outlier in both variants and regressed. Go's result is close to Dart's loop, +while the confirmed-woken Swift loop is below Kotlin's and Dart's. The remaining Swift +global-pool cost is a scheduler cost, not a reason to move Rust polling into a wake +callback or resume the caller on another executor. No performance threshold is a gate. The async-start design in [kixel-ai/rust2#741](https://github.com/kixel-ai/rust2/pull/741) -was still unmerged during this run. It removes ready-call crossings; a pending call -still polls again to register its real waker. This harness uses stock public bindings -and will measure that pending path once the design lands. +was still unmerged during these runs. It removes ready-call crossings; a pending call +still polls again to register its real waker. These changes preserve that design and +use the stock public binding APIs. ## rust2-ffi diff --git a/bench/ffi/report.py b/bench/ffi/report.py index 63f0162c4..c62dd8d57 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -37,6 +37,7 @@ ("ready", "a 100 B frame written and read, ready"), ("woken", "the same, read after a wake"), ("loop", "the same, one reader and writer for all frames"), + ("loop-global", "the Swift reader loop on the global executor"), ("payload", "`frame.payload`, 100 B"), ] diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index 320bd7d61..13fb5dad3 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -50,6 +50,30 @@ func block(_ body: @escaping @Sendable () async throws -> T) -> T return result.value.take()! } +/// A queue of jobs that a test runs when it chooses: the executor its tasks prefer, so a +/// call resumes on it. +final class Queue: TaskExecutor, @unchecked Sendable { + private let lock = NSLock() + private var jobs: [UnownedJob] = [] + + func enqueue(_ job: UnownedJob) { + lock.lock() + defer { lock.unlock() } + jobs.append(job) + } + + func drain() { + while true { + lock.lock() + let job = jobs.isEmpty ? nil : jobs.removeFirst() + lock.unlock() + guard let job else { return } + job.runSynchronously(on: asUnownedTaskExecutor()) + } + } + +} + /// The async cases' spans, the last frame read, and their reads' sum, which a tuple cannot /// hold. struct Reads: ~Copyable { @@ -200,7 +224,7 @@ func round() throws -> ([(String, Span)], Int) { } send.finish() try await writer.value - out.append(("loop", per(start))) + out.append(("loop-global", per(start))) var sum = 0 for size in sizes { @@ -214,6 +238,41 @@ func round() throws -> ([(String, Span)], Int) { sum += reads.sum let last = reads.last + // Drain one reader on its preferred executor until it suspends before every + // write. Unlike a yield on the global pool, an empty queue proves it parked. + let wakeTrack = try broadcast.createTrack("wake-loop", TrackInfo.default().withTimescale(Timescale.MICRO)) + let queue = Queue() + let consumed = Cell() + consumed.value = 0 + let done = Cell() + done.value = false + start = now() + Task(executorPreference: queue) { + var subscriber = try! await wakeTrack.consume().subscribe(nil) + for _ in 0.. Date: Fri, 13 Nov 2026 02:03:24 -0800 Subject: [PATCH 4/7] bench(ffi): force pending Go reads and separate executor diagnostics --- bench/ffi/README.md | 103 ++++++++++++++++++++----------------- bench/ffi/report.py | 4 +- ffi/moq/bench/export.c | 4 +- ffi/moq/bench/export.cpp | 4 +- ffi/moq/bench/export.go | 26 ++++++++-- ffi/moq/bench/export.rs | 4 +- ffi/moq/bench/export.swift | 4 +- ffi/runtime/ffi_cgo.go | 12 ++--- 8 files changed, 89 insertions(+), 72 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 2d43fcd3a..7e2089ea7 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -27,8 +27,8 @@ the run's summary; nothing fails on a number. | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | | ready read | a 100 B frame written, then read through a new future ready at its first poll | `await noop_async()`, plus a write | | woken read | a read started before its frame is written, as the language starts work concurrently, so it suspends and the write's wake resumes it | `await yield_once()`, plus a write | -| reader loop | one reader lives for all groups; Swift is drained to suspension on its preferred executor, other managed languages include one permit handoff per frame | none | -| Swift global loop | the same long-lived reader on Swift's global pool, with a permit handoff per frame | none | +| reader loop | one reader and writer live for all groups, including their handoff; Go waits for a real pending poll before writing | none | +| Swift drained reader | a separate executor diagnostic: one reader drained before each write, without a writer task or permit handoff | none | | payload | `frame.payload` of 100 B, handed to the language | none | | throughput | groups of 100 frames written and read, each payload's first byte touched, about 64 MiB | throughput | @@ -38,18 +38,24 @@ coroutine in Kotlin; and an eager call in Dart. So it measures a task's start an wake, and in Swift, Python, and Go, a read the scheduler had not yet started is ready instead of woken. -The `loop` case removes task creation per frame. In Go, Kotlin, Python, and Dart one -writer waits for a permit before each write, and for acknowledgement before advancing -groups, so a slow reader cannot lose its group. The channel or stream handoff is included; -Go's writer can race the first poll. Swift uses one task on a preferred executor that -this thread drains until its queue is empty before each write, then drains again until -the frame is consumed. Its per-frame count assertions prove that the read suspended and -that the write woke it. This is the same executor mechanism the Swift correctness tests -use. C, C++, and Rust already manually poll in one loop; `loop` repeats their `woken` floor. - -Swift also keeps `loop-global`: one long-lived reader and writer on the default global -pool, with one permit handoff per frame. The write can race the first poll there. Neither -this case nor the old task-per-read case guarantees every read suspended. +The `loop` case removes task creation per frame: one reader loops over all groups and +one writer supplies them. Kotlin, Python, Dart, and Swift include a permit handoff per +frame and an acknowledgement before advancing groups, bounding queued work. Swift runs +on its default global pool. Its writer can race the read's first poll, so this is a +streaming workload, not an isolated measurement of every read suspending. + +Go uses the same pending-context notification as its future correctness tests: `Done` +notifies the writer only after a poll has pended with its real waker registered. It +returns nil because this benchmark never cancels. The writer cannot race that poll; +the final count asserts exactly one pending notification per frame. Go's measured +handoff is thus after registration, rather than before the call as in the other loops. + +Swift also reports `drained`, a separate diagnostic on a preferred executor. This thread +drains one reader to suspension before each write, then drains it until that frame is +consumed, checking the count on both sides. It has no writer task or permit handoff and +must not be ranked against the managed `loop` row. C, C++, and Rust's existing `woken` +cases already manually poll in one loop; they remain the native floor, without printing +the same measurement twice under another name. ## Long-lived reader measurement @@ -67,48 +73,49 @@ frame (write plus read); CPU includes every process thread: | Kotlin, runBlocking | 1.88 / 1.94 | 1.80 / 1.83 | | Dart, isolate | 2.41 / 2.40 | 2.78 / 2.77 | | Swift, global pool | 5.14 / 18.88 | 4.30 / 9.50 | -| Swift, drained preferred executor | no task per frame | 1.22 / 1.21 | -| Go, before the nil-context wait change | 4.87 / 6.68 | 3.07 / 3.57 | - -The preferred-executor Swift loop was measured separately with the new harness. Its -contemporaneous global-loop median was 4.40 / 9.63 us, consistent with the baseline. -A separate counted run recorded 1,010,000 initial pending polls and 1,010,000 polls that -pended with a real waker: all one million reads plus the 10,000 group receives. The -counted run did not supply timing results. By contrast, the counted global-loop run had -only about 40% of calls pend with a real waker; instrumentation can affect this ratio. -The structural suspension check in the drained loop needs no instrumentation. +| Go, pending notification before every write | 5.54 / 7.70 | 3.41 / 4.26 | + +Go's row uses five fresh rounds after replacing the racing permit with the pending +notification. Its `loop` verifies all one million reads actually pended with a real +waker. The other managed rows use the original streaming protocol and include its +handoff; differences in scheduling mean these numbers are workload costs, not a precise +ranking of the bindings' wake primitives. + +Swift's separate `drained` diagnostic measured **1.22 / 1.21 us**, while its +contemporaneous default-global loop measured 4.40 / 9.63 us. A separate instrumented +run recorded 1,010,000 initial pending polls and 1,010,000 polls pending with a real +waker: all one million reads plus 10,000 group receives. Instrumentation supplied no +timing results. The global-loop counted run had only about 40% of calls pend with a real +waker; instrumentation can affect that ratio. The drained loop's structural suspension +check needs no instrumentation. This diagnostic establishes that the binding honors +the preferred executor; it does not establish that Swift is faster than other languages. `perf record -F 499 -g` on the original full Swift program attributed 55.5% of sampled cycles to the dispatch-worker subtree, including 43.4% under its semaphore-wait path. Kernel addresses were restricted, so kernel function names are unresolved. A separate language-only control using the same permit stream, an NSLock-protected continuation -mailbox, and no Rust measured 3.78 / 8.87 us (median of five after warmup). This is evidence -that the global executor and the permit handoff dominate that case, not a precise -subtraction of binding costs: the mailbox uses a lock, while the binding uses atomics. -Swift's binding already supports the caller's preferred executor and needs no runtime -change. Forcing a different executor would change caller behavior. - -Go's profile showed scheduling, cgo transitions, and `selectgo`. A context whose `Done` -is nil now waits directly on the wake channel, avoiding a select with a permanently nil -case. Cancellable contexts keep the select and the cancellation check before repolling. -Five alternating before/after runs on nixos, with the same Go compiler and Rust library: - -| Go case | before, wall / CPU | after, wall / CPU | -|---|---|---| -| ready | 0.860 / 0.880 | 0.855 / 0.878 | -| task per read | 4.910 / 6.741 | 4.739 / 6.508 | -| reader loop | 3.096 / 3.616 | 2.914 / 3.397 | - -The loop's wall and CPU medians drop about 6%. Four paired rounds improved; one had a -scheduler outlier in both variants and regressed. Go's result is close to Dart's loop, -while the confirmed-woken Swift loop is below Kotlin's and Dart's. The remaining Swift -global-pool cost is a scheduler cost, not a reason to move Rust polling into a wake -callback or resume the caller on another executor. No performance threshold is a gate. +mailbox, and no Rust measured 3.78 / 8.87 us (median of five after warmup). This supports +the global executor and permit handoff as the dominant cost. The control's lock differs +from the binding's atomics, so it cannot give a precise subtraction of binding costs. + +Go's profile showed scheduling, cgo transitions, and `selectgo`. An experiment replaced +its nil-`Done` select with a direct receive. On the original racing loop, five +alternating pairs suggested a 6% improvement (3.10 to 2.91 us wall). Repeating five +alternating pairs after forcing every read to pend instead measured **3.41 / 4.26 us +before and 3.84 / 4.93 us after**. The benefit did not survive the stricter workload, +so the experiment was dropped. The generated Go wait remains unchanged. + +The investigation found no supported runtime change that removes Swift's native pool +cost while preserving the caller's scheduling. The default Swift loop remains above +Kotlin's and Dart's. Close the optimization investigation on that explicit limitation, +rather than promise equal absolute costs across different language schedulers. Future +measurements retain both the default workload and the isolated executor diagnostic; +no threshold is an acceptance gate and no runtime speedup is claimed. The async-start design in [kixel-ai/rust2#741](https://github.com/kixel-ai/rust2/pull/741) was still unmerged during these runs. It removes ready-call crossings; a pending call -still polls again to register its real waker. These changes preserve that design and -use the stock public binding APIs. +still polls again to register its real waker. The harness preserves that design and +uses the stock public binding APIs. ## rust2-ffi diff --git a/bench/ffi/report.py b/bench/ffi/report.py index c62dd8d57..172855493 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -36,8 +36,8 @@ ("stamped", "the same, with a new `Timestamp` each"), ("ready", "a 100 B frame written and read, ready"), ("woken", "the same, read after a wake"), - ("loop", "the same, one reader and writer for all frames"), - ("loop-global", "the Swift reader loop on the global executor"), + ("loop", "one managed reader and writer, including their handoff"), + ("drained", "Swift only: one reader on a drained executor, no writer handoff"), ("payload", "`frame.payload`, 100 B"), ] diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index bb7d7387f..01e679f46 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -282,8 +282,8 @@ static void round_(int print) { moq_broadcast_Producer_free(broadcast); if (print) { - const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "loop", "payload"}; - span values[] = {call, getter, record, object, stamped, write, ready, woken, woken, payload_}; + const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; + span values[] = {call, getter, record, object, stamped, write, ready, woken, payload_}; for (size_t i = 0; i < sizeof names / sizeof names[0]; i++) { printf("%s\t%f\t%f\n", names[i], values[i].wall, values[i].cpu); } diff --git a/ffi/moq/bench/export.cpp b/ffi/moq/bench/export.cpp index 2de423ebd..bba2680ee 100644 --- a/ffi/moq/bench/export.cpp +++ b/ffi/moq/bench/export.cpp @@ -211,8 +211,8 @@ void round(bool print) { sink = sum; if (print) { - const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "loop", "payload"}; - span values[] = {call, getter, record, object, stamped, write, read, woken, woken, payload}; + const char *names[] = {"call", "getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; + span values[] = {call, getter, record, object, stamped, write, read, woken, payload}; for (std::size_t i = 0; i < std::size(names); i++) { std::printf("%s\t%f\t%f\n", names[i], values[i].wall, values[i].cpu); } diff --git a/ffi/moq/bench/export.go b/ffi/moq/bench/export.go index 9990a730d..74fc100bb 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -86,6 +86,21 @@ func throughput(ctx context.Context, track *net.TrackProducer, subscriber *net.T return span{1e3 / ns.wall, 1e3 / ns.cpu} } +// pendingContext reports only after the binding polls pending with a real waker, +// like the watched context in the future correctness tests. The writer cannot +// race the initial poll. It is never cancelled, so Done still returns nil. +type pendingContext struct { + context.Context + parked chan struct{} + waits int +} + +func (c *pendingContext) Done() <-chan struct{} { + c.waits++ + c.parked <- struct{}{} + return nil +} + type result struct { name string value span @@ -204,10 +219,11 @@ func round() []result { } out = append(out, result{"woken", per(start, n)}) - // One writer and the calling reader live for the whole case. A permit bounds - // the writer to one frame ahead; it can race the reader's first poll. + // One writer and the calling reader live for the whole case. Each write + // waits until the read has pended with its waker registered. start = now() permits := make(chan struct{}) + pending := &pendingContext{Context: ctx, parked: permits} done := make(chan struct{}) go func() { defer close(done) @@ -225,13 +241,15 @@ func round() []result { for range groups { reader := must(subscriber.RecvGroup(ctx)) for range frames { - permits <- struct{}{} - must(reader.ReadFrame(ctx)).Close() + must(reader.ReadFrame(pending)).Close() } reader.Close() permits <- struct{}{} } <-done + if pending.waits != n { + panic("each loop read must pend exactly once") + } out = append(out, result{"loop", per(start, n)}) for _, size := range sizes { diff --git a/ffi/moq/bench/export.rs b/ffi/moq/bench/export.rs index d75b983cb..baa1e5d5c 100644 --- a/ffi/moq/bench/export.rs +++ b/ffi/moq/bench/export.rs @@ -159,9 +159,7 @@ fn round() -> Vec<(String, (f64, f64))> { } group.finish().unwrap(); } - let woken = per(start, n); - out.push(("woken".into(), woken)); - out.push(("loop".into(), woken)); + out.push(("woken".into(), per(start, n))); // The payload as a binding hands it over: a new owner of the frame's bytes. let last = last.unwrap(); diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index 13fb5dad3..e3e010ba0 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -224,7 +224,7 @@ func round() throws -> ([(String, Span)], Int) { } send.finish() try await writer.value - out.append(("loop-global", per(start))) + out.append(("loop", per(start))) var sum = 0 for size in sizes { @@ -271,7 +271,7 @@ func round() throws -> ([(String, Span)], Int) { try group.finish() } precondition(done.value == true) - out.append(("loop", per(start))) + out.append(("drained", per(start))) // The last frame's 100 bytes, copied into a Data, and one of them read. start = now() diff --git a/ffi/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index 7f2872160..96be64b94 100644 --- a/ffi/runtime/ffi_cgo.go +++ b/ffi/runtime/ffi_cgo.go @@ -386,15 +386,9 @@ func await_(ctx context.Context, poll func(cWaker) (bool, error)) error { if done, err := poll(waker); done { return err } - // A context without cancellation needs only the wake channel, avoiding - // select's registration and lock ordering for a permanently nil case. - if cancelled := ctx.Done(); cancelled == nil { - <-woken - } else { - select { - case <-woken: - case <-cancelled: - } + select { + case <-woken: + case <-ctx.Done(): } // Done between the wake and the poll: the poll would take what the next call sees. if err := ctx.Err(); err != nil { From ac87dc72b23deb759504ef378f3d63b76079edad Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:48:44 -0800 Subject: [PATCH 5/7] docs(ffi): scope Go pending notification to reader loop --- bench/ffi/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 7e2089ea7..b399ea005 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -73,10 +73,11 @@ frame (write plus read); CPU includes every process thread: | Kotlin, runBlocking | 1.88 / 1.94 | 1.80 / 1.83 | | Dart, isolate | 2.41 / 2.40 | 2.78 / 2.77 | | Swift, global pool | 5.14 / 18.88 | 4.30 / 9.50 | -| Go, pending notification before every write | 5.54 / 7.70 | 3.41 / 4.26 | +| Go | 5.54 / 7.70 | 3.41 / 4.26 | -Go's row uses five fresh rounds after replacing the racing permit with the pending -notification. Its `loop` verifies all one million reads actually pended with a real +Go's row uses five fresh rounds. Its task-per-read column uses a goroutine and +`runtime.Gosched()` before each write; only its reader-loop column uses the pending +notification in place of the racing permit. Its `loop` verifies all one million reads actually pended with a real waker. The other managed rows use the original streaming protocol and include its handoff; differences in scheduling mean these numbers are workload costs, not a precise ranking of the bindings' wake primitives. From 00c19ab6e39427df68d2130fc6dd1e88ca521120 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:37:49 -0800 Subject: [PATCH 6/7] docs(ffi): label Go's loop handoff in the report Co-Authored-By: Claude Opus 5.5 --- bench/ffi/report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/ffi/report.py b/bench/ffi/report.py index 172855493..0146ff25d 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -36,7 +36,7 @@ ("stamped", "the same, with a new `Timestamp` each"), ("ready", "a 100 B frame written and read, ready"), ("woken", "the same, read after a wake"), - ("loop", "one managed reader and writer, including their handoff"), + ("loop", "one managed reader and writer, including their handoff; Go writes only after the read pends"), ("drained", "Swift only: one reader on a drained executor, no writer handoff"), ("payload", "`frame.payload`, 100 B"), ] From 9f5e02e191de9458934c529ec8f60b334709d9b5 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:06:29 -0800 Subject: [PATCH 7/7] docs(bench): point the woken read at its long-lived reader measurement Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index e40420aff..54956d529 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -226,8 +226,8 @@ frame path: 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 asyncio's, as BoltFFI's Python measured 10 us. This case starts a task per read; a - reader looping on one task would skip the start, and is what to measure before changing - a generator. + reader looping on one task, which skips the start, is measured in + [Long-lived reader measurement](#long-lived-reader-measurement). 4. **A future per read.** Since `_start` makes a future and polls it in one call, a ready read costs C 126 ns over the write where Rust costs 93: one crossing and one allocation, down from 176 with four allocations and three crossings. Measured 2026-11-13