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
13 changes: 10 additions & 3 deletions bench/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ measure them at the load beside them.
### Copy values

A `Copy` type with private fields, such as `Timestamp`, crosses by value: a struct of its
bytes in C, C++, Swift, Go, and Dart, a class over a `LongArray` in Kotlin (a word value
bytes in C, C++, Swift, Go, and Dart, a class of a `Long` per word in Kotlin (a word value
class when it fits one), and a class of the runtime's `Value` holding its bytes in Python.
Measured 2026-11-13 at rust2 `7766768f`, moq `ffcb7cd9`, on an AMD Ryzen 9 9950X (32 threads),
Linux, at a load average of 13 to 15, the median of 3 rounds, in CPU time:
Expand All @@ -466,8 +466,15 @@ Linux, at a load average of 13 to 15, the median of 3 rounds, in CPU time:
| stamped minus write | 0 | 3 | 1 | 33 | 45 | 150 (35 wall) | 160 |

C, C++, and Swift pay nothing for the timestamp; Go and Dart pay about their getter's call,
the `from_micros` itself. Kotlin's 35 ns wall is its call, and its CPU column adds the
collector's work on each `LongArray`. Python's is a call and a new object. The handle's cost
the `from_micros` itself. Kotlin's 35 ns wall was its call, and its CPU column adds the
collector's work on each `LongArray`. Since its value is a class of a `Long` per word, and a
`Result` of one returns its words through the thread's slots rather than encoded, the
stamped write costs Kotlin 30 ns of wall time over the write, against 270 on main's build:
measured 2026-11-14 at rust2 `2ec35316`, moq `ffcb7cd9`, on the same host, interleaved over 7
rounds at a load average of 6 to 17, the medians of each round's difference. Its CPU time,
which counts the JIT's and collector's threads, ranged from 30 to 300 ns at that load.
The thread allocates 32 B a stamped frame over the write, the `Timestamp` itself, which
`write_frame` keeps from the JIT's escape analysis, against 112 on main's. Python's is a call and a new object. The handle's cost
in the tables above came from another load, so Python's was also measured against main's
build on the same host, interleaved over 5 rounds at a load of 10 to 12: stamped minus write
fell from 205 ns to 166, and the stamped write from 441 to 391.
Expand Down
10 changes: 6 additions & 4 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,10 @@ kotlinx-coroutines. Each value crosses JNI as a scalar, a handle as a `Long`, by
in as a `ByteArray`, bytes returned as their owner, a block of numbers as its primitive
array, and anything else encoded into a `ByteArray` as its C struct holds it. A record a
call takes or returns directly, or in an `Option` returned, crosses as its scalars, a
nested record's included, as JNI parameters in and through a `LongArray` each thread keeps
out, with only its other fields encoded.
nested record's included, and an opaque value as its 64-bit words, as JNI parameters in
and through a `LongArray` each thread keeps out, with only a record's other fields encoded;
an opaque value of one word returns as that `Long`. Past the 255 slots a JVM method takes,
the rest cross encoded.

- **Names** are in a package of the prefix: types in PascalCase without it, members in
camelCase without their type's name (`moq.TrackProducer.appendGroup`). Pascal case
Expand Down Expand Up @@ -556,8 +558,8 @@ out, with only its other fields encoded.
value** is a class with an internal constructor, so only Rust makes one: of no more than
8 bytes on every target, with no padding, fields of integers, `bool`s, and `char`s alone, nested or not, and a
derived or std `PartialEq`, a value class over a `Long`, compared by its bits, which
allocates nothing unless Kotlin boxes it, as in a `List` or a nullable; otherwise one over
a `LongArray`, whose `==` is Rust's, and whose `hashCode` is 0, since none Kotlin
allocates nothing unless Kotlin boxes it, as in a `List` or a nullable; otherwise a class of a `Long` for each 64-bit word of its bytes, which allocates
nothing where the JIT sees it does not escape, whose `==` is Rust's, and whose `hashCode` is 0, since none Kotlin
computes agrees with it. Either is shared by reference, so it never changes: a `&mut
self` method, a field's setter included, returns the value as the call left it,
`x = x.add(o)`, and beside Rust's own return, a `Pair`, or each element of its tuple,
Expand Down
94 changes: 92 additions & 2 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,19 @@ impl Moment {
}
}

/// `ticks` of `rate` a second, failing at a rate of zero: a value past a word in a `Result`.
pub fn moment_at(ticks: u64, rate: u64) -> Result<Moment, String> {
Moment::new(ticks, rate).ok_or_else(|| "a rate of zero".to_string())
}

/// The stamp `millis` from zero, failing at zero: a value of a word in a `Result`.
pub fn stamp_at(millis: u64) -> Result<Stamp, String> {
match millis {
0 => Err("zero".to_string()),
millis => Ok(Stamp::from_millis(millis)),
}
}

/// The moment `shift` returns for `moment`, which C copies in and out.
pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment {
shift(moment)
Expand All @@ -1325,8 +1338,9 @@ impl Gap {
}
}

