Skip to content

perf(ffi): copy Kotlin's returned bytes up to 1 KiB in the getter's crossing - #240

Merged
kixelated merged 4 commits into
mainfrom
quest/m1/ffi-export/kotlin-reads
Oct 30, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m1/ffi-export/kotlin-reads

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m1/ffi-export/kotlin-reads.md, with the option the maintainer chose: copy short payloads, lend long ones.

Consumer: moq's Kotlin bindings, whose frame payloads (FrameFrame.payload, ContainerFrame.payload) cross on every read.

Changes

  • Bytes Rust returns up to 1 KiB (COPIED in ffi/runtime/jni.rs) cross as a byte[] copy made in the getter's own crossing, with the Rust bytes freed there and nothing registered with the cleaner. Longer ones cross as their owner in a long[] and are lent as a direct ByteBuffer as before.
  • In an encoded result (a record, a list, a future's output), a short payload is written inline after its length, and a long one after a LENT marker (-2) and its owner.
  • close() stays idempotent on both paths. A copied Bytes frees nothing when closed and refuses reads after it. A lent one frees once, and the GC backstop still frees it if it was never closed.

Measurements

Host load was 30-40 throughout, so the numbers are indicative. Every table is interleaved runs.

Threshold sweep. Each value is ns per payload read of one frame, with the payload always lent or always copied (6 rounds, median):

size (B) lent copied
256 204 68
512 209 124
1024 206 184
1536 210 266
2048 212 428
4096 207 834
16384 202 2905

The crossover falls between 1 KiB and 1.5 KiB, so the threshold is 1 KiB. A quieter host copies faster, which would move the crossover up.

Folding the copy into the getter's crossing. Each value is ns per read (8 rounds, median), compared with the two-crossing prototype (the getter, then a call that copies):

size (B) folded two crossings
256 104 131
512 120 165
1024 270 288
4096 (lent) 288 240

Folding is cheaper wherever the payload is copied. A lent payload costs one small long[] more, about 45 ns at this load, but only above 1 KiB, where the copy it replaces would have cost more.

just ffi bench (10 rounds, median, min in parentheses, ns):

build frame written and read 100 B payload read
main 825 (623) 187 (158)
this PR 922 (628) 84 (71)
C 519 (462) 18 (16)

This PR doesn't touch the frame read path, and its minimums match main's; the medians differ by noise.

Tests

api.kt's owned case (the GC test) now covers both paths:

  • payloads of 1023, 1024 and 1025 B round-trip through a record;
  • Rust's bytes of 1023 and 1024 B are freed at the call, and 1025 B are freed only when closed;
  • each closes twice and frees once, and refuses a read once closed;
  • the GC backstop now uses lent (1025 B) bytes, since copied ones never reach the cleaner.

token_bytes takes a length, and only api.kt calls it. The C and C++ fixtures and the transcript are unchanged.

Public API

No signature changes. Bytes.buffer is now a heap ByteBuffer for payloads up to 1 KiB, and still a direct one above that. The unreleased MIGRATE entry and the README describe the copy-or-lend split. Inside the generated Native object, owned() and LENT are new.

Checks

just check, just test, just ffi test and just ffi moq pass. The last two run in the bench shell, which has kotlinc.

Review

Round 2, quest review with Codex gpt-6.1-sol at medium effort, on head 77ad917 against origin/main f78c588: no findings.

Round 1, on head 8e9e093, found one issue: the cast to LongArray in rt.kt could be shadowed by an exported class of that name. Fixed in 77ad917 by qualifying it as kotlin.LongArray.

Merge of main at 4aa76e694042cff57d362a0b0727be65bb85203e: the only conflict was this PR deleting its finished quest file while #249 updated that file's links; resolved by deleting it. Nothing else changed since the reviewed head 77ad9171, so this merge is a recorded skip, too trivial to review.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 3 commits October 29, 2026 18:21
…rossing

A payload up to 1 KiB crosses as a byte[] copy, its Rust bytes freed in the same
crossing, with no cleaner registration; a longer one is lent as before. The threshold is
the measured crossover against NewDirectByteBuffer's upcall. An encoded result writes a
short payload inline and a long one after a LENT marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Kotlin payload reads at JNI's cost perf(ffi): copy Kotlin's returned bytes up to 1 KiB in the getter's crossing Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 02:24
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T02:25:50.143283Z 77ad917 Draft marked ready
🔒 Security Review ✅ Completed 2026-10-30T02:27:21.568793Z 77ad917 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 77ad917

No actionable bugs found. Copying short payloads in the getter's existing JNI crossing is a focused tradeoff for the named Kotlin frame consumer, while retaining lending for larger payloads avoids making their cost scale with a full copy. This reuses the existing Bytes ownership model rather than adding shared-buffer lifetime rules.

The direct-return and encoded paths agree on copied, lent and absent values; short buffers are copied before their Rust owner is freed, borrowed long buffers acquire ownership before lending, and the generated Kotlin/native contract includes the changed encoding. Close and cleaner handling remain coherent. The 1023/1024/1025-byte tests exercise both sides of the threshold and exact drop counts.

Nonblocking coverage suggestion: add one mixed encoded list or async result containing None, empty, short and long bytes, so marker decoding and ownership are checked together rather than only through scalar and record returns.

Verification: independent static review of JNI, Kotlin generation/runtime and regression tests. I did not run builds, tests, benchmarks or Android/ART checks; the reported performance measurements remain author-supplied.

(Written by OpenAI)

# Conflicts:
#	quest/m1/ffi-export/kotlin-reads.md
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b30bbfab-3937-4b34-a5d9-8a97891c67d4
Commits

Reviewing files that changed from the base of the PR and between e51ecad and 4aa76e6.

Files selected for processing: 9
  • ffi/MIGRATE.md
  • ffi/README.md
  • ffi/fixtures/api/src/lib.rs
  • ffi/runtime/jni.rs
  • ffi/runtime/rt.kt
  • ffi/src/kotlin.rs
  • ffi/tests/api.kt
  • quest/m1/ffi-export/README.md
  • quest/m1/ffi-export/kotlin-reads.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated
kixelated merged commit 58a1503 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m1/ffi-export/kotlin-reads branch October 30, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant