Skip to content

runtime-jit: access linear memory through the byte array - #130

Closed
Wavesonics wants to merge 2 commits into
CharlieTap:mainfrom
Wavesonics:jit-byte-array-memory
Closed

Wavesonics wants to merge 2 commits into
CharlieTap:mainfrom
Wavesonics:jit-byte-array-memory

Conversation

@Wavesonics

@Wavesonics Wavesonics commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #128.

runtime-jit no longer passes byteArrayBaseOffset to DexOptions, so the DEX backend emits its existing byte-array loads and stores for linear memory instead of sun.misc.Unsafe calls. On ART those Unsafe calls from generated code go through the generic JNI trampoline rather than an intrinsic, which costs far more than the array accesses that replace them and dominated the run time of memory-heavy modules.

The Unsafe lowering is untouched and still reachable through DexOptions, in case you'd rather gate it than leave it unused.

Harper's grammar checker (Rust, 7.6 MB, 3,574 functions), 300 paragraphs, x86_64 API 34 emulator, steady state over 12 calls in a fresh process:

Mode Steady
Interpreted 7.78 s
Compiled, Unsafe 7.42 s
Compiled, byte array 1.66 s

A Rust word count goes from 320 ms to 94 ms, and a C word count from 62 ms to 33 ms. Output is byte-identical across all three modes.

Testing: :chasm-jit:connectedAndroidDeviceTest passes on the same emulator (266 tests). Not yet measured on arm64 hardware.

When sun.misc.Unsafe was reachable, compile passed its byte array base
offset to the DEX backend, which then lowered inline linear memory
accesses to Unsafe.getInt, putInt, getLong and putLong calls. On ART
these calls from generated code go through the generic JNI trampoline
instead of an intrinsic, so a memory-heavy module spends most of its
time in JNI transitions and gains little from compilation.

Leave byteArrayBaseOffset unset so the backend emits its existing
byte-array loads and stores. The Unsafe lowering stays in the backend
and remains reachable through DexOptions.

Measured on an x86_64 API 34 emulator with a store that meters fuel
and is interruptible. Harper's grammar checker (Rust, 7.6 MB, 3,574
functions) checking 300 paragraphs, steady state over 12 calls in a
fresh process:

  interpreted               7.78 s
  compiled, Unsafe          7.42 s
  compiled, byte array      1.66 s

With Unsafe, simpleperf attributes about 72% of CPU time to ART's
generic JNI machinery. A Rust word count (115 KB) goes from 320 ms to
94 ms and a C word count (4 KB) from 62 ms to 33 ms. Output is
byte-identical across all three modes. Fixes CharlieTap#128.
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.

chasm-jit: Unsafe linear memory access goes through generic JNI on Android; the byte-array path is about 4.5x faster

1 participant