/// A value named as Kotlin's `LongArray`, which holds a value's words, with a method named
/// as C++'s member holding its bytes: each generator keeps its own apart.
/// A value named as Kotlin's `LongArray`, which holds a returned value's words, with methods named
/// as C++'s member holding its bytes and Kotlin's `also`, which makes a value past a word: each
/// generator keeps its own apart.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LongArray {
words: [u64; 2],
Expand All @@ -1340,8 +1354,84 @@ impl LongArray {
pub fn c_(&self) -> u64 {
self.words[0].wrapping_add(self.words[1])
}

/// Calls `f` with its low word.
pub fn also(&self, f: impl Fn(u64)) {
f(self.words[0])
}
}

/// A type named as the annotation Kotlin's fields take, which the binding qualifies.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JvmField {
pub n: u32,
}

/// A type named as the annotation Kotlin's static methods take, which the binding qualifies.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JvmStatic {
pub n: u32,
}

/// A type named as the annotation Kotlin opts in to an API with, which the binding qualifies.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OptIn {
pub n: u32,
}

/// The sum of a `JvmField`, a `JvmStatic`, and an `OptIn`.
pub fn jvm_sum(field: JvmField, method: JvmStatic, opt: OptIn) -> u32 {
field.n.wrapping_add(method.n).wrapping_add(opt.n)
}

/// A value of 128 words, past what a JVM method's 255 slots hold as `long`s: Kotlin makes
/// one with no constructor of a parameter each, and passes one encoded.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Wide {
words: [u64; 128],
}

impl Wide {
/// Words `seed`, `seed + 1`, and on, wrapping.
pub fn new(seed: u64) -> Self {
Self {
words: std::array::from_fn(|k| seed.wrapping_add(k as u64)),
}
}

pub fn sum(&self) -> u64 {
self.words.iter().fold(0, |a, w| a.wrapping_add(*w))
}
}

/// A moment and a count: a record holding a value past a word, flattened as its words.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Clocked {
pub at: Moment,
pub count: u32,
}

/// `clocked`, its moment `ticks` later and its count one more.
pub fn clocked_after(clocked: Clocked, ticks: u64) -> Clocked {
Clocked {
at: clocked.at.after(ticks),
count: clocked.count + 1,
}
}

/// Declares `crammed`, whose numbers take 248 of a JVM method's 255 slots, so its first
/// moment crosses as its words and its second, past the limit, encoded.
macro_rules! crammed {
($($n:ident)*) => {
/// Both moments' millis and every number, summed.
pub fn crammed(a: Moment, b: Moment, $($n: u64),*) -> u64 {
a.millis().wrapping_add(b.millis())$(.wrapping_add($n))*
}
};
}

crammed!(n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14 n15 n16 n17 n18 n19 n20 n21 n22 n23 n24 n25 n26 n27 n28 n29 n30 n31 n32 n33 n34 n35 n36 n37 n38 n39 n40 n41 n42 n43 n44 n45 n46 n47 n48 n49 n50 n51 n52 n53 n54 n55 n56 n57 n58 n59 n60 n61 n62 n63 n64 n65 n66 n67 n68 n69 n70 n71 n72 n73 n74 n75 n76 n77 n78 n79 n80 n81 n82 n83 n84 n85 n86 n87 n88 n89 n90 n91 n92 n93 n94 n95 n96 n97 n98 n99 n100 n101 n102 n103 n104 n105 n106 n107 n108 n109 n110 n111 n112 n113 n114 n115 n116 n117 n118 n119 n120 n121 n122 n123);

/// A `Copy` type whose `None` leaves its payload's bytes uninitialized, so no padding table
/// covers it: a handle, rather than a value.
#[derive(Clone, Copy, Debug)]
Expand Down
58 changes: 11 additions & 47 deletions ffi/runtime/jni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,62 +1014,26 @@ impl Writer {
}
}

