Repository navigation
perf(android): read JNI strings without the class check or an allocation - #247
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Android natives that take a
Stringread it withget_string_lossy, which called jni'sget_stringand then copied the result into aString.get_stringlooks upjava.lang.Stringand callsIsInstanceOfon every call. Every caller is a native declared with aStringparameter, so the check is skipped.GetStringUTFRegioninto 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 ownedString. Call sites are unchanged.Emulator, non-debuggable release build, median after JIT warmup:
nativeUaDefaultForTag("h1")JniStringInstrumentedTestcovers grid strings round-tripping through native, including a non-BMP line name, and UA defaults for a tag.