From 0f5141569397d7d982358a004e43e947fa60a9ee Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 21:42:28 -0800 Subject: [PATCH 1/9] chore(quest): claim quest/m1/ffi-speed/android-jni.md Quest-Claim: 1c4d5bfff5b5662a From 48c6caf9257c2d705efeb3a91a857a5d2f8a9c71 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 22:17:22 -0800 Subject: [PATCH 2/9] bench: measure the Kotlin rows on ART beside HotSpot's (wip) --- bench/ffi/report.py | 12 ++++++--- ffi/justfile | 58 +++++++++++++++++++++++++++++++++++++++++ ffi/moq/bench/export.kt | 40 +++++++++++++++++++++++----- 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/bench/ffi/report.py b/bench/ffi/report.py index c34f7e309..96dcd43ec 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -3,9 +3,9 @@ from `just ffi bench-run`'s raw.tsv; the median time and peak RSS of the export and its crate's build, beside moq's own, from its build.tsv; and the sizes of the export's generated bindings and native libraries, and of the wasm32 stand-in's TypeScript, module, and npm package, and of -one caller's bundle. +one caller's bundle. `just ffi bench-android`'s raw.tsv alone prints only the time tables. -Usage: report.py +Usage: report.py [ ] """ import collections @@ -23,6 +23,7 @@ ("cpp", "C++"), ("moq-c", "moq-c"), ("kotlin", "Kotlin"), + ("kotlin-art", "Kotlin (ART)"), ("python", "Python"), ("swift", "Swift"), ("go", "Go"), @@ -219,7 +220,7 @@ def builds(path): return table(["step", "wall s", "CPU s", "peak RSS", "load average"], rows) -def main(raw_path, build_path, export, web, caller): +def main(raw_path, build_path=None, export=None, web=None, caller=None): rounds = collections.defaultdict(list) with open(raw_path) as f: for line in f: @@ -261,6 +262,9 @@ def cell(lang, case, i): print(table(header, rows)) print() + if build_path is None: + return + print("### Build (median, from an emptied target)\n") print(builds(build_path)) print() @@ -305,6 +309,6 @@ def cell(lang, case, i): if __name__ == "__main__": - if len(sys.argv) != 6: + if len(sys.argv) not in (2, 6): sys.exit(__doc__) main(*sys.argv[1:]) diff --git a/ffi/justfile b/ffi/justfile index eba33772f..26dd1f347 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -140,6 +140,64 @@ bench-build checkout="": (moq-export checkout) (moq-web checkout) ts-program cod {{ quote(just_executable()) }} swift-module -O swiftc -O -warnings-as-errors $({{ quote(just_executable()) }} swift-target 15) -I target/swift -I target/swift-collections/build -I "$out/swift" -I "$hooks" moq/bench/export.swift target/swift/libMoq.a -L target/swift-collections/build -lBasicContainers -lInternalCollectionsUtilities -lSpanPreview "$out/target/release/libmoq.a" "$hooks/libcodspeed.a" -lpthread -ldl -lm -o target/bench-export-swift +# Measure the binding benchmark's Kotlin rows (bench/ffi/README.md) on HotSpot here and, shrunk by R8 as an app is, on ART on the Android device adb reaches, from the pinned moq revision or a checkout, in the Android shell (nix develop .#android) with ANDROID_HOME and ANDROID_NDK_HOME set. Its tables go to target/bench-android/report.md. +bench-android rounds="3" checkout="": + #!/usr/bin/env bash + set -euo pipefail + sdk=${ANDROID_HOME:?ANDROID_HOME names the Android SDK} + adb=$sdk/platform-tools/adb + newest() { find "$1" -mindepth 1 -maxdepth 1 | sort -V | tail -n 1; } + abi=$("$adb" shell getprop ro.product.cpu.abi | tr -d '\r') + case "$abi" in + arm64-v8a) target=aarch64-linux-android ;; + x86_64) target=x86_64-linux-android ;; + *) echo "bench-android: the Android shell's toolchain has no target for the device's ABI $abi" >&2; exit 1 ;; + esac + # The export merges the device's API, so its Kotlin and libraries serve both. + {{ quote(just_executable()) }} moq-api "$target" {{ quote(checkout) }} + {{ quote(just_executable()) }} moq-write {{ quote(checkout) }} + out=target/export/moq + dir=target/bench-android + rm -rf "$dir" + mkdir -p "$dir/dex" + cargo build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features kotlin + cargo ndk --target "$abi" --platform 24 --output-dir "$dir/lib" build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features kotlin + # One jar for both VMs, the Kotlin standard library inside, as R8 reads an app's. + JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" moq/bench/export.kt -include-runtime -d "$dir/bench.jar" + (cd "$dir" && jar xf "$KOTLINX_COROUTINES" META-INF/com.android.tools/r8/coroutines.pro) + # HotSpot's CPU clock, which ART's run never reaches, is not in Android's library. + printf '%s\n' '-keep class ExportKt { public static void main(java.lang.String[]); }' '-dontwarn com.sun.management.**' '-dontwarn java.lang.management.**' '-dontwarn org.jetbrains.annotations.**' > "$dir/app.pro" + java -cp "$(newest "$sdk/build-tools")/lib/d8.jar" com.android.tools.r8.R8 --release --min-api 24 --lib "$(newest "$sdk/platforms")/android.jar" \ + --pg-conf "$out/kotlin/moq.pro" --pg-conf "$dir/META-INF/com.android.tools/r8/coroutines.pro" --pg-conf "$dir/app.pro" \ + --output "$dir/dex" "$dir/bench.jar" "$KOTLINX_COROUTINES" + device=/data/local/tmp/rust2-bench + "$adb" shell "rm -rf $device && mkdir -p $device/dalvik-cache" + "$adb" push --sync "$dir/dex/classes.dex" "$dir/lib/$abi/libmoq.so" "$device" > /dev/null + # As bench-run does: each program's lines prefixed by its language and round, and + # suffixed by the host's load average over the run. + raw=$dir/raw.tsv + : > "$raw" + run() { + local lang=$1 before after + shift + before=$(awk '{ print $1 }' /proc/loadavg) + "$@" | tr -d '\r' > "$dir/run.tsv" + after=$(awk '{ print $1 }' /proc/loadavg) + awk -v lang="$lang" -v round="$r" -v loadavg="$(echo "$before $after" | awk '{ print ($1 + $2) / 2 }')" -F '\t' \ + 'NF == 3 { print lang "\t" round "\t" $1 "\t" $2 "\t" $3 "\t" loadavg; next } { print "bench-android: " lang ": unexpected line: " $0 > "/dev/stderr"; exit 1 }' \ + "$dir/run.tsv" >> "$raw" + } + for r in $(seq {{ rounds }}); do + run kotlin java -Djava.library.path="$out/target/release" -cp "$dir/bench.jar:$KOTLINX_COROUTINES" ExportKt + run kotlin-art "$adb" shell "ANDROID_DATA=$device dalvikvm -Djava.library.path=$device -cp $device/classes.dex ExportKt" + done + { + printf '## Kotlin on HotSpot and ART\n\nART: Android %s (API %s), %s, %s cores.\n\n' \ + "$("$adb" shell getprop ro.build.version.release | tr -d '\r')" "$("$adb" shell getprop ro.build.version.sdk | tr -d '\r')" "$abi" "$("$adb" shell nproc | tr -d '\r')" + python3 -I ../bench/ffi/report.py "$raw" + } > "$dir/report.md" + cat "$dir/report.md" + # Build moq/bench/codspeed.c over the bench shell's instrument-hooks into target/codspeed: a static library the compiled programs link, a shared one with the JNI functions that Kotlin, Python, and Dart load, the addon Node loads, and a module map Swift imports it by. [private] codspeed-hooks: diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 0a04f0977..413a49616 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -1,7 +1,8 @@ // bench/export.c's cases through the generated Kotlin, so the two differ by what JNI and // the Kotlin 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 JIT's and GC's threads included, or under -// CodSpeed, runs once for its CPU simulation to count. +// CodSpeed, runs once for its CPU simulation to count. It runs on HotSpot and, shrunk by R8, +// on ART. import java.lang.management.ManagementFactory import java.util.concurrent.CountDownLatch @@ -22,9 +23,25 @@ val SIZES = listOf(100, 1024, 16384, 65536, 1048576) // Wall and CPU nanoseconds: a point, or a span per call. data class Span(val wall: Double, val cpu: Double) -val os = ManagementFactory.getOperatingSystemMXBean() as com.sun.management.OperatingSystemMXBean +// ART, which has no java.lang.management, reports itself as Dalvik. +val art = System.getProperty("java.vm.name") == "Dalvik" -fun now() = Span(System.nanoTime().toDouble(), os.processCpuTime.toDouble()) +// HotSpot's count of the process's CPU time, apart so that ART never resolves its class. +object HotSpot { + val os = ManagementFactory.getOperatingSystemMXBean() as com.sun.management.OperatingSystemMXBean +} + +// The process's CPU nanoseconds, every thread's, ended ones included: on ART, the kernel's +// user and system ticks, at the 100 Hz USER_HZ every Linux ABI fixes, which is HotSpot's +// own clock on Linux too. +fun cpu(): Long = when (art) { + true -> java.io.File("/proc/self/stat").readText().substringAfterLast(") ").split(' ').let { stat -> + (stat[11].toLong() + stat[12].toLong()) * 10_000_000 + } + false -> HotSpot.os.processCpuTime +} + +fun now() = Span(System.nanoTime().toDouble(), cpu().toDouble()) // Nanoseconds per call since `start`, over `n` calls. fun per(start: Span, n: Double = N.toDouble()): Span { @@ -32,6 +49,10 @@ fun per(start: Span, n: Double = N.toDouble()): Span { return Span((end.wall - start.wall) / n, (end.cpu - start.cpu) / n) } +// Whether CodSpeed runs this, as `just ffi codspeed` says: only then is its library loaded, +// which ART's run has none of. +val codspeed = System.getenv("RUST2_CODSPEED") != null + // moq/bench/codspeed.h, through the JNI functions in the shared library bench-build makes. object Codspeed { init { @@ -48,13 +69,13 @@ object Codspeed { // A case's start: now, and CodSpeed counting from here. fun begin(): Span { val start = now() - Codspeed.start() + if (codspeed) Codspeed.start() return start } // The case `name`'s nanoseconds per call since `start`, over `n` calls. fun end(start: Span, name: String, n: Double = N.toDouble()): Span { - Codspeed.stop(name) + if (codspeed) Codspeed.stop(name) return per(start, n) } @@ -165,6 +186,13 @@ fun round(measured: Boolean): Map { repeat(N) { track.info().close() } out["object"] = end(start, "object") + // A primitive pub field read, which reaches no user code. + track.info().use { info -> + start = begin() + repeat(N) { sum += info.priorityGet } + out["field"] = end(start, "field") + } + // The same from THREADS threads at once, so objects made and freed together contend // for whatever the binding shares between them. Kotlin's alone: the others share none. // CodSpeed skips it as it does the other threaded cases. @@ -291,7 +319,7 @@ fun round(measured: Boolean): Map { fun main() { // Under CodSpeed, one round, which prints nothing: its simulation counts each case. - if (Codspeed.init("ffi/moq/bench/export.kt::kotlin")) { + if (codspeed && Codspeed.init("ffi/moq/bench/export.kt::kotlin")) { GROUPS /= 10 round(true) return From dd32e8c35bb0163e7a75625ae9d317d94d4316cb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:00:29 -0800 Subject: [PATCH 3/9] ffi(kotlin): @FastNative on leaves, registered by __contract (wip) --- ffi/justfile | 6 +-- ffi/runtime/FastNative.kt | 10 ++++ ffi/runtime/jni.rs | 15 ++++-- ffi/src/glue.rs | 1 + ffi/src/kotlin.rs | 94 ++++++++++++++++++++++++++---------- ffi/tests/fixtures.rs | 3 +- ffi/tests/fixtures/corpus.rs | 3 +- ffi/tests/fixtures/shapes.rs | 4 +- 8 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 ffi/runtime/FastNative.kt diff --git a/ffi/justfile b/ffi/justfile index 26dd1f347..a0429c93d 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -136,7 +136,7 @@ bench-build checkout="": (moq-export checkout) (moq-web checkout) ts-program cod {{ quote(just_executable()) }} dart-program bench-dart moq/bench/export.dart rm -rf target/bench-kt # moq's Kotlin outgrows the 512 MiB heap kotlinc's launcher defaults to. - JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" moq/bench/export.kt -d target/bench-kt + JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" "$out/kotlin/FastNative.kt" moq/bench/export.kt -d target/bench-kt {{ quote(just_executable()) }} swift-module -O swiftc -O -warnings-as-errors $({{ quote(just_executable()) }} swift-target 15) -I target/swift -I target/swift-collections/build -I "$out/swift" -I "$hooks" moq/bench/export.swift target/swift/libMoq.a -L target/swift-collections/build -lBasicContainers -lInternalCollectionsUtilities -lSpanPreview "$out/target/release/libmoq.a" "$hooks/libcodspeed.a" -lpthread -ldl -lm -o target/bench-export-swift @@ -163,7 +163,7 @@ bench-android rounds="3" checkout="": cargo build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features kotlin cargo ndk --target "$abi" --platform 24 --output-dir "$dir/lib" build --locked --quiet --release --manifest-path "$out/Cargo.toml" --features kotlin # One jar for both VMs, the Kotlin standard library inside, as R8 reads an app's. - JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" moq/bench/export.kt -include-runtime -d "$dir/bench.jar" + JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" "$out/kotlin/FastNative.kt" moq/bench/export.kt -include-runtime -d "$dir/bench.jar" (cd "$dir" && jar xf "$KOTLINX_COROUTINES" META-INF/com.android.tools/r8/coroutines.pro) # HotSpot's CPU clock, which ART's run never reaches, is not in Android's library. printf '%s\n' '-keep class ExportKt { public static void main(java.lang.String[]); }' '-dontwarn com.sun.management.**' '-dontwarn java.lang.management.**' '-dontwarn org.jetbrains.annotations.**' > "$dir/app.pro" @@ -340,7 +340,7 @@ moq checkout="": (moq-cpp checkout) target/moq-test # The Kotlin, on the JVM, through the JNI functions the kotlin feature builds. rm -rf target/moq-kt - JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" moq/moq.kt -d target/moq-kt + JAVA_OPTS="-Xmx2g -Xms128M" kotlinc -Werror -cp "$KOTLINX_COROUTINES" "$out/kotlin/moq.kt" "$out/kotlin/FastNative.kt" moq/moq.kt -d target/moq-kt kotlin -Djava.library.path="$out/target/release" -cp "target/moq-kt:$KOTLINX_COROUTINES" MoqKt # The Python, through the CPython extension the python feature builds, and its types # through mypy and pyright, as `api_py_types` checks the api fixture's. diff --git a/ffi/runtime/FastNative.kt b/ffi/runtime/FastNative.kt new file mode 100644 index 000000000..5303a76e5 --- /dev/null +++ b/ffi/runtime/FastNative.kt @@ -0,0 +1,10 @@ +// Generated by rust2-ffi: ART's `@FastNative`, which Android's SDK makes public only from +// API 34, for a build against an older SDK or the JVM's. ART reads it off the dex by its +// name, Android 7 and the JVM ignore it, and on a device the boot class path's own class +// shadows this one. Do not edit. + +package dalvik.annotation.optimization + +@Retention(AnnotationRetention.BINARY) +@Target(AnnotationTarget.FUNCTION) +annotation class FastNative diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index ccd8752e9..acf20ef86 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -26,18 +26,25 @@ use crate::rt; static VM: OnceLock = OnceLock::new(); static CLASS: OnceLock = OnceLock::new(); -/// Note the JVM and `class`, from its first native call, which its initializer makes. The -/// library defines no `JNI_OnLoad`, so the crate it exports may define its own. +/// Note the JVM and `class`, from its first native call, which its initializer makes, and +/// register `natives` on it: the `@FastNative` methods, which ART binds by their names only +/// from Android 12. The library defines no `JNI_OnLoad`, so the crate it exports may define +/// its own. /// /// # Safety -/// `env` is this thread's, and `class` the native class. -pub unsafe fn load(env: *mut JNIEnv, class: jclass) { +/// `env` is this thread's, `class` the native class, and each of `natives` a function of +/// the signature it names. +pub unsafe fn load(env: *mut JNIEnv, class: jclass, natives: &[JNINativeMethod]) { // SAFETY: `env` is this thread's, as the caller's contract says. unsafe { let mut vm: *mut JavaVM = std::ptr::null_mut(); if ((**env).v1_2.GetJavaVM)(env, &mut vm) != JNI_OK { fail(env, "load", "the JVM is missing"); } + // The rules keep each, so a shrunk app lacking one is a build's mistake. + if !natives.is_empty() && ((**env).v1_2.RegisterNatives)(env, class, natives.as_ptr(), natives.len() as jint) != JNI_OK { + fail(env, "load", "a native method to register is missing"); + } let _ = VM.set(vm as usize); CLASS.get_or_init(|| ((**env).v1_2.NewGlobalRef)(env, class) as usize); // JNI permits a VM without direct-buffer access. Refuse it while loading, diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index bed53f3da..8e7acb76c 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -455,6 +455,7 @@ impl Crate<'_> { &out.join("kotlin").join(format!("{}.pro", self.abi.prefix)), &kotlin.rules, ); + output.file(&out.join("kotlin").join("FastNative.kt"), crate::kotlin::FAST_NATIVE); output.file(&out.join("src/python/mod.rs"), &python.glue); output.file(&out.join("src/python/rt.rs"), crate::python::GLUE_RUNTIME); output.file( diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index edbedfd49..b96817558 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -35,6 +35,8 @@ const RUNTIME: &str = include_str!("../runtime/rt.kt"); /// The runtime's public types, at the generated file's top level. const TYPES: &str = include_str!("../runtime/types.kt"); pub(crate) const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); +/// `kotlin/FastNative.kt`, the annotation a leaf's native method takes, for a build without it. +pub(crate) const FAST_NATIVE: &str = include_str!("../runtime/FastNative.kt"); /// What the generator writes: the Kotlin file, the Rust module of JNI functions, and the /// R8 and ProGuard rules keeping what that module looks up by name. @@ -72,6 +74,7 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result jlong {{\n\ - \tunsafe {{ rt::load(env, class) }};\n\ + \tunsafe {{ rt::load(env, class, &[\n{fast}\t]) }};\n\ \t0x{contract:016x}u64 as jlong\n\ }}\n\n\ #[unsafe(no_mangle)]\n\ @@ -212,6 +215,7 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result;\n}}\n" + ); + } Ok(Output { names: g.locked, kotlin, @@ -611,6 +623,8 @@ struct Gen<'a> { /// The JVM parameter slots the function being written may still spend on a record's /// scalars. room: usize, + /// The leaves' native methods, as the `JNINativeMethod`s `__contract` registers. + fast: String, } impl<'a> Gen<'a> { @@ -1551,7 +1565,10 @@ impl<'a> Gen<'a> { } let call = format!("crate::{c}_hash({rust_self})"); let body = checked(&call, &format!("{c}_hash"), "ret as jlong"); - g.native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body); + match object && g.abi.leaf_trait(ty, true) { + true => g.leaf_native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body), + false => g.native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body), + } format!( "\t/** Rust's `Hash`, as `Long.hashCode` folds it. */\n\toverride fun hashCode(): kotlin.Int = {open}\n\t\t_Native.{bare}_hash({arg}).hashCode()\n\t}}\n" ) @@ -1618,16 +1635,16 @@ impl<'a> Gen<'a> { )); } if object && t.eq { - self.native( - &format!("{bare}_eq"), - &[("jlong", "Long"), ("jlong", "Long")], - ("jboolean", "Boolean"), - &checked( - &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), - &format!("{c}_eq"), - "ret", - ), + let rust = checked( + &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &format!("{c}_eq"), + "ret", ); + let params = [("jlong", "Long"), ("jlong", "Long")]; + match self.abi.leaf_trait(ty, false) { + true => self.leaf_native(&format!("{bare}_eq"), ¶ms, ("jboolean", "Boolean"), &rust), + false => self.native(&format!("{bare}_eq"), ¶ms, ("jboolean", "Boolean"), &rust), + } let hash = hash_code(self); body.push(format!( "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> _Native.{bare}_eq(a, b) }} }}\n\n{hash}" @@ -2334,6 +2351,18 @@ impl<'a> Gen<'a> { } fn native(&mut self, bare: &str, params: &[(&str, &str)], ret: (&str, &str), body: &str) { + self.declare(bare, params, ret, body, false); + } + + /// [`Self::native`] for a leaf, as [`Abi::leaf`] proves: `@FastNative`, so ART keeps + /// the thread runnable across it rather than switching its state twice, which a call + /// that ends in a bounded number of steps and waits on no lock allows. ART binds such a + /// method by its name only from Android 12, so `__contract` registers it instead. + fn leaf_native(&mut self, bare: &str, params: &[(&str, &str)], ret: (&str, &str), body: &str) { + self.declare(bare, params, ret, body, true); + } + + fn declare(&mut self, bare: &str, params: &[(&str, &str)], ret: (&str, &str), body: &str, fast: bool) { let kt: Vec = params .iter() .enumerate() @@ -2361,7 +2390,11 @@ impl<'a> Gen<'a> { let bare = external.as_str(); let _ = write!( self.natives, - "\t@kotlin.jvm.JvmStatic external fun {bare}({}): {}\n\n", + "{}\t@kotlin.jvm.JvmStatic external fun {bare}({}): {}\n\n", + match fast { + true => "\t@dalvik.annotation.optimization.FastNative\n", + false => "", + }, kt.join(", "), ret.1 ); @@ -2372,11 +2405,23 @@ impl<'a> Gen<'a> { .collect(); let mut all = vec!["env: *mut JNIEnv".to_string(), "_: jclass".to_string()]; all.extend(rust); + let function = format!("Java_{}__1Native_{}", mangle(&self.abi.prefix), mangle(bare)); + // A registered function needs no symbol of its own. + let export = match fast { + true => { + let jvm: String = params.iter().map(|(j, _)| descriptor(j)).collect(); + let _ = writeln!( + self.fast, + "\t\tJNINativeMethod {{ name: c\"{bare}\".as_ptr() as _, signature: c\"({jvm}){}\".as_ptr() as _, fnPtr: {function} as *mut c_void }},", + descriptor(ret.0) + ); + "" + } + false => "#[unsafe(no_mangle)]\npub ", + }; let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"system\" fn Java_{}__1Native_{}({}) -> {} {{\n\tlet _ = env;\n\tunsafe {{\n\t\t{body}\n\t}}\n}}\n\n", - mangle(&self.abi.prefix), - mangle(bare), + "{export}unsafe extern \"system\" fn {function}({}) -> {} {{\n\tlet _ = env;\n\tunsafe {{\n\t\t{body}\n\t}}\n}}\n\n", all.join(", "), ret.0 ); @@ -3083,16 +3128,15 @@ impl<'a> Gen<'a> { _ if returned.is_some() => "rt::new_block(env, ret)".to_string(), _ => ret_expr(abi, &c_ret, &ok), }; - self.native( - &bare, - ¶ms, - (jni, native), - &format!( - "{}\n\t\tlet ret = {};{check}\n\t\t{ret_expr}", - rust_pre.join("\n\t\t"), - rust_call(&[]) - ), + let body = format!( + "{}\n\t\tlet ret = {};{check}\n\t\t{ret_expr}", + rust_pre.join("\n\t\t"), + rust_call(&[]) ); + match abi.leaf(function, owner_ty) { + true => self.leaf_native(&bare, ¶ms, (jni, native), &body), + false => self.native(&bare, ¶ms, (jni, native), &body), + } value = match (&ok, &returned) { (Type::Unit, _) => call, (_, Some(b)) => b.unsigned(&call), diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 1c9f270e6..dbde57737 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1249,7 +1249,8 @@ fn kotlinc(out: &Path, script: &Path, dest: &Path) -> Command { let mut cmd = Command::new("kotlinc"); cmd.args(["-Werror", "-cp"]) .arg(coroutines()) - .arg(out.join("kotlin").join("api.kt")) + // The generated Kotlin, `api.kt` and `FastNative.kt`. + .arg(out.join("kotlin")) .arg(manifest_dir().join("tests").join("api.kt")) .arg(script) .arg(manifest_dir().join("tests").join("protocol.kt")) diff --git a/ffi/tests/fixtures/corpus.rs b/ffi/tests/fixtures/corpus.rs index 4bf988064..faae85f6a 100644 --- a/ffi/tests/fixtures/corpus.rs +++ b/ffi/tests/fixtures/corpus.rs @@ -429,7 +429,8 @@ fn languages(lib: &str, out: &Path, dir: &Path, built: &Built) -> Result<(), (&' Command::new("kotlinc") .args(["-J-Xmx4g", "-Werror", "-cp"]) .arg(super::coroutines()) - .arg(out.join("kotlin").join(format!("{lib}.kt"))) + // The generated Kotlin, `{lib}.kt` and `FastNative.kt`. + .arg(out.join("kotlin")) .arg("-d") .arg(dir.join("kotlin")), )?; diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 9ecc33f18..5298b3853 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -2767,10 +2767,11 @@ fn round_trip(built: &Built, language: &str) { step::write(&main, kotlin(built, calls)); let classes = dir.join("classes"); let generated = built.out.join("kotlin").join(format!("{name}.kt")); + let fast = built.out.join("kotlin").join("FastNative.kt"); let jar = PathBuf::from(super::coroutines()); let step = Step { stamp: &step::stamp(&classes), - reads: &[&main, &generated, &jar], + reads: &[&main, &generated, &fast, &jar], writes: &[&classes], }; step.run( @@ -2779,6 +2780,7 @@ fn round_trip(built: &Built, language: &str) { .args(["-J-Xmx4g", "-Werror", "-cp"]) .arg(&jar) .arg(&generated) + .arg(&fast) .arg(&main) .arg("-d") .arg(&classes), From 84a765e825b553a6cb2f9c926e2885d7f67c0a05 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:13:18 -0800 Subject: [PATCH 4/9] wip: docs, nightly, follow-ups --- .github/workflows/nightly.yml | 15 +++++- bench/ffi/README.md | 73 ++++++++++++++++++++++++-- bench/ffi/report.py | 4 +- ffi/README.md | 12 +++-- ffi/justfile | 8 +-- ffi/src/abi.rs | 6 ++- quest/m1/ffi-speed/android-9-memory.md | 25 +++++++++ quest/m1/ffi-speed/android-jni.md | 32 ----------- quest/m1/ffi-speed/art-throughput.md | 24 +++++++++ 9 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 quest/m1/ffi-speed/android-9-memory.md delete mode 100644 quest/m1/ffi-speed/android-jni.md create mode 100644 quest/m1/ffi-speed/art-throughput.md diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 835fd02a1..d41158b0e 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -370,7 +370,7 @@ jobs: ffi-android: name: Export to Kotlin on Android runs-on: ubuntu-24.04 - timeout-minutes: 60 + timeout-minutes: 90 steps: - name: Checkout @@ -402,6 +402,19 @@ jobs: arch: x86_64 script: nix develop .#android --command just ffi android + # The binding benchmark's Kotlin rows on ART beside HotSpot's, into the run's summary: + # no threshold fails the step. Four cores, for its threaded cases. + - name: Bench on API 35 + uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 + with: + api-level: 35 + arch: x86_64 + cores: 4 + script: nix develop .#android --command just ffi bench-android + + - name: Summarize the bench + run: cat ffi/target/bench-android/report.md >> "$GITHUB_STEP_SUMMARY" + # An image whose kernel pages by 16 KB, as Google Play requires of 64-bit libraries from # 2027-02-01: the library must load there, and its segments must align to that. - name: API 35, 16 KB pages diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 88531c952..ff88fa49e 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -6,6 +6,7 @@ stand-in `just ffi bench` exports, against BoltFFI's numbers below as targets. ```bash nix develop .#bench --command just ffi bench # build, run 3 rounds, and write ffi/target/bench/report.md nix develop .#bench --command just ffi bench-run 5 # rerun what bench-build built, 5 rounds +nix develop .#android --command just ffi bench-android # the Kotlin rows on HotSpot and on the Android device adb reaches ``` `ffi/moq/bench/export.*` time the same cases in each language, `export.rs` the floor @@ -32,7 +33,7 @@ single-threaded case on every push to main and on each PR that can move one. | 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 | -| field | `info.priority`: an `i8` `pub` field read, which no user code reaches, so a leaf in C, Python, and Dart alone | none | +| field | `info.priority`: an `i8` `pub` field read, which no user code reaches, so a leaf in C, Kotlin, Python, and Dart alone | none | | hop call, hop field | Dart's handle call and field read while Rust holds a closure of the isolate | none | | write | `group.write_frame(at, frame)` with one `Timestamp` a group and 100 B, copied into Rust | `write_frame` | | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | @@ -219,8 +220,9 @@ GIL, which dwarfs the handle's own contention. A leaf is a call the export proves runs no code but std's and the glue's, waits on no lock, and calls no binding: a primitive `pub` field of a `Send` object reached without a `Deref`, or a `PartialEq` or `Hash` derived down to primitives ([`Abi::leaf`](../../ffi/src/abi.rs)). -Python keeps the GIL across one, and Dart makes it a leaf call (`isLeaf`) to the C function -itself on the isolate's thread, with no serving thunk, no thread hop, and no `_Arena`. +Python keeps the GIL across one, Dart makes it a leaf call (`isLeaf`) to the C function +itself on the isolate's thread, with no serving thunk, no thread hop, and no `_Arena`, and +Kotlin marks its native method `@FastNative` ([Kotlin on ART](#kotlin-on-art)). The benchmark's u64 getter, `demand.sequence()`, is a method, which the proof cannot see into, so the `field` case reads `track::Info::priority` instead; `group.sequence` is a field too, but reached through `group::Producer`'s `Deref`, which is user code. @@ -263,6 +265,71 @@ round trip, as `rt::call` does more around it: it may look for the event loop ru after the call drains what other threads freed and resumes what the call woke, all of which a leaf skips. A getter that is no leaf, such as `demand.sequence()`, still pays it. +## Kotlin on ART + +`just ffi bench-android` runs `export.kt` on HotSpot on the host and, shrunk by R8 with the +generated rules as an app is, on ART through `dalvikvm` on the Android device adb reaches, +over a library cargo-ndk builds for it, round after round, into one report. Nightly runs it +in the Android job, on its x86_64 API 35 emulator with 4 cores, into the run's summary; the +other languages' rows run on arm64, so read Kotlin's on HotSpot beside ART's from this +report alone. ART has no `java.lang.management`, so its CPU time is the kernel's tick count +in `/proc/self/stat`, at HotSpot's own 10 ms resolution on Linux. + +Measured 2026-11-20 on **fedora**, an AMD Ryzen 9 9950X, through `quest gate`, against moq +`1aa86d5e`, with this change, 3 rounds at a load of 6 to 8, on an Android 15 (API 35) +x86_64 emulator with 4 cores under KVM. OpenJDK 21.0.13 with Kotlin 2.4.21, R8 8.6 from +build-tools 35.0.0. Wall ns per call, the median; the CPU cells are coarse at this load's +tick: + +| call | HotSpot | ART | +|---|---|---| +| handle call | 32 | 41 | +| u64 getter | 23 | 32 | +| field, a leaf | 23 | 25 | +| record return | 56 | 129 | +| object return | 97 | 230 | +| write, 100 B | 224 | 382 | +| ready read | 630 | 1,190 | +| woken read | 1,761 | 3,751 | +| `frame.payload`, 100 B | 64 | 143 | +| throughput, 16 KiB frames | 9,268 MB/s | 652 MB/s | + +1. **A plain JNI call costs ART about 9 ns more than HotSpot.** The handle call and the + getter run the same Kotlin and the same Rust on both. +2. **A leaf is `@FastNative`, which keeps ART's thread runnable across the call instead of + switching its state there and back.** Each leaf's native method takes it, as + `_Native.track_Info_priority_get` does, and `_Native`'s first call registers each through + `RegisterNatives` from the library's one exported `__contract`, since ART binds such a + method by its name only from Android 12. The annotation blocks the collector for the + call, so only a leaf takes it: a call that may wait on a lock, as `is_closed()` waits on + moq's, or call back into Kotlin, could stall every thread's collection behind it. +3. **Throughput falls off from 16 KiB on ART alone**, near 650 MB/s against HotSpot's 6.7 to + 9.3 GB/s, while 100 B and 1 KiB frames stay within 3x. + [ART's throughput](/quest/m1/ffi-speed/art-throughput.md) finds why. + +The spike, 5 rounds of each side alternating on one emulator at a load of 12 to 29, wall ns, +before (main) and after (`@FastNative` on leaves, registered): + +| case | API 35 before | API 35 after | API 30 before | API 30 after | +|---|---|---|---|---| +| field, a leaf | 34.1 | 25.8 | 43.5 | 28.1 | +| handle call, no leaf | 40.3 | 43.6 | 80.3 | 80.8 | +| u64 getter, no leaf | 32.6 | 33.8 | 43.2 | 43.4 | + +Marking the handle call and the getter too, by hand, on API 35 (3 rounds), cost 36.6 against +41.1 and 25.7 against 34.2: the same few ns, which a call the proof cannot bound may not +take. moq's export has 214 leaves, the api fixture's 21. Registration covers only those: +registering all of moq's 6,455 native methods would scan the class's methods once each, and +R8 strips the natives nothing calls, which `RegisterNatives` would then refuse, so the +generated rules keep each `@FastNative` method instead. `kotlin/FastNative.kt` declares +the annotation, which Android's SDK makes public only from API 34, for the JVM and older +SDKs; R8 keeps it on the methods only by the rule naming it. + +`just ffi android` passes, with these rules, on API 24 (Android 7, which ignores the +annotation), 28, 30, and 35. On API 28, `export.kt` itself never finishes, with or without +the annotation: within 100 s its native memory passes 1.5 GB under a 256 MB heap +([Kotlin on Android 9](/quest/m1/ffi-speed/android-9-memory.md)). + ## Swift's inlined calls Measured 2026-11-16 on **nixos**, as above, through `quest gate`, against moq `ffcb7cd9`. diff --git a/bench/ffi/report.py b/bench/ffi/report.py index 96dcd43ec..ff0cff742 100644 --- a/bench/ffi/report.py +++ b/bench/ffi/report.py @@ -249,7 +249,9 @@ def cell(lang, case, i): for i, clock, rate in [(1, "CPU", "MB per CPU second"), (0, "wall", "MB/s")]: print(f"### Calls (ns per call, {clock} time; load average)\n") header = ["call"] + [name for _, name in langs] - print(table(header, [[label] + [cell(lang, case, i) for lang, _ in langs] for case, label in CALLS])) + rows = [[label] + [cell(lang, case, i) for lang, _ in langs] for case, label in CALLS] + # A case no language here runs, as in bench-android's Kotlin alone. + print(table(header, [row for row in rows if any(row[1:])])) print(f"\n### Throughput ({rate}, write plus ready read; load average)\n") header = ["frame"] + [name for _, name in thru] rows = [[LABELS[n]] + [cell(lang, f"mbps:{n}", i) for lang, _ in thru] for n in SIZES] diff --git a/ffi/README.md b/ffi/README.md index a5462979f..5c1745acd 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -43,8 +43,9 @@ its source's URL. A `[replace]` from the source it replaces, which no `[patch]` can carry, is an error. Beside the C header, `include/.hpp` holds the same API in C++, over a header per crate, `kotlin/.kt` holds it in Kotlin, over JNI functions the crate builds with -`--features kotlin`, and `kotlin/.pro` the R8 and ProGuard rules keeping what -those functions look up by name. `python/.py` holds it in Python, over a CPython +`--features kotlin`, beside `kotlin/FastNative.kt`, which a build compiles with it, and +`kotlin/.pro` the R8 and ProGuard rules keeping what those functions look up by +name. `python/.py` holds it in Python, over a CPython extension the crate builds with `--features python`, `swift/.swift` holds it in Swift, over the C header as a clang module, `go/` holds it in Go, a package per crate over the C ABI through cgo, and `dart/.dart` holds it in Dart, over the C ABI through @@ -595,7 +596,12 @@ packages place it in. `.kt` is Kotlin for the JVM and Android over the crate's JNI functions, which `--features kotlin` builds into the same library, loaded by `System.loadLibrary`. It needs -kotlinx-coroutines. Each value crosses JNI as a scalar, a handle as a `Long`, bytes passed +kotlinx-coroutines, and compiles with `FastNative.kt`, ART's `@FastNative` annotation, +which Android's SDK makes public only from API 34. A leaf's native method, a primitive field +read or write or a derived `equals` or `hashCode` the export proves waits on nothing +([`Abi::leaf`](src/abi.rs)), takes it, so ART keeps the thread runnable across the call; +`_Native`'s first call registers each through `RegisterNatives`, since ART binds such a +method by its name only from Android 12, and the rules keep each. Each value crosses JNI as a scalar, a handle as a `Long`, bytes passed 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`, crosses as its scalars, a nested diff --git a/ffi/justfile b/ffi/justfile index a0429c93d..10e37e91a 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -153,7 +153,8 @@ bench-android rounds="3" checkout="": x86_64) target=x86_64-linux-android ;; *) echo "bench-android: the Android shell's toolchain has no target for the device's ABI $abi" >&2; exit 1 ;; esac - # The export merges the device's API, so its Kotlin and libraries serve both. + # The export merges the device's API, so its Kotlin and libraries serve both; the API + # stays staged in target/moq-api, as moq-api leaves it. {{ quote(just_executable()) }} moq-api "$target" {{ quote(checkout) }} {{ quote(just_executable()) }} moq-write {{ quote(checkout) }} out=target/export/moq @@ -189,11 +190,12 @@ bench-android rounds="3" checkout="": } for r in $(seq {{ rounds }}); do run kotlin java -Djava.library.path="$out/target/release" -cp "$dir/bench.jar:$KOTLINX_COROUTINES" ExportKt - run kotlin-art "$adb" shell "ANDROID_DATA=$device dalvikvm -Djava.library.path=$device -cp $device/classes.dex ExportKt" + # dalvikvm interprets on Android 9 unless told to JIT, as an app's runtime does. + run kotlin-art "$adb" shell "ANDROID_DATA=$device dalvikvm -Xusejit:true -Djava.library.path=$device -cp $device/classes.dex ExportKt" done { printf '## Kotlin on HotSpot and ART\n\nART: Android %s (API %s), %s, %s cores.\n\n' \ - "$("$adb" shell getprop ro.build.version.release | tr -d '\r')" "$("$adb" shell getprop ro.build.version.sdk | tr -d '\r')" "$abi" "$("$adb" shell nproc | tr -d '\r')" + "$("$adb" shell getprop ro.build.version.release | tr -d '\r')" "$("$adb" shell getprop ro.build.version.sdk | tr -d '\r')" "$abi" "$("$adb" shell grep -c ^processor /proc/cpuinfo | tr -d '\r')" python3 -I ../bench/ffi/report.py "$raw" } > "$dir/report.md" cat "$dir/report.md" diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index aabb51708..a5af20861 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -1289,8 +1289,10 @@ impl<'a> Abi<'a> { /// Whether a call of `function`, a method of `owner` if it has one, is a leaf: proven to /// run no code but the glue's, its runtime's, and std's, to wait on no lock, and to call - /// into no binding. Python then keeps the GIL across it, and Dart makes it a leaf call, - /// which runs on the isolate's own thread with no thread hop. It is the read or write of a + /// into no binding. Python then keeps the GIL across it, Dart makes it a leaf call, + /// which runs on the isolate's own thread with no thread hop, and Kotlin marks it + /// `@FastNative`, which ART's collector waits on, so its bound keeps a collection from + /// stalling behind it. It is the read or write of a /// primitive `pub` field of a `Send` object reached without a `Deref`, which no object /// that is not `Send` may lend. /// diff --git a/quest/m1/ffi-speed/android-9-memory.md b/quest/m1/ffi-speed/android-9-memory.md new file mode 100644 index 000000000..0583a9edb --- /dev/null +++ b/quest/m1/ffi-speed/android-9-memory.md @@ -0,0 +1,25 @@ +# [M] Find what grows the Kotlin benchmark's native memory on Android 9 + +## Goal + +`export.kt`, [the binding benchmark](/bench/ffi/README.md#kotlin-on-art)'s Kotlin, finishes +on an Android 9 (API 28) emulator as it does on API 30 and 35, or the binding's part in what +stops it is fixed, with a check that would have caught it. + +## Plan + +Found by the ART measurement on 2026-11-20. On an x86_64 API 28 emulator with 2 GB of RAM, +`dalvikvm -Xusejit:true -Xmx256m` running the R8-shrunk `export.kt` grows its resident +memory from 632 MB at 20 s to 1.5 GB at 100 s, past the heap's cap, so the growth is native; +the device then thrashes and its system server restarts. It does so with and without +`@FastNative`, and on main's generator too. `just ffi android`, the api fixture, passes +there. Bisect by case first: the bench's loops make and close millions of handles, frames, +and timestamps, so a leak per call, such as a JNI reference ART 9 keeps or a handle freed +only by the collector, is the first suspect. `just ffi bench-android` runs it with +`ANDROID_HOME` naming an SDK with the API 28 image. + +Public API: none expected. + +## Hosts + +- linux - the Android emulator needs KVM diff --git a/quest/m1/ffi-speed/android-jni.md b/quest/m1/ffi-speed/android-jni.md deleted file mode 100644 index e5e877cc9..000000000 --- a/quest/m1/ffi-speed/android-jni.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] Measure Kotlin on ART, then try @FastNative - -## Goal - -[The binding benchmark](/bench/ffi/README.md) reports the Kotlin rows on ART, on the -Android emulator the nightly already runs, beside HotSpot's. Then a spike registers the -JNI functions with `RegisterNatives` from the binding's first-call `load()` and marks -short, non-blocking calls `@FastNative`; it lands only where ART measures it faster, and -otherwise its numbers go in the PR and the README. - -## Plan - -Found by the 2026-11-18 language audit. Every Kotlin number today is HotSpot's: the getter -costs 24 ns wall against BoltFFI's 5.8. Android lists `@FastNative` near 35 ns against a -regular JNI call ([JNI tips](https://developer.android.com/ndk/guides/jni-tips)). - -- `@FastNative` needs `RegisterNatives` on Android 8 to 11, where the glue uses `Java_` - name lookup today (`ffi/src/kotlin.rs:2280`). Today `load()` is reached only through the - exported `Java_..._1Native__1_1contract`, and the library has no `JNI_OnLoad`, so - registration needs a native entry point that runs before any registered method. - Recommended: keep that one contract export, which registers every other method on its - first call, so the other `Java_` symbols drop; the spike says if `JNI_OnLoad` is simpler. -- It is public SDK API only from API 34, so the JVM build needs a stub annotation. -- It blocks the GC for the call, so it suits only calls that never block or call back. -- `@CriticalNative` does not fit: it has no `JNIEnv` to throw a failed check, and - Android 7, which moq's API 24 floor includes, does not support it. - -Public API: none. - -## Hosts - -- linux - the Android emulator needs KVM diff --git a/quest/m1/ffi-speed/art-throughput.md b/quest/m1/ffi-speed/art-throughput.md new file mode 100644 index 000000000..9e214aaaa --- /dev/null +++ b/quest/m1/ffi-speed/art-throughput.md @@ -0,0 +1,24 @@ +# [M] Find why ART's throughput falls off from 16 KiB frames + +## Goal + +[The binding benchmark](/bench/ffi/README.md#kotlin-on-art)'s Kotlin throughput on ART at +16 KiB to 1 MiB frames is explained by a profile, and cut where the binding causes it, with +before and after numbers in the README. + +## Plan + +Found by the ART measurement on 2026-11-20: on one API 35 x86_64 emulator, ART moves 100 B +and 1 KiB frames within 3x of HotSpot (52 against 149 MB/s, 411 against 1,195), but from +16 KiB on it holds near 650 MB/s against HotSpot's 6.7 to 9.3 GB/s. Each frame is written +from a `ByteArray` and read back as `Bytes` whose first byte is touched, so the suspects are +the copy into Rust (`GetByteArrayRegion`, which ART may route through a pinned or copied +array), a copy out on the read, and the emulator's memory bandwidth, which a native C +memcpy loop on the same emulator separates from the binding. Profile with `simpleperf` on +the emulator before changing anything. + +Public API: none expected. + +## Hosts + +- linux - the Android emulator needs KVM From f45e2783463b522315704d84ba9d820787bdab7a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:20:27 -0800 Subject: [PATCH 5/9] ffi: surface files with FastNative.kt --- ffi/surface/api/kotlin.kt | 4 ++++ ffi/surface/moq/kotlin.kt | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/ffi/surface/api/kotlin.kt b/ffi/surface/api/kotlin.kt index e55948964..016188ffe 100644 --- a/ffi/surface/api/kotlin.kt +++ b/ffi/surface/api/kotlin.kt @@ -1,3 +1,7 @@ +// FastNative.kt +package dalvik.annotation.optimization +@Retention(AnnotationRetention.BINARY) @Target(AnnotationTarget.FUNCTION) annotation class FastNative + // api.kt fun interface Waker { fun wake() diff --git a/ffi/surface/moq/kotlin.kt b/ffi/surface/moq/kotlin.kt index 9267adda9..4b563bec7 100644 --- a/ffi/surface/moq/kotlin.kt +++ b/ffi/surface/moq/kotlin.kt @@ -1,3 +1,7 @@ +// FastNative.kt +package dalvik.annotation.optimization +@Retention(AnnotationRetention.BINARY) @Target(AnnotationTarget.FUNCTION) annotation class FastNative + // moq.kt fun interface Waker { fun wake() From bbb310e0ef5c91137a6399b51aef7ff7df2682c3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:28:37 -0800 Subject: [PATCH 6/9] ffi: api_android checks R8 keeps every leaf's @FastNative --- ffi/tests/fixtures.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index dbde57737..9a7bc13d5 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1115,6 +1115,20 @@ fn api_android() { usage.lines().any(|line| line.starts_with("api.")), "R8 stripped none of the generated Kotlin:\n{usage}" ); + // And keeps every leaf's `@FastNative`, which ART reads off the dex, and the method + // itself, which `__contract` registers. + let marked = std::fs::read_to_string(out.join("kotlin").join("api.kt")) + .unwrap() + .matches("@dalvik.annotation.optimization.FastNative\n") + .count(); + assert!(marked > 0, "the api fixture has no leaf to mark"); + let dump = run(Command::new(newest(&sdk.join("build-tools")).join("dexdump")) + .arg("-a") + .arg(dex.join("classes.dex"))); + let kept = String::from_utf8_lossy(&dump.stdout) + .matches("VISIBILITY_BUILD Ldalvik/annotation/optimization/FastNative;") + .count(); + assert_eq!(kept, marked, "R8 kept {kept} of the {marked} @FastNative methods"); run(Command::new(&adb) .args(["shell", "rm", "-rf", DEVICE, "&&", "mkdir", "-p"]) From a9849abf0d1578cfafd8771912e8dab1a9d0133d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:50:27 -0800 Subject: [PATCH 7/9] ffi(kotlin): @FastNative eq and hash only when bounded by size; bench-android samples load on macOS; document FastNative.kt's place --- bench/ffi/README.md | 8 ++++++-- ffi/README.md | 15 +++++++++------ ffi/justfile | 11 +++++++++-- ffi/runtime/FastNative.kt | 7 ++++--- ffi/src/kotlin.rs | 5 +++-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/bench/ffi/README.md b/bench/ffi/README.md index befc56b7c..020d226ef 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -318,12 +318,16 @@ before (main) and after (`@FastNative` on leaves, registered): Marking the handle call and the getter too, by hand, on API 35 (3 rounds), cost 36.6 against 41.1 and 25.7 against 34.2: the same few ns, which a call the proof cannot bound may not -take. moq's export has 214 leaves, the api fixture's 21. Registration covers only those: +take. moq's export has 200 leaves, the api fixture's 17. Registration covers only those: registering all of moq's 6,455 native methods would scan the class's methods once each, and R8 strips the natives nothing calls, which `RegisterNatives` would then refuse, so the generated rules keep each `@FastNative` method instead. `kotlin/FastNative.kt` declares the annotation, which Android's SDK makes public only from API 34, for the JVM and older -SDKs; R8 keeps it on the methods only by the rule naming it. +SDKs. Compiled as a program class, R8 keeps it on the methods only by the rule naming it; +against android.jar's own class, R8 keeps it with no rule, so a build against SDK 34 or +later leaves the file out, as one of two exports in an app must, since D8 refuses a class +defined twice. Only leaves whose `eq` and `hash` walk no value of a caller's size are +marked, as Dart's leaf calls are. `just ffi android` passes, with these rules, on API 24 (Android 7, which ignores the annotation), 28, 30, and 35. On API 28, `export.kt` itself never finishes, with or without diff --git a/ffi/README.md b/ffi/README.md index c961c7768..920ade752 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -45,7 +45,7 @@ its source's URL. A `[replace]` from the source it replaces, which no `[patch]` can carry, is an error. Beside the C header, `include/.hpp` holds the same API in C++, over a header per crate, `kotlin/.kt` holds it in Kotlin, over JNI functions the crate builds with -`--features kotlin`, beside `kotlin/FastNative.kt`, which a build compiles with it, and +`--features kotlin`, beside `kotlin/FastNative.kt`, an annotation a build may need with it, and `kotlin/.pro` the R8 and ProGuard rules keeping what those functions look up by name. `python/.py` holds it in Python, over a CPython extension the crate builds with `--features python`, `swift/.swift` holds it in @@ -600,12 +600,15 @@ packages place it in. `.kt` is Kotlin for the JVM and Android over the crate's JNI functions, which `--features kotlin` builds into the same library, loaded by `System.loadLibrary`. It needs -kotlinx-coroutines, and compiles with `FastNative.kt`, ART's `@FastNative` annotation, -which Android's SDK makes public only from API 34. A leaf's native method, a primitive field -read or write or a derived `equals` or `hashCode` the export proves waits on nothing -([`Abi::leaf`](src/abi.rs)), takes it, so ART keeps the thread runnable across the call; +kotlinx-coroutines. A leaf's native method, a primitive field read or write or a derived +`equals` or `hashCode` the export proves waits on nothing and walks no value of a size the +caller sets ([`Abi::leaf`](src/abi.rs)), takes ART's `@FastNative`, so ART keeps the thread runnable across the call; `_Native`'s first call registers each through `RegisterNatives`, since ART binds such a -method by its name only from Android 12, and the rules keep each. Each value crosses JNI as a scalar, a handle as a `Long`, bytes passed +method by its name only from Android 12, and the rules keep each. `FastNative.kt` +declares the annotation for a build whose classpath lacks it: the JVM, or an Android SDK +below API 34, where it became public. An Android build against SDK 34 or later leaves the +file out, as android.jar's own class serves, and so does every build after its first of +two exports in one app, whose two copies D8 refuses as one class defined twice. Each value crosses JNI as a scalar, a handle as a `Long`, bytes passed 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`, crosses as its scalars, a nested diff --git a/ffi/justfile b/ffi/justfile index 967b63d1c..161d2e58b 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -178,12 +178,19 @@ bench-android rounds="3" checkout="": # suffixed by the host's load average over the run. raw=$dir/raw.tsv : > "$raw" + load() { + if [ "{{ os() }}" = macos ]; then + sysctl -n vm.loadavg | awk '{ print $2 }' + else + awk '{ print $1 }' /proc/loadavg + fi + } run() { local lang=$1 before after shift - before=$(awk '{ print $1 }' /proc/loadavg) + before=$(load) "$@" | tr -d '\r' > "$dir/run.tsv" - after=$(awk '{ print $1 }' /proc/loadavg) + after=$(load) awk -v lang="$lang" -v round="$r" -v loadavg="$(echo "$before $after" | awk '{ print ($1 + $2) / 2 }')" -F '\t' \ 'NF == 3 { print lang "\t" round "\t" $1 "\t" $2 "\t" $3 "\t" loadavg; next } { print "bench-android: " lang ": unexpected line: " $0 > "/dev/stderr"; exit 1 }' \ "$dir/run.tsv" >> "$raw" diff --git a/ffi/runtime/FastNative.kt b/ffi/runtime/FastNative.kt index 5303a76e5..32ddee546 100644 --- a/ffi/runtime/FastNative.kt +++ b/ffi/runtime/FastNative.kt @@ -1,7 +1,8 @@ // Generated by rust2-ffi: ART's `@FastNative`, which Android's SDK makes public only from -// API 34, for a build against an older SDK or the JVM's. ART reads it off the dex by its -// name, Android 7 and the JVM ignore it, and on a device the boot class path's own class -// shadows this one. Do not edit. +// API 34, for a build whose classpath lacks it: the JVM's, or an older SDK's. A build against +// SDK 34 or later leaves this file out, as does one compiling a second export, whose copy +// would define the class twice. ART reads it off the dex by its name, Android 7 and the JVM +// ignore it, and on a device the boot class path's own class shadows this one. Do not edit. package dalvik.annotation.optimization diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 26a229f9f..1eb33f9f7 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1579,7 +1579,8 @@ impl<'a> Gen<'a> { } let call = format!("crate::{c}_hash({rust_self})"); let body = checked(&call, &format!("{c}_hash"), "ret as jlong"); - match object && g.abi.leaf_trait(ty, true) { + // Bounded too, so the collector never waits on a walk the value's size sets. + match object && g.abi.leaf_trait(ty, true) && t.leaf_sized { true => g.leaf_native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body), false => g.native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body), } @@ -1655,7 +1656,7 @@ impl<'a> Gen<'a> { "ret", ); let params = [("jlong", "Long"), ("jlong", "Long")]; - match self.abi.leaf_trait(ty, false) { + match self.abi.leaf_trait(ty, false) && t.leaf_sized { true => self.leaf_native(&format!("{bare}_eq"), ¶ms, ("jboolean", "Boolean"), &rust), false => self.native(&format!("{bare}_eq"), ¶ms, ("jboolean", "Boolean"), &rust), } From 2ea42c3e4d5e3cd0499ba2e5ce676ea42adf1090 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 10:52:28 -0800 Subject: [PATCH 8/9] ffi(kotlin): a lacking _Native throws from __contract; no @FastNative past a lend cycle; bench-android runs last A registration failure leaves the JVM's NoSuchMethodError pending instead of aborting, so a Kotlin file from another build still throws. A leaf's handle whose lender chain a cycle of lending methods makes unbounded is no longer a leaf. The nightly runs the Android bench after the 16 KB pages gate. The api fixture's Kotlin cache reads FastNative.kt, and the README lists bench-android. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 27 +++++----- ffi/README.md | 1 + ffi/runtime/jni.rs | 13 +++-- ffi/src/abi.rs | 71 ++++++++++++++++++++++++-- ffi/src/kotlin.rs | 5 +- ffi/tests/fixtures.rs | 33 ++++++++++++ quest/m1/ffi-speed/android-9-memory.md | 5 +- 7 files changed, 131 insertions(+), 24 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 3e16d177f..5f36536c9 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -411,19 +411,6 @@ jobs: arch: x86 script: nix develop .#android --command just ffi android - # The binding benchmark's Kotlin rows on ART beside HotSpot's, into the run's summary: - # no threshold fails the step. Four cores, for its threaded cases. - - name: Bench on API 35 - uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 - with: - api-level: 35 - arch: x86_64 - cores: 4 - script: nix develop .#android --command just ffi bench-android - - - name: Summarize the bench - run: cat ffi/target/bench-android/report.md >> "$GITHUB_STEP_SUMMARY" - # An image whose kernel pages by 16 KB, as Google Play requires of 64-bit libraries from # 2027-02-01: the library must load there, and its segments must align to that. - name: API 35, 16 KB pages @@ -436,6 +423,20 @@ jobs: test "$($ANDROID_HOME/platform-tools/adb shell getconf PAGE_SIZE | tr -d '\r')" = 16384 nix develop .#android --command just ffi android + # The binding benchmark's Kotlin rows on ART beside HotSpot's, into the run's summary: + # no threshold fails the step. Four cores, for its threaded cases. Last, so a failed + # measurement can't skip a correctness gate above. + - name: Bench on API 35 + uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 + with: + api-level: 35 + arch: x86_64 + cores: 4 + script: nix develop .#android --command just ffi bench-android + + - name: Summarize the bench + run: cat ffi/target/bench-android/report.md >> "$GITHUB_STEP_SUMMARY" + ffi-bench: name: Bench the bindings runs-on: ubuntu-24.04-arm diff --git a/ffi/README.md b/ffi/README.md index 920ade752..5b6d31c97 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1552,6 +1552,7 @@ just ffi corpus-update # take corpus/crates.txt afresh from crates.io's top cra just ffi protocol # run longer protocol scripts from more seeds through the C ABI and every binding, as the nightly does nix develop .#bench --command just ffi browsers # api's TypeScript in headless Chromium, Firefox, and on Linux WebKit, lowering `using` three ways for WebKit, as the nightly runs it nix develop .#android --command just ffi android # the Kotlin test, shrunk by R8, under CheckJNI on the Android device adb reaches +nix develop .#android --command just ffi bench-android # the benchmark's Kotlin rows on HotSpot and, shrunk by R8, on ART on the Android device adb reaches nix develop .#bench --command just ffi bench # the binding benchmark: bench/ffi/README.md's cases from Rust, and through the export from C, C++, Kotlin, Python, Swift, Go, and Dart nix develop .#bench --command just ffi codspeed # after bench-build, each single-threaded case once under CodSpeed's CPU simulation, as the CodSpeed workflow runs it nix develop .#sanitize --command just ffi sanitize # api's C and C++ scripts under ASan with UBSan, TSan, and Valgrind, its Swift under TSan, its Go under the race detector and cgocheck2, its Python looped on a debug CPython, and its C ABI under Miri diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 2b68d81cc..fd8bbd1f7 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -31,19 +31,25 @@ static CLASS: OnceLock = OnceLock::new(); /// from Android 12. The library defines no `JNI_OnLoad`, so the crate it exports may define /// its own. /// +/// Returns false, with the JVM's `NoSuchMethodError` pending, when `class` lacks one of +/// `natives`: a Kotlin file from another build, or a shrinker dropping a method the rules +/// keep. The caller returns at once, so the class's initializer throws it. +/// /// # Safety /// `env` is this thread's, `class` the native class, and each of `natives` a function of /// the signature it names. -pub unsafe fn load(env: *mut JNIEnv, class: jclass, natives: &[JNINativeMethod]) { +pub unsafe fn load(env: *mut JNIEnv, class: jclass, natives: &[JNINativeMethod]) -> bool { // SAFETY: `env` is this thread's, as the caller's contract says. unsafe { let mut vm: *mut JavaVM = std::ptr::null_mut(); if ((**env).v1_2.GetJavaVM)(env, &mut vm) != JNI_OK { fail(env, "load", "the JVM is missing"); } - // The rules keep each, so a shrunk app lacking one is a build's mistake. if !natives.is_empty() && ((**env).v1_2.RegisterNatives)(env, class, natives.as_ptr(), natives.len() as jint) != JNI_OK { - fail(env, "load", "a native method to register is missing"); + if !((**env).v1_2.ExceptionCheck)(env) { + fail(env, "load", "registering the native methods failed with no exception"); + } + return false; } let _ = VM.set(vm as usize); CLASS.get_or_init(|| ((**env).v1_2.NewGlobalRef)(env, class) as usize); @@ -56,6 +62,7 @@ pub unsafe fn load(env: *mut JNIEnv, class: jclass, natives: &[JNINativeMethod]) } ((**env).v1_2.DeleteLocalRef)(env, buffer); } + true } /// The class holding the Kotlin functions Rust calls. diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index a5af20861..05da02f98 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -372,6 +372,49 @@ fn affine(api: &Api) -> Vec { } } +/// Whether a handle of each object may be lent through a chain of lenders as long as the +/// caller makes it, as by calling a `view(&self) -> &Self` on each handle it returned: a +/// shared borrow of it walks that chain, so its cost has no bound. True of each object a +/// cycle of lending methods reaches; any other's chain is at most one handle per object. +fn chained(api: &Api) -> Vec { + let lent: Vec> = (api.objects.iter()) + .map(|object| { + let mut lent = Vec::new(); + for f in object.methods.iter().filter(|f| f.lends) { + api.handles(&f.ok, &mut lent, &mut Vec::new()); + } + lent + }) + .collect(); + past_cycles(&lent) +} + +/// Whether a cycle of `edges`, each node's successors, reaches each node. +fn past_cycles(edges: &[Vec]) -> Vec { + let n = edges.len(); + // The nodes reachable in one edge or more from `from`. + let reach = |from: usize| { + let mut seen = vec![false; n]; + let mut stack = edges[from].clone(); + while let Some(j) = stack.pop() { + if !std::mem::replace(&mut seen[j], true) { + stack.extend(&edges[j]); + } + } + seen + }; + let mut past = vec![false; n]; + for i in 0..n { + let seen = reach(i); + if seen[i] { + for (j, seen) in seen.into_iter().enumerate() { + past[j] |= seen; + } + } + } + past +} + /// `ty` as a clone of each borrow it holds. fn owned(ty: &Type) -> Type { match ty { @@ -413,6 +456,8 @@ pub(crate) struct Abi<'a> { pub(crate) held: Vec, /// Whether each object is not `Send` or may be lent from one: see [`affine`]. affine: Vec, + /// Whether each object's chain of lenders has no bound: see [`chained`]. + chained: Vec, /// Every C name claimed. claims: Vec, /// The C name of each item whose own clashed. @@ -435,6 +480,7 @@ impl<'a> Abi<'a> { checked: checked(api), held: held(api), affine: affine(api), + chained: chained(api), claims: Vec::new(), renames: HashMap::new(), clashes: Vec::new(), @@ -498,6 +544,7 @@ impl<'a> Abi<'a> { checked: self.checked.clone(), held: self.held.clone(), affine: self.affine.clone(), + chained: self.chained.clone(), } } @@ -1303,7 +1350,8 @@ impl<'a> Abi<'a> { /// Its value is `Send`, so its borrow runs no `Thread::own`, whose drain would drop what /// other threads queued of the thread's values that are not `Send`, by any `Drop`; a /// handle lent from another borrows that one too, so no owner up its chain of lenders - /// may be other than `Send` either. A field + /// may be other than `Send` either, and that chain is no longer than the export's objects + /// are many, as no cycle of lending methods reaches the object. A field /// reached through `Deref`, of an enum, or of anything but a primitive would run user /// code to read, clone, or drop. So it ends in a bounded number of steps whoever holds /// the GIL, and since it runs no Python, nothing in it needs the GIL either. @@ -1324,15 +1372,21 @@ impl<'a> Abi<'a> { Access::Field { set: true, .. } => matches!(&function.params[..], [param] if primitive(¶m.ty)), _ => false, }; - field && !self.affine[*i] && function.derefs == 0 && function.err.is_none() && !function.future + field + && !self.affine[*i] + && !self.chained[*i] + && function.derefs == 0 + && function.err.is_none() + && !function.future } /// Whether `ty`'s `_eq`, or with `hash` its `_hash`, is a leaf, as [`Self::leaf`] /// says: its impl is derived or std's down to primitives, and it borrows only the handle - /// of a `Send` object no other that is not `Send` may lend, or as a value, none. + /// of a `Send` object no other that is not `Send` may lend, through a chain of lenders + /// with a bound, or as a value, none. pub(crate) fn leaf_trait(&self, ty: &Type, hash: bool) -> bool { let (traits, handles) = match ty { - Type::Object(i) => (self.api.objects[*i].traits, self.affine[*i]), + Type::Object(i) => (self.api.objects[*i].traits, self.affine[*i] || self.chained[*i]), Type::Record(i) => (self.api.records[*i].traits, self.holds_handles(ty)), Type::Enum(i) => (self.api.enums[*i].traits, self.holds_handles(ty)), _ => return false, @@ -2142,7 +2196,14 @@ pub(crate) fn dyn_wrap(form: Dyn, expr: &str) -> String { #[cfg(test)] mod tests { - use super::{apart, shortest, unique}; + use super::{apart, past_cycles, shortest, unique}; + + #[test] + fn past_cycles_marks_what_a_cycle_reaches() { + // 0 lends itself and 1; 2 lends 3 and 3 lends 2, which lends 4; 5 lends 6. + let edges = [vec![0, 1], vec![], vec![3, 4], vec![2], vec![], vec![6], vec![]]; + assert_eq!(past_cycles(&edges), [true, true, true, true, true, false, false]); + } /// Each item's [`apart`] name, by its path's segments. fn names(items: &[&[&str]]) -> Vec { diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 1eb33f9f7..c9781b938 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -183,7 +183,10 @@ pub(crate) fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result jlong {{\n\ - \tunsafe {{ rt::load(env, class, &[\n{fast}\t]) }};\n\ + \tif !unsafe {{ rt::load(env, class, &[\n{fast}\t]) }} {{\n\ + \t\t// The pending exception throws in place of the contract, which Kotlin never reads.\n\ + \t\treturn 0;\n\ + \t}}\n\ \t0x{contract:016x}u64 as jlong\n\ }}\n\n\ #[unsafe(no_mangle)]\n\ diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 05d54e450..c86501fe8 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1082,6 +1082,38 @@ fn api_kotlin() { stderr.contains("api.kt and the library are from different builds"), "a mismatched contract printed {stderr}" ); + + // A _Native from another build lacking the leaves `__contract` registers throws the + // JVM's NoSuchMethodError from its initializer, rather than aborting. + let lacking = scratch().join("api").join("kotlin-lacking"); + let source = lacking.join("src").join("_Native.java"); + step::write( + &source, + "package api;\n\npublic final class _Native {\n\tstatic {\n\t\tSystem.loadLibrary(\"api\");\n\t\t__contract();\n\t}\n\n\tstatic native long __contract();\n\n\tpublic static void main(String[] args) {}\n}\n", + ); + run(Command::new("javac") + .arg("-d") + .arg(lacking.join("classes")) + .arg(&source)); + let output = no_core( + Command::new("java") + .arg("-Xcheck:jni") + .arg(format!("-Djava.library.path={}", api.lib.display())) + .arg("-cp") + .arg(lacking.join("classes")) + .arg("api._Native"), + ) + .output() + .unwrap(); + failed(&output, "a _Native lacking its leaves"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.code().is_some() + && stderr.contains("java.lang.NoSuchMethodError") + && stderr.contains("at api._Native.__contract(Native Method)"), + "a _Native lacking its leaves exited with {} and printed {stderr}", + output.status + ); } /// The same script on Android: the classes, shrunk by R8 into an app's dex with only the @@ -1356,6 +1388,7 @@ fn kotlin_classes(api: &Api) -> PathBuf { stamp: &step::stamp(&classes), reads: &[ &api.out.join("kotlin").join("api.kt"), + &api.out.join("kotlin").join("FastNative.kt"), &manifest_dir().join("tests").join("api.kt"), &script, &manifest_dir().join("tests").join("protocol.kt"), diff --git a/quest/m1/ffi-speed/android-9-memory.md b/quest/m1/ffi-speed/android-9-memory.md index 0583a9edb..839496cb4 100644 --- a/quest/m1/ffi-speed/android-9-memory.md +++ b/quest/m1/ffi-speed/android-9-memory.md @@ -15,8 +15,9 @@ the device then thrashes and its system server restarts. It does so with and wit `@FastNative`, and on main's generator too. `just ffi android`, the api fixture, passes there. Bisect by case first: the bench's loops make and close millions of handles, frames, and timestamps, so a leak per call, such as a JNI reference ART 9 keeps or a handle freed -only by the collector, is the first suspect. `just ffi bench-android` runs it with -`ANDROID_HOME` naming an SDK with the API 28 image. +only by the collector, is the first suspect. `just ffi bench-android` runs it on the device +adb reaches, so start an API 28 emulator and make it the only device, or name it in +`ANDROID_SERIAL`. Public API: none expected. From dfe0291d983e885452c82c17b1266bab00f5c132 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 11:11:16 -0800 Subject: [PATCH 9/9] ffi(kotlin): look up __refused and __invalid while loading, before a @FastNative call can need them Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/jni.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index fd8bbd1f7..b1c34560b 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -62,6 +62,11 @@ pub unsafe fn load(env: *mut JNIEnv, class: jclass, natives: &[JNINativeMethod]) } ((**env).v1_2.DeleteLocalRef)(env, buffer); } + // Looked up now, before a leaf can run: a `@FastNative` call refusing its borrow or an + // argument must not wait on another thread's lookup, which a collection pending on the + // leaf would stall. + method(env, &REFUSED, REFUSED_METHOD.0, REFUSED_METHOD.1); + method(env, &INVALID, INVALID_METHOD.0, INVALID_METHOD.1); true } @@ -125,6 +130,7 @@ pub fn fail(env: *mut JNIEnv, what: &str, msg: &str) -> ! { } static REFUSED: OnceLock = OnceLock::new(); +const REFUSED_METHOD: (&CStr, &CStr) = (c"__refused", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); /// Throw the `_Native.__Refused`, an `IllegalStateException`, a call of `what` raises once /// its check failed with `status`, returning a zeroed value Kotlin never reads, as JNI's @@ -137,7 +143,7 @@ pub fn refuse(env: *mut JNIEnv, status: i32, what: &str) -> R { None => format!("{what}: an unknown status {status}"), }; let message = std::ffi::CString::new(message).expect("no NUL in a message"); - let id = method(env, &REFUSED, c"__refused", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); + let id = method(env, &REFUSED, REFUSED_METHOD.0, REFUSED_METHOD.1); // SAFETY: `env` is this thread's, and `__refused` takes the message. unsafe { let text = ((**env).v1_2.NewStringUTF)(env, message.as_ptr()); @@ -155,13 +161,14 @@ pub fn refuse(env: *mut JNIEnv, status: i32, what: &str) -> R { } static INVALID: OnceLock = OnceLock::new(); +const INVALID_METHOD: (&CStr, &CStr) = (c"__invalid", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); /// Throw the `_Native.__Invalid`, an `IllegalArgumentException`, an argument holding a /// value its Rust type cannot raises, which the native method then returns with, as /// [`raised`] does. Its own class tells Kotlin the call moved nothing. #[cold] fn invalid(env: *mut JNIEnv, message: &CStr) { - let id = method(env, &INVALID, c"__invalid", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); + let id = method(env, &INVALID, INVALID_METHOD.0, INVALID_METHOD.1); // SAFETY: `env` is this thread's, and `__invalid` takes the message. unsafe { let text = ((**env).v1_2.NewStringUTF)(env, message.as_ptr());