/// An opaque value's C struct from the `long` Kotlin holds it in: its bytes, at the word's
/// lowest addresses, as [`to_word`] put them.
pub fn from_word<T: Copy>(word: jlong) -> T {
const { assert!(size_of::<T>() <= size_of::<jlong>()) };
/// An opaque value's C struct from the `W` `long`s Kotlin holds it in: its bytes, in the
/// words' order, as [`to_words`] put them.
pub fn from_words<T: Copy, const W: usize>(words: [jlong; W]) -> T {
const { assert!(size_of::<T>() <= W * size_of::<jlong>()) };
let mut value = std::mem::MaybeUninit::<T>::zeroed();
// SAFETY: `T` is a struct of `MaybeUninit` units, any bytes of which are one.
unsafe {
std::ptr::copy_nonoverlapping((&raw const word).cast::<u8>(), value.as_mut_ptr().cast::<u8>(), size_of::<T>());
value.assume_init()
}
}

/// The `long` Kotlin holds an opaque value's C struct in, which has no padding: Kotlin
/// compares two by it.
pub fn to_word<T: Copy>(value: T) -> jlong {
const { assert!(size_of::<T>() <= size_of::<jlong>()) };
let mut word: jlong = 0;
// SAFETY: every byte of a value with no padding is initialized, and the rest are zero.
unsafe { std::ptr::copy_nonoverlapping((&raw const value).cast::<u8>(), (&raw mut word).cast::<u8>(), size_of::<T>()) };
word
}

/// An opaque value's C struct from the `W` words of the `long[]` Kotlin holds it in.
///
/// # Safety
/// `array` is a live `long[]`.
pub unsafe fn from_words<T: Copy, const W: usize>(env: *mut JNIEnv, array: jlongArray) -> T {
const { assert!(size_of::<T>() <= W * size_of::<jlong>()) };
let mut words = [std::mem::MaybeUninit::<jlong>::zeroed(); W];
// SAFETY: `array` is live, and `T` is a struct of `MaybeUninit` units.
unsafe {
if array.is_null() || ((**env).v1_2.GetArrayLength)(env, array) != W as jsize {
fail(env, "jni", "an opaque value's words are not its own");
}
((**env).v1_2.GetLongArrayRegion)(env, array, 0, W as jsize, words.as_mut_ptr().cast());
let mut value = std::mem::MaybeUninit::<T>::zeroed();
std::ptr::copy_nonoverlapping(words.as_ptr().cast::<u8>(), value.as_mut_ptr().cast::<u8>(), size_of::<T>());
value.assume_init()
}
}

/// A new `long[]` of the `W` words an opaque value's C struct is held in: its bytes, then
/// zeros.
pub fn to_words<T: Copy, const W: usize>(env: *mut JNIEnv, value: T) -> jlongArray {
/// The `W` `long`s Kotlin holds an opaque value's C struct in: its bytes, then zeros.
/// Kotlin compares a value class of one by it.
pub fn to_words<T: Copy, const W: usize>(value: T) -> [jlong; W] {
const { assert!(size_of::<T>() <= W * size_of::<jlong>()) };
let mut words = [std::mem::MaybeUninit::<jlong>::zeroed(); W];
// SAFETY: `env` is this thread's; JNI copies the words as bytes.
unsafe {
std::ptr::copy_nonoverlapping((&raw const value).cast::<u8>(), words.as_mut_ptr().cast::<u8>(), size_of::<T>());
let array = ((**env).v1_2.NewLongArray)(env, W as jsize);
if array.is_null() {
fail(env, "jni", "a long[] is out of memory");
}
((**env).v1_2.SetLongArrayRegion)(env, array, 0, W as jsize, words.as_ptr().cast());
array
}
let mut words = [0; W];
// SAFETY: an opaque value's C struct initializes every byte, as the glue makes it.
unsafe { std::ptr::copy_nonoverlapping((&raw const value).cast::<u8>(), words.as_mut_ptr().cast::<u8>(), size_of::<T>()) };
words
}

/// An opaque value's C struct as it crosses encoded: the `W` words Kotlin holds it in,
Expand Down
8 changes: 4 additions & 4 deletions ffi/runtime/rt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ abstract class __Cleanup(owner: Any?) : java.lang.ref.PhantomReference<Any>(owne
val QUEUE = java.lang.ref.ReferenceQueue<Any>()
/** One lock and list of the tracked. */
class Stripe {
@JvmField var head: __Cleanup? = null
@kotlin.jvm.JvmField var head: __Cleanup? = null
}

// A power of two, past the cores a phone or most servers have.
Expand Down Expand Up @@ -407,7 +407,7 @@ class __Handle(
) : __Cleanup(if (free == null) null else owner) {
// The holds, one for the owner until closed and one per call, shifted past a bit set
// once closed: one word, so a handle allocates nothing beside itself.
@Volatile @JvmField internal var state = HELD
@kotlin.concurrent.Volatile @kotlin.jvm.JvmField internal var state = HELD

init {
if (free != null) track()
Expand Down Expand Up @@ -563,7 +563,7 @@ class __Signal : java.util.concurrent.atomic.AtomicReference<Any?>(null), Waker
override fun wake() {
val parked = getAndSet(WOKEN)
if (parked !is kotlinx.coroutines.CancellableContinuation<*>) return
@Suppress("UNCHECKED_CAST")
@kotlin.Suppress("UNCHECKED_CAST")
val c = parked as kotlinx.coroutines.CancellableContinuation<Unit>
// Never resumed in place, since the next poll would run inside Rust's wake, which may
// hold a lock that poll takes: a dispatcher that would resume here is made to defer,
Expand Down Expand Up @@ -614,7 +614,7 @@ class __Pollable(

// The caller's job reaches it as it starts cancelling, not once its children finish,
// which only the internal overload of invokeOnCompletion tells.
@OptIn(kotlinx.coroutines.InternalCoroutinesApi::class)
@kotlin.OptIn(kotlinx.coroutines.InternalCoroutinesApi::class)
fun poll(given: Long): Boolean {
val old: Long
synchronized(this) {
Expand Down
Loading