Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions bench/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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` |
Expand All @@ -36,6 +38,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
Expand All @@ -61,6 +73,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 `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.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 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 (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 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 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.

## Long-lived reader measurement

Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores, 32 threads), after a
Expand Down
12 changes: 12 additions & 0 deletions bench/ffi/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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"),
("objects", "the same from 4 threads at once, Kotlin only"),
Expand All @@ -43,6 +47,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"]
}

# Each step `just ffi bench-run` times from an emptied target, and what it is.
BUILDS = [
("export", "rust2-ffi's export, a `cargo check` of moq under its driver"),
Expand Down Expand Up @@ -144,6 +154,8 @@ def main(raw_path, build_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)]
Expand Down
85 changes: 82 additions & 3 deletions ffi/moq/bench/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -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};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down
66 changes: 63 additions & 3 deletions ffi/moq/bench/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// prints its wall time and the process's CPU time.

#include <algorithm>
#include <atomic>
#include <chrono>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <optional>
#include <thread>
#include <vector>

#include "moq.hpp"
Expand All @@ -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.
Expand Down Expand Up @@ -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 <class T, class F>
span concurrent(const T *const (&handles)[threads], F call, unsigned &sum) {
std::atomic<int> ready{0};
std::atomic<bool> go{false};
unsigned sums[threads] = {};
std::vector<std::thread> 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<double>(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) {
Expand Down Expand Up @@ -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();
Expand All @@ -124,6 +161,29 @@ void round(bool print) {
sum += static_cast<unsigned>(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<unsigned>(t.is_closed()); };
auto sequence = [](const moq::group_Demand &d) { return static_cast<unsigned>(d.sequence()); };
std::vector<moq::track_Producer> tracks;
std::vector<moq::group_Demand> 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");
}

Expand Down Expand Up @@ -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);
}
Expand Down
Loading