Repository navigation
perf(ffi): copy Kotlin's returned bytes up to 1 KiB in the getter's crossing - #240
Conversation
Quest-Claim: f8503715363a70ca
…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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
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
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
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
COPIEDinffi/runtime/jni.rs) cross as abyte[]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 along[]and are lent as a directByteBufferas before.LENTmarker (-2) and its owner.close()stays idempotent on both paths. A copiedBytesfrees 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):
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):
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):This PR doesn't touch the frame read path, and its minimums match main's; the medians differ by noise.
Tests
api.kt'sownedcase (the GC test) now covers both paths:token_bytestakes a length, and onlyapi.ktcalls it. The C and C++ fixtures and the transcript are unchanged.Public API
No signature changes.
Bytes.bufferis now a heapByteBufferfor 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 generatedNativeobject,owned()andLENTare new.Checks
just check,just test,just ffi testandjust ffi moqpass. The last two run in the bench shell, which haskotlinc.Review
Round 2,
quest reviewwith Codexgpt-6.1-solat medium effort, on head 77ad917 against origin/main f78c588: no findings.Round 1, on head 8e9e093, found one issue: the cast to
LongArrayinrt.ktcould be shadowed by an exported class of that name. Fixed in 77ad917 by qualifying it askotlin.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 head77ad9171, so this merge is a recorded skip, too trivial to review.🤖 Generated with Claude Code
(written by Claude Opus 5.5)