perf : Use GetStringRegion for JavaTurboModule string returns - #58611
riteshshukla04 wants to merge 2 commits into
Conversation
|
Cool idea! I wonder if there's higher impact areas for this beyond turbomodule return values (eg WritableNativeMap / WritableNativeArray) - and what the right max stack size to use is. |
|
Worth measuring too: the 2nd param to |
I checked the copy thing with the old code, |
Also I asked claude to create a quick POC accordingly. One more observation here is , we are using
|
javache
left a comment
There was a problem hiding this comment.
Can we use fbjni's JStringUtf16Extractor. That uses GetStringCritical, which probably performs even better.
Summary: I found (while working on #58611) that `WritableNativeMap::putString` converted its value with fbjni toString(), which calls Java Object.toString() through JNI before running toStdString(). Since the value is already a `jstring`, I call `toStdString()` directly, which produces identical bytes with one fewer JNI call and local reference. | Value | Before | After | Delta | |---|---|---|---| | 11 ASCII | 0.205 | 0.191 | -6.8% | | 100 ASCII | 0.265 | 0.231 | -12.8% | | 300 ASCII | 0.451 | 0.418 | -7.3% | | 1000 ASCII | 0.984 | 0.966 | -1.8% | | 100 CJK | 0.325 | 0.298 | -8.3% | ## Changelog: [ANDROID] [CHANGED] - Skip a redundant JNI Object.toString() call in WritableNativeMap.putString. Pull Request resolved: #58644 Test Plan: I measured putString at 0.614 → 0.474 µs per call for short strings (-23%) on a Galaxy M14, identical output on 21 edge-case strings. Reviewed By: cortinico Differential Revision: D121774499 Pulled By: javache fbshipit-source-id: a20a3d64c6afaf75515f3c3e8173c8e86a1122ce
Summary:
I changed the sync Java TurboModule string return path to copy the jstring with
GetStringRegioninto a 256 char stack buffer (heap above that), going from 3 JNI calls (GetStringLength,GetStringChars,ReleaseStringChars) -> 2 (GetStringLength,GetStringRegion) per call.I did 100_000 calls of
SampleTurboModule.getString(s)For some things like
NativeAppearance.getColorScheme()I got upto 19% improvement.The 256 number is just a POC. We can try 512 or 1024 or something. I am happy to discuss on this .
Changelog:
[ANDROID][CHANGED] - Use GetStringRegion for JavaTurboModule to reduce JNI calls from 3->2
Test Plan:
I ran maestro tests locally . Everything seems to pass.
I also asked claude to create a set of test cases with different string types(text, String , emojis) in turbo module to see nothing breaks