From b858c20be84953cdf015e0e7d3039bad2fa1be32 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:43:44 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m1/ffi-speed/concurrent-calls.md Quest-Claim: 1ece2a3752ac76d4 From 7325aedc964df70bf9d1133ea4fe288cd692476f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:35:00 -0800 Subject: [PATCH 2/4] feat(bench): time the handle call and getter from 4 threads, shared and own Each language but Dart, whose isolates take no other's object, runs the handle call and the u64 getter on 4 threads at once, on one shared handle and on a clone each. The gap is the binding's checks under contention; on nixos, C's getter costs 102 ns shared against 4.3 own, the shared borrow count's cache line. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 54 ++++++++++++++++ bench/ffi/report.py | 12 ++++ ffi/moq/bench/export.c | 85 +++++++++++++++++++++++++- ffi/moq/bench/export.cpp | 66 +++++++++++++++++++- ffi/moq/bench/export.go | 71 ++++++++++++++++++++- ffi/moq/bench/export.kt | 36 +++++++++++ ffi/moq/bench/export.py | 33 +++++++++- ffi/moq/bench/export.rs | 39 ++++++++++++ ffi/moq/bench/export.swift | 59 ++++++++++++++++++ quest/m1/ffi-speed/README.md | 4 +- quest/m1/ffi-speed/codspeed.md | 5 +- quest/m1/ffi-speed/concurrent-calls.md | 27 -------- quest/m1/ffi-speed/shared-borrow.md | 30 +++++++++ 13 files changed, 479 insertions(+), 42 deletions(-) delete mode 100644 quest/m1/ffi-speed/concurrent-calls.md create mode 100644 quest/m1/ffi-speed/shared-borrow.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 819320db3..75b9c5b76 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -21,6 +21,8 @@ the run's summary; nothing fails on a number. |---|---|---| | handle call | `track.is_closed()`: one borrowed handle, a bool back, and moq's own read lock | `noop()` | | u64 getter | `demand.sequence()`: one borrowed handle and a field read, so all but 0.4 ns is the binding's | `group.sequence()` | +| shared call, own call | the handle call on 4 threads at once, on one handle they share, then on a clone each | none | +| shared getter, own getter | the u64 getter the same way | none | | record return | `track.largest()`: an `Option` of two `u64`s, copied across | `track.info()`, a record | | object return | `track.info()`: a new handle, freed | none | | write | `group.write_frame(at, frame)` with one `Timestamp` handle and 100 B, copied into Rust | `write_frame` | @@ -32,6 +34,16 @@ the run's summary; nothing fails on a number. | 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 | +The four threaded cases start their threads, wait until all are ready, then time each +making a million calls (Python 100,000), as the single-thread rows do. Wall time is per call +on one thread and CPU time per call over all of them, so where the threads never meet, +each equals its single-thread row. A clone is a new handle over the same Rust value, so +both cases share moq's own state and differ only in the binding's: the gap between them is +what a binding's run-time checks cost under contention. The handle call also takes moq's +read lock, which all four threads share in both cases; the getter takes none. C, C++, +Kotlin, Swift, and Python use OS threads, and Go goroutines. Dart has no number: an +isolate cannot receive another's object, so no two threads ever borrow one handle. + A woken read starts its read the language's own way: by polling in C, C++, and Rust; a `Task` and a yield in Swift and Python; a goroutine and a yield in Go; an undispatched coroutine in Kotlin; and an eager call in Dart. So it measures a task's start and its @@ -57,6 +69,48 @@ must not be ranked against the managed `loop` row. C, C++, and Rust's existing ` cases already manually poll in one loop; they remain the native floor, without printing the same measurement twice under another name. +## Concurrent calls + +Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores in two 8-core dies, 32 +threads), at rust2 `b858c20b` plus these cases and moq `ffcb7cd9`, through `quest gate`. +Each cell is the median of 5 rounds at a load average of 14 to 15, beside it. Rust +1.98.1, GCC 16.2.0, CPython 3.14.10 (with its GIL), OpenJDK 21.0.13 with Kotlin 2.4.21, +Swift 6.4, Go 1.27.3. Nanoseconds per call, CPU first, then wall: + +| call | Rust | C | C++ | Kotlin | Python | Swift | Go | +|---|---|---|---|---|---|---|---| +| u64 getter, one thread | 0.4 / 0.4 | 3.6 / 3.6 | 3.7 / 3.7 | 20 / 10 | 70 / 70 | 3.7 / 3.7 | 30 / 30 | +| 4 threads, one shared handle | 0.4 / 0.6 | 102 / 124 | 90 / 101 | 395 / 407 | 1,602 / 2,482 | 102 / 123 | 255 / 262 | +| 4 threads, a handle each | 0.4 / 0.6 | 4.3 / 4.6 | 4.5 / 4.8 | 10 / 11 | 1,539 / 2,417 | 6.2 / 8.1 | 38 / 44 | +| handle call, one thread | 3.4 / 3.4 | 7.0 / 7.0 | 7.2 / 7.3 | 20 / 13 | 75 / 75 | 11 / 11 | 32 / 32 | +| 4 threads, one shared handle | 81 / 92 | 198 / 207 | 245 / 251 | 612 / 573 | 1,767 / 2,859 | 471 / 483 | 382 / 393 | +| 4 threads, a handle each | 82 / 90 | 157 / 169 | 138 / 144 | 208 / 215 | 1,702 / 2,739 | 165 / 176 | 217 / 224 | + +What they show: + +1. **A shared handle's borrow count is one contended cache line.** Every shared borrow at + the C ABI is a compare-and-swap in and a `fetch_sub` out of the handle's `state`, so 4 + threads on one handle bounce its line: C's getter costs 102 ns against 4.3 on a handle + each, near its 3.6 on one thread. A C model of that borrow alone, run on its own after + the benchmark at the same load, cost 68 to 142 ns a borrow on one counter and 4 on a + counter each, so the gap is that line and nothing else of the binding's. +2. **A hold count on the same object adds another.** Kotlin's (395) and Go's (255) shared + getters, and Swift's shared handle call on `TrackProducer`, still a class (471 against + C's 198), pay their own hold count's atomics on the shared object beside the C ABI's. + Swift's getter on a noncopyable `GroupDemand`, which has none, costs what C's does. +3. **moq's read lock is the rest of the handle call.** Rust pays 81 ns on a shared value + and 82 on clones of it: the clones share moq's state, so its lock contends in both, and + no binding's handle call can drop below that. +4. **Python's threads serialize on the GIL.** Its glue releases the GIL around each call + and takes it back after, so with 4 threads each call hands the GIL to another: 1.5 us of + CPU a call either way, 20 times its single thread, and the handle's own contention is + lost in it. + +The load limits these numbers: 4 threads among 14 to 15 others' leave the scheduler free to +place them on one die or across both, whose line transfer is slower, which the model's +twofold spread shows. Read the shared rows as their order of magnitude, against a handle +each, at the same load. + ## Long-lived reader measurement Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores, 32 threads), after a diff --git a/bench/ffi/report.py b/bench/ffi/report.py index 2bcef1fe8..cd2260db1 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -30,6 +30,10 @@ CALLS = [ ("call", "`track.is_closed()`, a handle call"), ("getter", "`demand.sequence()`, a u64 getter"), + ("shared:call", "the handle call on 4 threads, one shared handle"), + ("own:call", "the same, a handle per thread"), + ("shared:getter", "the u64 getter on 4 threads, one shared handle"), + ("own:getter", "the same, a handle per thread"), ("record", "`track.largest()`, a record return"), ("object", "`track.info()`, an object return"), ("write", "`group.write_frame(at, frame)`, 100 B"), @@ -41,6 +45,12 @@ ("payload", "`frame.payload`, 100 B"), ] +# What a language does in place of a case it cannot run, printed in that case's cell. +INSTEAD = { + ("dart", case): "no: an isolate takes no other's object" + for case in ["shared:call", "own:call", "shared:getter", "own:getter"] +} + SIZES = [100, 1024, 16384, 65536, 1048576] # Each binding's generated files, and its Rust glue in the export's crate, under the export. @@ -118,6 +128,8 @@ def main(raw_path, export): langs = [(key, name) for key, name in LANGS if any(lang == key for lang, _ in median)] def cell(lang, case, i): + if (lang, case) in INSTEAD: + return INSTEAD[(lang, case)] if (lang, case) not in median: return "" values = median[(lang, case)] diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 268c54a95..fcd361ebb 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -3,8 +3,10 @@ * bench/export.cpp through the C++ header. Futures are polled by hand with a null waker. * Each case prints its wall time and the process's CPU time. */ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L +#include +#include #include #include #include @@ -16,6 +18,7 @@ #define GROUPS 10000 #define FRAMES 100 #define N (GROUPS * FRAMES) +#define THREADS 4 static const size_t sizes[] = {100, 1024, 16384, 65536, 1048576}; @@ -127,6 +130,64 @@ static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscrib return (span){1e3 / ns.wall, 1e3 / ns.cpu}; } +/* `concurrent`'s start: each thread counts itself ready, then waits for go. macOS has no + * pthread barrier. */ +typedef struct { + atomic_int ready; + atomic_bool go; +} gate; + +/* One of `concurrent`'s threads: N calls on its handle once every thread is ready. */ +typedef struct { + gate *start; + const void *handle; + int getter; + unsigned sum; +} worker; + +static void *work(void *arg) { + worker *w = arg; + unsigned sum = 0; + atomic_fetch_add(&w->start->ready, 1); + while (!atomic_load(&w->start->go)) { + } + if (w->getter) { + for (int i = 0; i < N; i++) { + sum += (unsigned)moq_group_Demand_sequence(w->handle); + } + } else { + for (int i = 0; i < N; i++) { + sum += moq_track_Producer_is_closed(w->handle); + } + } + w->sum = sum; + return NULL; +} + +/* N calls on each of THREADS threads at once, thread i on handles[i]: wall nanoseconds per + * call on one thread, and CPU nanoseconds per call. */ +static span concurrent(const void *const *handles, int getter, unsigned *sum) { + gate start = {0}; + worker workers[THREADS]; + pthread_t threads[THREADS]; + for (int i = 0; i < THREADS; i++) { + workers[i] = (worker){.start = &start, .handle = handles[i], .getter = getter}; + if (pthread_create(&threads[i], NULL, work, &workers[i]) != 0) { + fail("pthread_create"); + } + } + while (atomic_load(&start.ready) < THREADS) { + } + span begin = now(); + atomic_store(&start.go, true); + for (int i = 0; i < THREADS; i++) { + pthread_join(threads[i], NULL); + *sum += workers[i].sum; + } + span end = now(); + return (span){(end.wall - begin.wall) / N, (end.cpu - begin.cpu) / ((double)N * THREADS)}; +} + static void round_(int print) { moq_broadcast_Info *binfo = moq_broadcast_Info_new(); moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo); @@ -161,6 +222,24 @@ static void round_(int print) { sum += (unsigned)moq_group_Demand_sequence(demand); } span getter = per(start, N); + + /* The handle call and the getter from THREADS threads: on one shared handle, then on a + * clone each, a handle of its own to the same value. */ + const void *shared_track[THREADS], *shared_demand[THREADS], *own_track[THREADS], *own_demand[THREADS]; + for (int i = 0; i < THREADS; i++) { + shared_track[i] = track; + shared_demand[i] = demand; + own_track[i] = moq_track_Producer_clone(track); + own_demand[i] = moq_group_Demand_clone(demand); + } + span shared_call = concurrent(shared_track, 0, &sum); + span own_call = concurrent(own_track, 0, &sum); + span shared_getter = concurrent(shared_demand, 1, &sum); + span own_getter = concurrent(own_demand, 1, &sum); + for (int i = 0; i < THREADS; i++) { + moq_track_Producer_free((moq_track_Producer *)own_track[i]); + moq_group_Demand_free((moq_group_Demand *)own_demand[i]); + } moq_group_Demand_free(demand); finish(demanded); @@ -282,8 +361,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", "shared:call", "own:call", "shared:getter", "own:getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; + span values[] = {call, getter, shared_call, own_call, shared_getter, own_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 bba2680ee..17bd93f2c 100644 --- a/ffi/moq/bench/export.cpp +++ b/ffi/moq/bench/export.cpp @@ -3,11 +3,13 @@ // prints its wall time and the process's CPU time. #include +#include #include #include #include #include #include +#include #include #include "moq.hpp" @@ -17,6 +19,7 @@ namespace { constexpr int groups = 10000; constexpr int frames = 100; constexpr int n = groups * frames; +constexpr int threads = 4; constexpr std::size_t sizes[] = {100, 1024, 16384, 65536, 1048576}; // What the loops sum, kept so the compiler cannot drop the calls. @@ -80,6 +83,40 @@ auto ready(F future, const char *what) { return must(must(must(future.poll(noop()), what), what), what); } +// `call` made n times on each of `threads` threads at once, thread i on handles[i]: wall +// nanoseconds per call on one thread, and CPU nanoseconds per call. +template +span concurrent(const T *const (&handles)[threads], F call, unsigned &sum) { + std::atomic ready{0}; + std::atomic go{false}; + unsigned sums[threads] = {}; + std::vector workers; + for (int i = 0; i < threads; i++) { + workers.emplace_back([&, i] { + unsigned local = 0; + ready.fetch_add(1); + while (!go.load()) { + } + for (int j = 0; j < n; j++) { + local += call(*handles[i]); + } + sums[i] = local; + }); + } + while (ready.load() < threads) { + } + span start = now(); + go.store(true); + for (auto &worker : workers) { + worker.join(); + } + span end = now(); + for (unsigned s : sums) { + sum += s; + } + return {(end.wall - start.wall) / n, (end.cpu - start.cpu) / (static_cast(n) * threads)}; +} + // Groups of 100 frames of `size` bytes written and each read back, its payload's first byte // touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, const moq::Timestamp &at, std::size_t size, unsigned &sum) { @@ -115,7 +152,7 @@ void round(bool print) { } span call = per(start); - span getter; + span getter, shared_call, own_call, shared_getter, own_getter; { auto demanded = must(track.append_group(), "append_group"); auto demand = demanded.demand(); @@ -124,6 +161,29 @@ void round(bool print) { sum += static_cast(demand.sequence()); } getter = per(start); + + // The handle call and the getter from `threads` threads: on one shared handle, then + // on a clone each, a handle of its own to the same value. + auto is_closed = [](const moq::track_Producer &t) { return static_cast(t.is_closed()); }; + auto sequence = [](const moq::group_Demand &d) { return static_cast(d.sequence()); }; + std::vector tracks; + std::vector demands; + for (int i = 0; i < threads; i++) { + tracks.push_back(track.clone()); + demands.push_back(demand.clone()); + } + const moq::track_Producer *shared_tracks[threads], *own_tracks[threads]; + const moq::group_Demand *shared_demands[threads], *own_demands[threads]; + for (int i = 0; i < threads; i++) { + shared_tracks[i] = &track; + own_tracks[i] = &tracks[i]; + shared_demands[i] = &demand; + own_demands[i] = &demands[i]; + } + shared_call = concurrent(shared_tracks, is_closed, sum); + own_call = concurrent(own_tracks, is_closed, sum); + shared_getter = concurrent(shared_demands, sequence, sum); + own_getter = concurrent(own_demands, sequence, sum); must(std::move(demanded).finish(), "finish"); } @@ -211,8 +271,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", "shared:call", "own:call", "shared:getter", "own:getter", "record", "object", "stamped", "write", "ready", "woken", "payload"}; + span values[] = {call, getter, shared_call, own_call, shared_getter, own_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 74fc100bb..7b3e42ace 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "runtime" + "sync" "syscall" "time" @@ -14,9 +15,10 @@ import ( ) const ( - groups = 10000 - frames = 100 - n = groups * frames + groups = 10000 + frames = 100 + n = groups * frames + threads = 4 ) var sizes = []int{100, 1024, 16384, 65536, 1048576} @@ -101,6 +103,37 @@ func (c *pendingContext) Done() <-chan struct{} { return nil } +// concurrent makes each of calls n times on a goroutine of its own, all at once: wall +// nanoseconds per call on one goroutine, and CPU nanoseconds per call. +func concurrent(calls []func() int) span { + var ready, done sync.WaitGroup + gate := make(chan struct{}) + sums := make([]int, len(calls)) + for i, call := range calls { + ready.Add(1) + done.Add(1) + go func() { + defer done.Done() + sum := 0 + ready.Done() + <-gate + for range n { + sum += call() + } + sums[i] = sum + }() + } + ready.Wait() + start := now() + close(gate) + done.Wait() + ns := per(start, n) + for _, sum := range sums { + sink += sum + } + return span{ns.wall, ns.cpu / float64(len(calls))} +} + type result struct { name string value span @@ -129,6 +162,38 @@ func round() []result { sink += int(must(demand.Sequence())) } out = append(out, result{"getter", per(start, n)}) + + // The handle call and the getter from threads goroutines: on one shared handle, then on + // a clone each, a handle of its own to the same value. + isClosed := func(t *net.TrackProducer) func() int { + return func() int { + if must(t.IsClosed()) { + return 1 + } + return 0 + } + } + sequence := func(d *net.GroupDemand) func() int { + return func() int { return int(must(d.Sequence())) } + } + var sharedCalls, ownCalls, sharedGetters, ownGetters []func() int + var clones []interface{ Close() } + for range threads { + t := must(track.Clone()) + d := must(demand.Clone()) + clones = append(clones, t, d) + sharedCalls = append(sharedCalls, isClosed(track)) + ownCalls = append(ownCalls, isClosed(t)) + sharedGetters = append(sharedGetters, sequence(demand)) + ownGetters = append(ownGetters, sequence(d)) + } + out = append(out, result{"shared:call", concurrent(sharedCalls)}) + out = append(out, result{"own:call", concurrent(ownCalls)}) + out = append(out, result{"shared:getter", concurrent(sharedGetters)}) + out = append(out, result{"own:getter", concurrent(ownGetters)}) + for _, clone := range clones { + clone.Close() + } demand.Close() check(demanded.Finish()) demanded.Close() diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index fa1465c4c..6b95c8698 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -3,6 +3,7 @@ // its wall time and the process's CPU time, the JIT's and GC's threads included. import java.lang.management.ManagementFactory +import java.util.concurrent.CountDownLatch import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.async import kotlinx.coroutines.launch @@ -13,6 +14,7 @@ import moq.* const val GROUPS = 10000 const val FRAMES = 100 const val N = GROUPS * FRAMES +const val THREADS = 4 val SIZES = listOf(100, 1024, 16384, 65536, 1048576) // Wall and CPU nanoseconds: a point, or a span per call. @@ -31,6 +33,27 @@ fun per(start: Span, n: Double = N.toDouble()): Span { // What the loops sum, kept so the JIT cannot drop the calls. @Volatile var sink = 0 +// `call` made N times on each of THREADS threads at once, thread i on handles[i]: wall +// nanoseconds per call on one thread, and CPU nanoseconds per call. +fun concurrent(handles: List, call: (T) -> Int): Span { + val ready = CountDownLatch(THREADS) + val go = CountDownLatch(1) + val workers = handles.map { handle -> + Thread { + var sum = 0 + ready.countDown() + go.await() + repeat(N) { sum += call(handle) } + sink += sum + }.apply { start() } + } + ready.await() + val start = now() + go.countDown() + workers.forEach { it.join() } + return per(start, N.toDouble()).let { Span(it.wall, it.cpu / THREADS) } +} + // Groups of 100 frames of `size` bytes written and each read back, its payload's first // byte touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. suspend fun throughput(track: TrackProducer, subscriber: TrackSubscriber, at: Timestamp, size: Int): Span { @@ -72,6 +95,19 @@ fun round(): Map { start = now() repeat(N) { sum += demand.sequence().toInt() } out["getter"] = per(start) + + // The handle call and the getter from THREADS threads: on one shared handle, then + // on a clone each, a handle of its own to the same value. + val isClosed = { t: TrackProducer -> if (t.isClosed()) 1 else 0 } + val sequence = { d: GroupDemand -> d.sequence().toInt() } + val tracks = List(THREADS) { track.clone() } + val demands = List(THREADS) { demand.clone() } + out["shared:call"] = concurrent(List(THREADS) { track }, isClosed) + out["own:call"] = concurrent(tracks, isClosed) + out["shared:getter"] = concurrent(List(THREADS) { demand }, sequence) + out["own:getter"] = concurrent(demands, sequence) + tracks.forEach { it.close() } + demands.forEach { it.close() } } group.finish() } diff --git a/ffi/moq/bench/export.py b/ffi/moq/bench/export.py index 0605ad1d7..ee931f808 100644 --- a/ffi/moq/bench/export.py +++ b/ffi/moq/bench/export.py @@ -3,6 +3,7 @@ # Each case prints its wall time and the process's CPU time. import asyncio +import threading import time import moq @@ -10,6 +11,7 @@ GROUPS = 1000 FRAMES = 100 N = GROUPS * FRAMES +THREADS = 4 SIZES = [100, 1024, 16384, 65536, 1048576] @@ -24,6 +26,27 @@ def per(start, n=N): return (wall - start[0]) / n, (cpu - start[1]) / n +def concurrent(calls): + """Each of `calls`, a bound method, made N times on a thread of its own, all at once: + wall nanoseconds per call on one thread, and CPU nanoseconds per call.""" + start = threading.Barrier(len(calls) + 1) + + def work(call): + start.wait() + for _ in range(N): + call() + + threads = [threading.Thread(target=work, args=(call,)) for call in calls] + for thread in threads: + thread.start() + start.wait() + begin = now() + for thread in threads: + thread.join() + wall, cpu = per(begin) + return wall, cpu / len(calls) + + async def throughput(track, subscriber, at, size): """Groups of 100 frames of `size` bytes written and each read back, its payload's first byte touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU @@ -60,11 +83,19 @@ def round(loop): # `group.sequence()`. BoltFFI holds the GIL through the call, where the export releases # it so Rust may call Python back from another thread. demanded = track.append_group() - sequence = demanded.demand().sequence + demand = demanded.demand() + sequence = demand.sequence start = now() for _ in range(N): sequence() out["getter"] = per(start) + + # The handle call and the getter from THREADS threads: on one shared handle, then on a + # clone each, a handle of its own to the same value. + out["shared:call"] = concurrent([track.is_closed] * THREADS) + out["own:call"] = concurrent([track.clone().is_closed for _ in range(THREADS)]) + out["shared:getter"] = concurrent([sequence] * THREADS) + out["own:getter"] = concurrent([demand.clone().sequence for _ in range(THREADS)]) demanded.finish() start = now() diff --git a/ffi/moq/bench/export.rs b/ffi/moq/bench/export.rs index baa1e5d5c..eea188166 100644 --- a/ffi/moq/bench/export.rs +++ b/ffi/moq/bench/export.rs @@ -6,11 +6,13 @@ use std::future::Future; use std::hint::black_box; use std::pin::pin; +use std::sync::Barrier; use std::task::{Context, Poll, Waker}; use std::time::Instant; const GROUPS: u64 = 10000; const FRAMES: u64 = 100; +const THREADS: usize = 4; const SIZES: [usize; 5] = [100, 1024, 16384, 65536, 1048576]; /// When a case started, on the wall clock and in the process's CPU time. @@ -42,6 +44,28 @@ fn per(start: Start, n: u64) -> (f64, f64) { (wall / n as f64, cpu / n as f64) } +/// `call` made `n` times on each of [`THREADS`] threads at once, thread `i` on `handles[i]`: +/// wall nanoseconds per call on one thread, and CPU nanoseconds per call. +fn concurrent(handles: &[&T], n: u64, call: impl Fn(&T) + Sync) -> (f64, f64) { + let start = Barrier::new(THREADS + 1); + // The scope joins every thread before it returns, so the span ends after the last call. + let begin = std::thread::scope(|scope| { + for &handle in handles { + let (start, call) = (&start, &call); + scope.spawn(move || { + start.wait(); + for _ in 0..n { + call(black_box(handle)); + } + }); + } + start.wait(); + begin() + }); + let (wall, cpu) = elapsed(begin); + (wall / n as f64, cpu / (n * THREADS as u64) as f64) +} + /// `future`'s output, which must be ready at its first poll. fn ready(future: F) -> F::Output { match pin!(future).poll(&mut Context::from_waker(Waker::noop())) { @@ -94,6 +118,21 @@ fn round() -> Vec<(String, (f64, f64))> { } out.push(("getter".into(), per(start, n))); + // The handle call and the getter from THREADS threads: on one shared value, then on a + // clone each, as the bindings' own-handle cases do. + let is_closed = |track: &moq::track::Producer| { + black_box(track.is_closed()); + }; + let sequence = |demand: &moq::group::Demand| { + black_box(demand.sequence()); + }; + let tracks: Vec<_> = (0..THREADS).map(|_| track.clone()).collect(); + let demands: Vec<_> = (0..THREADS).map(|_| demand.clone()).collect(); + out.push(("shared:call".into(), concurrent(&[&track; THREADS], n, is_closed))); + out.push(("own:call".into(), concurrent(&tracks.iter().collect::>(), n, is_closed))); + out.push(("shared:getter".into(), concurrent(&[&demand; THREADS], n, sequence))); + out.push(("own:getter".into(), concurrent(&demands.iter().collect::>(), n, sequence))); + let start = begin(); for _ in 0..n { black_box(black_box(&track).largest()); diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index e3e010ba0..d9783fd0b 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -8,6 +8,7 @@ import Moq let groups = 10000 let frames = 100 let n = groups * frames +let threads = 4 let sizes = [100, 1024, 16384, 65536, 1_048_576] /// Wall and CPU nanoseconds: a point, or a span per call. @@ -74,6 +75,54 @@ final class Queue: TaskExecutor, @unchecked Sendable { } +/// A value threads share: a noncopyable object a closure can only borrow through a class. +final class Shared: Sendable { + let value: T + init(_ value: consuming T) { + self.value = value + } +} + +/// Each of `calls` made n times on a thread of its own, all at once: wall nanoseconds per +/// call on one thread, and CPU nanoseconds per call. +func concurrent(_ calls: [@Sendable () -> Int]) -> Span { + let ready = DispatchSemaphore(value: 0) + let go = DispatchSemaphore(value: 0) + let done = DispatchSemaphore(value: 0) + for call in calls { + Thread { + var sum = 0 + ready.signal() + go.wait() + for _ in 0.. ([(String, Span)], Int) { sum &+= Int(demand.sequence()) } out.append(("getter", per(start))) + + // The handle call and the getter from `threads` threads: on one shared handle, then on a + // clone each, a handle of its own to the same value. + let isClosed = { (t: TrackProducer) -> @Sendable () -> Int in { t.isClosed() ? 1 : 0 } } + let sequence = { (d: Shared) -> @Sendable () -> Int in { Int(truncatingIfNeeded: d.value.sequence()) } } + let shared = Shared(demand.clone()) + out.append(("shared:call", concurrent((0.. Date: Fri, 13 Nov 2026 19:10:49 -0800 Subject: [PATCH 3/4] fix(bench): begin each concurrent span before its threads start Rust's and Python's gates released the threads before the span began, so a descheduled main thread could leave calls out of it. Both now wait at a ready gate, then a go gate the main thread opens after it begins. The README's tables are rerun on the fix. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 34 +++++++++++++++++----------------- ffi/moq/bench/export.py | 9 ++++++--- ffi/moq/bench/export.rs | 14 +++++++++----- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 75b9c5b76..4467e01be 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -72,41 +72,41 @@ the same measurement twice under another name. ## Concurrent calls Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores in two 8-core dies, 32 -threads), at rust2 `b858c20b` plus these cases and moq `ffcb7cd9`, through `quest gate`. -Each cell is the median of 5 rounds at a load average of 14 to 15, beside it. Rust +threads), at rust2 `a7db2666` plus these cases and moq `ffcb7cd9`, through `quest gate`. +Each cell is the median of 5 rounds, at a load average of 15 to 17 throughout. Rust 1.98.1, GCC 16.2.0, CPython 3.14.10 (with its GIL), OpenJDK 21.0.13 with Kotlin 2.4.21, Swift 6.4, Go 1.27.3. Nanoseconds per call, CPU first, then wall: | call | Rust | C | C++ | Kotlin | Python | Swift | Go | |---|---|---|---|---|---|---|---| -| u64 getter, one thread | 0.4 / 0.4 | 3.6 / 3.6 | 3.7 / 3.7 | 20 / 10 | 70 / 70 | 3.7 / 3.7 | 30 / 30 | -| 4 threads, one shared handle | 0.4 / 0.6 | 102 / 124 | 90 / 101 | 395 / 407 | 1,602 / 2,482 | 102 / 123 | 255 / 262 | -| 4 threads, a handle each | 0.4 / 0.6 | 4.3 / 4.6 | 4.5 / 4.8 | 10 / 11 | 1,539 / 2,417 | 6.2 / 8.1 | 38 / 44 | -| handle call, one thread | 3.4 / 3.4 | 7.0 / 7.0 | 7.2 / 7.3 | 20 / 13 | 75 / 75 | 11 / 11 | 32 / 32 | -| 4 threads, one shared handle | 81 / 92 | 198 / 207 | 245 / 251 | 612 / 573 | 1,767 / 2,859 | 471 / 483 | 382 / 393 | -| 4 threads, a handle each | 82 / 90 | 157 / 169 | 138 / 144 | 208 / 215 | 1,702 / 2,739 | 165 / 176 | 217 / 224 | +| u64 getter, one thread | 0.4 / 0.4 | 3.7 / 3.7 | 3.6 / 3.6 | 20 / 9.6 | 75 / 75 | 3.7 / 3.7 | 33 / 33 | +| 4 threads, one shared handle | 0.5 / 0.7 | 107 / 129 | 127 / 143 | 378 / 417 | 1,617 / 2,496 | 114 / 131 | 252 / 255 | +| 4 threads, a handle each | 0.6 / 0.7 | 4.1 / 4.6 | 4.2 / 4.2 | 10 / 12 | 1,525 / 2,395 | 5.4 / 6.2 | 34 / 36 | +| handle call, one thread | 3.4 / 3.4 | 7.0 / 7.1 | 7.1 / 7.2 | 20 / 12 | 78 / 78 | 11 / 11 | 35 / 35 | +| 4 threads, one shared handle | 79 / 88 | 242 / 251 | 229 / 237 | 558 / 530 | 1,653 / 2,595 | 405 / 416 | 312 / 319 | +| 4 threads, a handle each | 80 / 89 | 138 / 146 | 149 / 159 | 198 / 201 | 1,722 / 2,755 | 162 / 167 | 201 / 206 | What they show: 1. **A shared handle's borrow count is one contended cache line.** Every shared borrow at the C ABI is a compare-and-swap in and a `fetch_sub` out of the handle's `state`, so 4 - threads on one handle bounce its line: C's getter costs 102 ns against 4.3 on a handle - each, near its 3.6 on one thread. A C model of that borrow alone, run on its own after - the benchmark at the same load, cost 68 to 142 ns a borrow on one counter and 4 on a + threads on one handle bounce its line: C's getter costs 107 ns against 4.1 on a handle + each, near its 3.7 on one thread. A C model of that borrow alone, run on its own after + the benchmark at the same load, cost 73 to 157 ns a borrow on one counter and 4 on a counter each, so the gap is that line and nothing else of the binding's. -2. **A hold count on the same object adds another.** Kotlin's (395) and Go's (255) shared - getters, and Swift's shared handle call on `TrackProducer`, still a class (471 against - C's 198), pay their own hold count's atomics on the shared object beside the C ABI's. +2. **A hold count on the same object adds another.** Kotlin's (378) and Go's (252) shared + getters, and Swift's shared handle call on `TrackProducer`, still a class (405 against + C's 242), pay their own hold count's atomics on the shared object beside the C ABI's. Swift's getter on a noncopyable `GroupDemand`, which has none, costs what C's does. -3. **moq's read lock is the rest of the handle call.** Rust pays 81 ns on a shared value - and 82 on clones of it: the clones share moq's state, so its lock contends in both, and +3. **moq's read lock is the rest of the handle call.** Rust pays 79 ns on a shared value + and 80 on clones of it: the clones share moq's state, so its lock contends in both, and no binding's handle call can drop below that. 4. **Python's threads serialize on the GIL.** Its glue releases the GIL around each call and takes it back after, so with 4 threads each call hands the GIL to another: 1.5 us of CPU a call either way, 20 times its single thread, and the handle's own contention is lost in it. -The load limits these numbers: 4 threads among 14 to 15 others' leave the scheduler free to +The load limits these numbers: 4 threads among 15 to 17 others' leave the scheduler free to place them on one die or across both, whose line transfer is slower, which the model's twofold spread shows. Read the shared rows as their order of magnitude, against a handle each, at the same load. diff --git a/ffi/moq/bench/export.py b/ffi/moq/bench/export.py index ee931f808..e2ea73344 100644 --- a/ffi/moq/bench/export.py +++ b/ffi/moq/bench/export.py @@ -29,18 +29,21 @@ def per(start, n=N): def concurrent(calls): """Each of `calls`, a bound method, made N times on a thread of its own, all at once: wall nanoseconds per call on one thread, and CPU nanoseconds per call.""" - start = threading.Barrier(len(calls) + 1) + ready = threading.Barrier(len(calls) + 1) + go = threading.Event() def work(call): - start.wait() + ready.wait() + go.wait() for _ in range(N): call() threads = [threading.Thread(target=work, args=(call,)) for call in calls] for thread in threads: thread.start() - start.wait() + ready.wait() begin = now() + go.set() for thread in threads: thread.join() wall, cpu = per(begin) diff --git a/ffi/moq/bench/export.rs b/ffi/moq/bench/export.rs index eea188166..22104e6a2 100644 --- a/ffi/moq/bench/export.rs +++ b/ffi/moq/bench/export.rs @@ -47,20 +47,24 @@ fn per(start: Start, n: u64) -> (f64, f64) { /// `call` made `n` times on each of [`THREADS`] threads at once, thread `i` on `handles[i]`: /// wall nanoseconds per call on one thread, and CPU nanoseconds per call. fn concurrent(handles: &[&T], n: u64, call: impl Fn(&T) + Sync) -> (f64, f64) { - let start = Barrier::new(THREADS + 1); + // Every thread waits at `ready`, then at `go`, which opens only once the span has begun. + let (ready, go) = (Barrier::new(THREADS + 1), Barrier::new(THREADS + 1)); // The scope joins every thread before it returns, so the span ends after the last call. let begin = std::thread::scope(|scope| { for &handle in handles { - let (start, call) = (&start, &call); + let (ready, go, call) = (&ready, &go, &call); scope.spawn(move || { - start.wait(); + ready.wait(); + go.wait(); for _ in 0..n { call(black_box(handle)); } }); } - start.wait(); - begin() + ready.wait(); + let begin = begin(); + go.wait(); + begin }); let (wall, cpu) = elapsed(begin); (wall / n as f64, cpu / (n * THREADS as u64) as f64) From ebe728f8c7ab1b7b394f73350526f4a15aef2b23 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:16:10 -0800 Subject: [PATCH 4/4] quest: record the maintainer keeping shared-borrow Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-speed/shared-borrow.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quest/m1/ffi-speed/shared-borrow.md b/quest/m1/ffi-speed/shared-borrow.md index 5027894d6..a12b8c6b8 100644 --- a/quest/m1/ffi-speed/shared-borrow.md +++ b/quest/m1/ffi-speed/shared-borrow.md @@ -12,6 +12,8 @@ and Go's hold counts, and a Swift class's, add a second contended atomic on the ## Plan +- Maintainer decision (2026-11-13): ✅ keep this follow-up (rejected: drop). + Recommended: a reader-biased lock in the style of BRAVO (Dice and Kogan, USENIX ATC 2019) for handles of `Sync` values in `ffi/runtime/rt.rs`. A shared borrow publishes the handle in a slot of a global table hashed by thread and handle, and checks the handle's bias