Skip to content

perf(android): read JNI strings without the class check or an allocation - #247

Merged
triniwiz merged 2 commits into
mainfrom
perf/android-jni-strings
Dec 1, 2026
Merged

triniwiz merged 2 commits into
mainfrom
perf/android-jni-strings

Conversation

@triniwiz

@triniwiz triniwiz commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Android natives that take a String read it with get_string_lossy, which called jni's get_string and then copied the result into a String.

  • No class check. get_string looks up java.lang.String and calls IsInstanceOf on every call. Every caller is a native declared with a String parameter, so the check is skipped.
  • No allocation. The helper reads the string with GetStringUTFRegion into a 256-byte stack buffer and returns a guard that derefs to &str. Neither ART nor Rust allocates for the common case. Only text that modified UTF-8 encodes differently from UTF-8 (NUL, characters outside the BMP) is converted into an owned String. Call sites are unchanged.

Emulator, non-debuggable release build, median after JIT warmup:

Native Before After
nativeUaDefaultForTag("h1") 461 ns 105 ns
Grid setter, 3 strings 3213 ns 2140 ns

JniStringInstrumentedTest covers grid strings round-tripping through native, including a non-BMP line name, and UA defaults for a tag.

get_string looked up java.lang.String and called IsInstanceOf on every
call, and the result was then copied into a String. Every caller is a
native declared with a String parameter, so read with
get_string_unchecked and borrow JNI's buffer as &str; only NUL or
non-BMP text, which modified UTF-8 encodes differently, is copied.
@triniwiz triniwiz changed the title perf(android): read JNI strings without the class check or a copy perf(android): read JNI strings without the class check or an allocation Dec 1, 2026
@triniwiz
triniwiz merged commit 0f1da1e into main Dec 1, 2026
1 of 4 checks passed
@triniwiz
triniwiz deleted the perf/android-jni-strings branch December 1, 2026 02:55
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