Repository navigation
Convert JS strings passed to iOS native modules without UTF-8 - #59422
hryhoriiK97 wants to merge 2 commits into
Conversation
convertJSIStringToNSString read every JS string argument (including object keys and string values inside objects and arrays) as UTF-8 and decoded it again into an NSString. Copy the string's own ASCII or UTF-16 characters with jsi::String::getStringData instead, and give JSCRuntime a native getStringData that passes the JSString's UTF-16 characters directly. Strings with a lone surrogate or a leading byte order mark, or that arrive in several chunks, still go through UTF-8, so every string converts to the same NSString as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| // A lone surrogate can't be encoded as UTF-8, and native code often needs UTF-8, so such strings | ||
| // keep going through UTF-8, which replaces it as before. So does a leading byte order mark, | ||
| // which decoding UTF-8 drops. | ||
| if ((length > 0 && chars[0] == 0xFEFF) || hasLoneSurrogate(chars, length)) { |
There was a problem hiding this comment.
Why does the BOM trigger UTF8 representation?
There was a problem hiding this comment.
@javache It's only there to keep the same result as before. When UTF-8 is turned into an NSString, a BOM at the start is removed, so native modules never got it (I checked on a device: "\uFEFFabc" arrived as "abc"). I can just remove it from the UTF-16 string instead, with the same result and no UTF-8. Or we can keep the BOM if you're OK with that small change. What do you prefer?
There was a problem hiding this comment.
Let's keep the output compatible like it was before. Please document.
There was a problem hiding this comment.
Done, it now drops the leading BOM directly from the UTF-16 string (same output as before, checked on a device) and the comment explains why - bb26bad
Strings used to reach native code through UTF-8, and decoding UTF-8 into an NSString drops a byte order mark at the start. Drop it directly from the UTF-16 characters instead of taking the UTF-8 path, so the output stays the same as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Visual regressionNo visual differences at ios: no capture |
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D127648419. |
|
This pull request was successfully merged by @hryhoriiK97 in ed15270 When will my fix make it into a release? | How to file a pick request? |
…59422) Summary: This is the other direction of react#59325. On iOS, every JS string that reaches a native module goes through `convertJSIStringToNSString` (`ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm`): string arguments, every key of an object argument, and string values inside objects and arrays. It read the string with `jsi::String::utf8`, so the runtime first converted its own characters to UTF-8, and then `-[NSString initWithBytes:length:encoding:NSUTF8StringEncoding]` decoded that UTF-8 again. For text with accented letters or emoji, both conversions run over the whole string. **With this change.** - The string's own characters are read with `jsi::String::getStringData`. Hermes implements it natively and passes its ASCII or UTF-16 characters directly; they are copied once, with `-initWithBytes:length:encoding:NSASCIIStringEncoding` or `-initWithCharacters:length:`. - A string that starts with a byte order mark (U+FEFF) loses that mark, as decoding UTF-8 used to drop it. A string with a lone surrogate still goes through UTF-8, which replaces the surrogate as before. Native code often needs the string as UTF-8, and an `NSString` with a lone surrogate has none: `UTF8String` returns `NULL`, so for example a `fetch` body with one would be dropped. Finding one is a single pass over the characters, and it costs nothing measurable. A string that arrives in several chunks also goes through UTF-8; no runtime in the repository splits strings. - `JSCRuntime` gets a native `getStringData` that passes the `JSStringRef`'s UTF-16 characters (`JSStringGetCharactersPtr`). Without it, JSC would use JSI's default `getStringData`, which converts to UTF-8 and then to UTF-16, and the new path would be up to 2.6× slower on JSC than before. Every string converts to the same `NSString` as before, including a `\0` (kept since react#57906), lone surrogates and a leading byte order mark. A runtime that doesn't implement `getStringData` itself uses JSI's default, which converts to UTF-8 and then to UTF-16, so on such a runtime this conversion is slower than before, mostly for ASCII text. That applies to runtimes outside this repository, such as `react-native-community/javascriptcore`; the same few lines as in `JSCRuntime` would fix it there. ## Changelog: [IOS] [CHANGED] - Strings passed from JS to native modules convert faster Pull Request resolved: react#59422 Test Plan: - `clang-format --dry-run --Werror` (the repository's pinned version) on both files passes. - RNTester built with `xcodebuild` in Release and run on a physical iPhone 15 Pro (iOS 26.6.1), with Hermes: - In the TurboModule example, "Run function call tests" returns the expected value for all 18 methods. - **Same output:** a local build that ran both conversions on every string and compared them saw 1,276,000 strings (the benchmarks below and the TurboModule, Linking, AppState, Alert and TextInput examples) and found no difference. - **Edge cases**, each passed to `getString` and compared with what comes back, with and without this change: an empty string, `"a\x00b"`, emoji, accented letters, Japanese, and mixed text come back unchanged in both; `"x\uD800y"` (a lone surrogate) comes back with the surrogate replaced, in both. Strings with a byte order mark (`"\uFEFFabc"`, `"\uFEFF"`, `"\uFEFF\uFEFFabc"`, `"a\uFEFFb"`, one with accented letters, one with a lone surrogate, and an object key) come back the same in both: one leading mark is dropped, a mark in the middle or a second one is kept. - **JSC:** a macOS program built with the system JavaScriptCore and this PR's `JSCRuntime.cpp` and `jsi.cpp` converts ASCII, empty, `\0`, lone-surrogate, emoji, accented, Japanese and concatenated strings, and strings with a byte order mark at the start or inside, with the old and the new function, and every result is the same. Old → new, best of 9 rounds: ASCII −34% at 16 characters to −58% at 10 KB; accented text −61% to −97% (10 KB: 24.8 → 0.72 µs). - **Measurement on the iPhone.** RNTester Playground calling `NativeSampleTurboModule.getString` with JSON-like text in ASCII or with accented letters, and `getObject` with 20 keys whose values are 24-character strings, timed with `performance.now()` (20,000 calls up to 128 characters, 5,000 at 1 KB and for the objects, 1,000 at 10 KB, 200 at 100 KB, 20 at 1 MB). The same Release binary, this PR's code plus a launch argument that switches back to the old conversion, alternating, 4 launches each after a warm-up launch. The presses were driven by [Argent](https://github.com/software-mansion/argent) on the physical iPhone. `getString` also returns the string, which since react#59325 is a single copy, so the times below include that return. Mean time per call: | Argument | before | this PR | change | |---|--:|--:|--:| | accented letters, 128 characters | 2.75 µs | 1.71 µs | −38% | | accented letters, 1 KB | 9.69 µs | 2.00 µs | −79% | | accented letters, 10 KB | 80.82 µs | 4.64 µs | −94% | | accented letters, 100 KB | 783.74 µs | 26.06 µs | −97% | | accented letters, 1 MB | 8.01 ms | 0.34 ms | −96% | | ASCII, 128 characters | 2.06 µs | 1.85 µs | −10% | | ASCII, 1 KB | 3.83 µs | 2.40 µs | −37% | | ASCII, 10 KB | 22.97 µs | 8.69 µs | −62% | | ASCII, 100 KB | 211.38 µs | 69.70 µs | −67% | | ASCII, 1 MB | 2.14 ms | 0.68 ms | −68% | | object, 20 keys of accented strings | 18.45 µs | 12.43 µs | −33% | | object, 20 keys of ASCII strings | 12.96 µs | 11.71 µs | −10% | For every row, the ranges of the two arms don't overlap. For 16-character strings, the change was −8% with accented letters and within noise for ASCII. An earlier run of the same comparison (5 and 6 launches, up to 10 KB) gave the same results within 3 points. - **A `fetch` body.** In the same launches, the JS-thread time of `XMLHttpRequest.send(body)` with a 1 MB JSON body, which converts the body to an `NSString` for `RCTNetworking` before anything is sent (median of 25 sends per launch, posted to a closed local port so the network isn't timed): | Body | before | this PR | change | |---|--:|--:|--:| | 1 MB JSON, ASCII | 2.60 ms | 1.35 ms | −48% | | 1 MB JSON, accented letters | 7.95 ms | 0.25 ms | −97% | 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: cipolleschi Differential Revision: D127648419 Pulled By: javache fbshipit-source-id: bfc769b185414927e8a56a87ecafd38ed9896d39
Summary:
This is the other direction of #59325.
On iOS, every JS string that reaches a native module goes through
convertJSIStringToNSString(ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm): string arguments, every key of an object argument, and string values inside objects and arrays. It read the string withjsi::String::utf8, so the runtime first converted its own characters to UTF-8, and then-[NSString initWithBytes:length:encoding:NSUTF8StringEncoding]decoded that UTF-8 again. For text with accented letters or emoji, both conversions run over the whole string.With this change.
jsi::String::getStringData. Hermes implements it natively and passes its ASCII or UTF-16 characters directly; they are copied once, with-initWithBytes:length:encoding:NSASCIIStringEncodingor-initWithCharacters:length:.NSStringwith a lone surrogate has none:UTF8StringreturnsNULL, so for example afetchbody with one would be dropped. Finding one is a single pass over the characters, and it costs nothing measurable. A string that arrives in several chunks also goes through UTF-8; no runtime in the repository splits strings.JSCRuntimegets a nativegetStringDatathat passes theJSStringRef's UTF-16 characters (JSStringGetCharactersPtr). Without it, JSC would use JSI's defaultgetStringData, which converts to UTF-8 and then to UTF-16, and the new path would be up to 2.6× slower on JSC than before.Every string converts to the same
NSStringas before, including a\0(kept since #57906), lone surrogates and a leading byte order mark.A runtime that doesn't implement
getStringDataitself uses JSI's default, which converts to UTF-8 and then to UTF-16, so on such a runtime this conversion is slower than before, mostly for ASCII text. That applies to runtimes outside this repository, such as@react-native-community/javascriptcore; the same few lines as inJSCRuntimewould fix it there.Changelog:
[IOS] [CHANGED] - Strings passed from JS to native modules convert faster
Test Plan:
clang-format --dry-run --Werror(the repository's pinned version) on both files passes.xcodebuildin Release and run on a physical iPhone 15 Pro (iOS 26.6.1), with Hermes:getStringand compared with what comes back, with and without this change: an empty string,"a\x00b", emoji, accented letters, Japanese, and mixed text come back unchanged in both;"x\uD800y"(a lone surrogate) comes back with the surrogate replaced, in both. Strings with a byte order mark ("\uFEFFabc","\uFEFF","\uFEFF\uFEFFabc","a\uFEFFb", one with accented letters, one with a lone surrogate, and an object key) come back the same in both: one leading mark is dropped, a mark in the middle or a second one is kept.JSCRuntime.cppandjsi.cppconverts ASCII, empty,\0, lone-surrogate, emoji, accented, Japanese and concatenated strings, and strings with a byte order mark at the start or inside, with the old and the new function, and every result is the same. Old → new, best of 9 rounds: ASCII −34% at 16 characters to −58% at 10 KB; accented text −61% to −97% (10 KB: 24.8 → 0.72 µs).NativeSampleTurboModule.getStringwith JSON-like text in ASCII or with accented letters, andgetObjectwith 20 keys whose values are 24-character strings, timed withperformance.now()(20,000 calls up to 128 characters, 5,000 at 1 KB and for the objects, 1,000 at 10 KB, 200 at 100 KB, 20 at 1 MB). The same Release binary, this PR's code plus a launch argument that switches back to the old conversion, alternating, 4 launches each after a warm-up launch. The presses were driven by Argent on the physical iPhone.getStringalso returns the string, which since Convert NSStrings returned to JS to UTF-8 only once #59325 is a single copy, so the times below include that return. Mean time per call:For every row, the ranges of the two arms don't overlap. For 16-character strings, the change was −8% with accented letters and within noise for ASCII. An earlier run of the same comparison (5 and 6 launches, up to 10 KB) gave the same results within 3 points.
fetchbody. In the same launches, the JS-thread time ofXMLHttpRequest.send(body)with a 1 MB JSON body, which converts the body to anNSStringforRCTNetworkingbefore anything is sent (median of 25 sends per launch, posted to a closed local port so the network isn't timed):🤖 Generated with Claude Code