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
50 changes: 47 additions & 3 deletions bench/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,59 @@ What they show:
code in 16-byte steps moves the getter between 4.0 and 4.5 with a 64-byte period, and
shifting the Rust export alone, by up to 128 bytes, moves it by no more than 0.1. The
earlier count-only borrow, rebuilt on the same tree, costs 4.15. A Swift wrapper marked
`@inlinable`, so the loop calls the export directly, costs 3.5 at every shift, which
[a quest](/quest/m1/ffi-speed/shared-borrow.md) takes up.
`@inlinable`, so the loop calls the export directly, costs 3.5 at every shift, as
every generated wrapper now is: [Swift's inlined calls](#swifts-inlined-calls).

The load limits these numbers: 4 threads beside others' leave the scheduler free to place
them on one die or across both, whose line transfer is slower. Read the shared rows as
their order of magnitude, against a handle each, at the same load. Rust's and Python's
columns are not remeasured: Rust borrows natively, and Python's threads serialize on the
GIL, which dwarfs the handle's own contention.

## Swift's inlined calls

Measured 2026-11-16 on **nixos**, as above, through `quest gate`, against moq `ffcb7cd9`.
Before is rust2 `8ae7404b`, whose wrappers a consumer calls into the Swift module; after,
every generated body is `@inlinable`, so the bench program's loop calls the C export
itself. Each cell is the median of 3 rounds, before and after alternating at each
placement, at a load average of 2.3 to 6.2: a `.text` pad of 0, 16, 32, or 48 bytes,
linked ahead of the Swift objects, shifts all code through the 64-byte period the getter's
cost followed. Nanoseconds per call, CPU first, then wall; C's getter costs 3.4 and its
handle call 6.4.

| call | pad 0 | pad 16 | pad 32 | pad 48 |
|---|---|---|---|---|
| u64 getter, before | 4.84 / 4.87 | 4.46 / 4.48 | 4.38 / 4.40 | 4.71 / 4.73 |
| u64 getter, after | 3.39 / 3.40 | 3.39 / 3.42 | 3.38 / 3.39 | 3.33 / 3.34 |
| handle call, before | 10.08 / 10.12 | 10.11 / 10.16 | 9.95 / 10.02 | 9.97 / 10.02 |
| handle call, after | 9.87 / 9.92 | 9.85 / 9.91 | 9.90 / 9.95 | 9.79 / 9.84 |
| getter, 4 threads, a handle each, before | 9.84 / 10.49 | 8.75 / 9.99 | 8.21 / 9.11 | 9.32 / 10.09 |
| getter, 4 threads, a handle each, after | 7.24 / 7.88 | 6.99 / 7.71 | 6.71 / 7.19 | 6.92 / 7.93 |
| object, before | 26.52 / 26.61 | 26.55 / 26.68 | 25.68 / 25.77 | 25.91 / 25.99 |
| object, after | 27.62 / 27.74 | 30.22 / 30.34 | 27.72 / 27.84 | 26.68 / 26.78 |
| ready read, before | 458 / 460 | 461 / 463 | 452 / 454 | 449 / 451 |
| ready read, after | 443 / 445 | 445 / 447 | 436 / 438 | 440 / 442 |

1. **The getter costs C's at every placement.** Its loop calls `moq_group_Demand_sequence`
directly, with no merged thunk between, so where the linker puts code no longer moves it.
2. **The handle call inlines too, but keeps its hold.** `TrackProducer` is a class, whose
call holds its handle with a compare-and-swap and lets go with an add of its own, about
3.4 ns over C's. Its body first inlined only once a failed check's error, a handle's
free, and the release of a hold in the table moved out of line, and the handle's
constants became computed properties, which another module folds instead of reading
through an accessor.
3. **A call the optimizer declines to inline still goes through the thunk.** The program's
`track.info()`, whose object it then drops, calls the module's copy before and after, so
its cost still follows placement: 26.7 to 30.2 against 25.7 to 26.6. Most other rows
fell a little: the record by up to 2 ns at three placements, a write by 1.5 to 6, a
stamped write by 2.6 to 5.5, and the ready read, an `async` call, by 9 to 16.
Throughput is bimodal on either side, about 2,200 or 8,600 MB/s for 16 KiB frames in a
run, as the global executor's threads interleave.
4. **The cost is in the Swift module's build.** moq's module, 90,000 lines of Swift, takes
81 to 89 s to build with `-O` against 66, its `.swiftmodule` 19.1 MB against 3.6, and its
library 11.6 to 11.8 MB against 11.5; the bench program's own build is unchanged, 1.2 to
2.1 s.

## Long-lived reader measurement

Measured 2026-11-13 on **nixos**, an AMD Ryzen 9 5900XT (16 cores, 32 threads), after a
Expand Down Expand Up @@ -442,7 +486,7 @@ frame path:
(5,298), leaving Dart's own await, which schedules a microtask even for a complete
future. Go's is the returned frame's ownership (`AddCleanup`, `Cleanup.Stop`, and a
close's two crossings), and cgo's check and heap allocation of each out pointer, which
[Go's pointers](../../quest/m1/ffi-speed/go-pointers.md) takes: what remains after both
[kixel-ai/rust2#837](https://github.com/kixel-ai/rust2/pull/837) took: what remains after both
is one crossing. Off the main thread, glibc's malloc arena for the thread grows and
trims with `mprotect`, which adds about 60 ns to a write and 20 to a read in C on a
pthread (`MALLOC_ARENA_MAX=1` removes it). Swift's ready read runs in a task on such a
Expand Down
10 changes: 10 additions & 0 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,16 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u
returns wakes through Rust and drops once Swift releases it.
- **The contract** is checked once, at the first call into the library: a Swift file and a
library from different builds trap.
- **Every body may inline into its caller.** Each function, initializer, subscript, and
computed property is `@inlinable`, and what they name `@usableFromInline`, so a
consumer's optimizer in another module can inline a call and reach the C function
directly, with no flag of its own. Where it declines, the call goes to the module's
copy, which Swift may merge with like wrappers into one that calls the C function
through a pointer, whose cost follows where the linker places it. Their cold paths, a
failed check's error and a handle's free, stay out of line, so a call more often looks
worth inlining. The module's `.swiftmodule` holds the bodies, about 5 times its size
without them.

## Go

`go/` holds a Go package per crate whose items the facade exports, over the C header
Expand Down
67 changes: 50 additions & 17 deletions ffi/runtime/rt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public struct CheckError: Swift.Error, CustomStringConvertible, Sendable {
/// What broke.
public let message: Swift.String

init(function: Swift.String, message: Swift.String) {
self.function = function
self.message = message
}

public var description: Swift.String { "\(function): \(message)" }
}

Expand Down Expand Up @@ -396,6 +401,8 @@ extension SeekableReader {
extension Native {
/// A seek past `Int64.max` of a reader with no `seek(start:)` of its own.
struct SeekPast: Swift.Error, CustomStringConvertible {
init() {}

var description: Swift.String { "a seek past Int64.max of a reader with no seek(start:)" }
}
}
Expand Down Expand Up @@ -814,17 +821,17 @@ extension Native {
let free: (@convention(c) (OpaquePointer?) -> Void)?
let type: StaticString

static let closed = 1
static let bias = 2
static let held = 4
static var closed: Int { 1 }
static var bias: Int { 2 }
static var held: Int { 4 }
/// Set in a slot whose hold a close counted.
static let revoked = 1
static var revoked: Int { 1 }
/// A cache line of rows to a column, which the thread's stack picks, and a row per
/// handle, with a line between columns: two threads in one column, or two handles in
/// one row on one thread, use the count.
static let rowBits = 3
static let columnBits = 8
static let stride = 2 << rowBits
static var rowBits: Int { 3 }
static var columnBits: Int { 8 }
static var stride: Int { 2 << rowBits }
/// Only ever touched atomically.
nonisolated(unsafe) static let slots: UnsafeMutablePointer<Int> = {
let count = stride << columnBits
Expand Down Expand Up @@ -853,11 +860,24 @@ extension Native {
/// Hold it for a call, until `release` takes what this returns. Traps once closed.
func hold() -> Int {
guard let slot = held() else {
fatalError("\(type) is closed")
closed()
}
return slot
}

/// Trap for a call of a closed handle: out of line, so a caller's inlined call stays
/// small enough to inline.
@inline(never)
func closed() -> Never {
fatalError("\(type) is closed")
}

/// The error a call of `what` throws once it is closed, out of line as `closed` is.
@inline(never)
func closed(_ what: StaticString) -> CheckError {
CheckError(function: "\(what)", message: "\(type) is closed")
}

/// Hold it for a call, until `release` takes what this returns, unless it is closed.
@inline(__always)
func held() -> Int? {
Expand Down Expand Up @@ -927,14 +947,27 @@ extension Native {

@inline(__always)
func release(_ slot: Int) {
if slot >= 0 && exchange(Handle.slots + slot, 0) == key {
return
if slot >= 0 {
release(listed: slot)
} else if add(state, -Handle.held) < Handle.held {
freed()
}
if add(state, -Handle.held) < Handle.held {
free?(raw)
}

/// `release` of a hold in `slots`, out of line, as `closed` is.
@inline(never)
func release(listed slot: Int) {
if exchange(Handle.slots + slot, 0) != key && add(state, -Handle.held) < Handle.held {
freed()
}
}

/// Free it once its last hold lets go: out of line, as `closed` is.
@inline(never)
func freed() {
free?(raw)
}

/// Run `body` with the raw handle, held until it returns.
@inline(__always)
func with<R: ~Copyable>(_ body: (OpaquePointer?) throws -> R) rethrows -> R {
Expand All @@ -948,7 +981,7 @@ extension Native {
@inline(__always)
func with<R: ~Copyable>(_ what: StaticString, _ body: (OpaquePointer?) throws -> R) throws -> R {
guard let slot = held() else {
throw CheckError(function: "\(what)", message: "\(type) is closed")
throw closed(what)
}
defer { release(slot) }
return try body(raw)
Expand Down Expand Up @@ -1104,7 +1137,7 @@ extension Native {
/// a throw for the call if it is closed.
func held(_ handle: Handle) throws -> OpaquePointer? {
guard let slot = handle.held() else {
throw CheckError(function: "\(what)", message: "\(handle.type) is closed")
throw handle.closed(what)
}
held.append((handle, slot))
return handle.raw
Expand Down Expand Up @@ -1307,9 +1340,9 @@ extension Native {
/// What a future parks on: it latches a wake that lands between a poll that pends and
/// the park, so the next poll sees it.
final class Signal: @unchecked Sendable {
static let idle = 0
static let woken = 1
static let parked = 2
static var idle: Int { 0 }
static var woken: Int { 1 }
static var parked: Int { 2 }

/// Which of those it is, changed only atomically.
private let state = UnsafeMutablePointer<Swift.Int>.allocate(capacity: 1)
Expand Down
Loading