Skip to content

Commit b86c82c

Browse files
hryhoriiK97claude
andauthored
Convert iOS native module objects to JS with fewer lookups and retains (#59450)
* Convert iOS native module objects to JS with fewer lookups and retains TurboModule return values, promise results, callback arguments and codegen events are converted by convertObjCObjectToJSIValue. Dictionaries now read their keys and values at once instead of looking up each key again, keys are created as property names directly, nested values are retained once per container instead of once per call, and short strings without character storage are copied to the stack instead of the heap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Address review: no string wrapper, unretained objects, no stack copy - Call convertNSStringToJSI<jsi::String> directly instead of convertNSStringToJSIString. - Read a container's objects into one vector and return it. - Don't retain the objects: the container holds them while they are converted. - Drop the stack copy of short strings. With it back, the benchmarks were within 1%, so it didn't help. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7619e9c commit b86c82c

1 file changed

Lines changed: 72 additions & 23 deletions

File tree

  • packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon

‎packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm‎

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#import <sstream>
3333
#import <string>
34+
#import <type_traits>
3435
#import <vector>
3536

3637
using namespace facebook;
@@ -49,65 +50,108 @@ static int32_t getUniqueId()
4950
/**
5051
* All static helper functions are ObjC++ specific.
5152
*/
52-
static jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime &runtime, NSNumber *value)
53+
static jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime &runtime, __unsafe_unretained NSNumber *value)
5354
{
5455
return jsi::Value((bool)[value boolValue]);
5556
}
5657

57-
static jsi::Value convertNSNumberToJSINumber(jsi::Runtime &runtime, NSNumber *value)
58+
static jsi::Value convertNSNumberToJSINumber(jsi::Runtime &runtime, __unsafe_unretained NSNumber *value)
5859
{
5960
return jsi::Value([value doubleValue]);
6061
}
6162

62-
static jsi::String convertNSStringToJSIString(jsi::Runtime &runtime, NSString *value)
63+
// Creates a JS string or property name from the string's own ASCII or UTF-16 characters.
64+
template <typename T>
65+
static T convertNSStringToJSI(jsi::Runtime &runtime, __unsafe_unretained NSString *value)
6366
{
67+
auto fromAscii = [&](const char *ascii, size_t length) {
68+
if constexpr (std::is_same_v<T, jsi::PropNameID>) {
69+
return jsi::PropNameID::forAscii(runtime, ascii, length);
70+
} else {
71+
return jsi::String::createFromAscii(runtime, ascii, length);
72+
}
73+
};
74+
auto fromUtf16 = [&](const char16_t *utf16, size_t length) {
75+
if constexpr (std::is_same_v<T, jsi::PropNameID>) {
76+
return jsi::PropNameID::forUtf16(runtime, utf16, length);
77+
} else {
78+
return jsi::String::createFromUtf16(runtime, utf16, length);
79+
}
80+
};
81+
6482
if (value == nil) {
65-
return jsi::String::createFromAscii(runtime, "", 0);
83+
return fromAscii("", 0);
6684
}
6785

6886
// Hand the JS runtime the string's own ASCII or UTF-16 characters when
6987
// CoreFoundation can expose them, and copy them once otherwise.
7088
auto string = (__bridge CFStringRef)value;
7189
CFIndex length = CFStringGetLength(string);
7290
if (const char *ascii = CFStringGetCStringPtr(string, kCFStringEncodingASCII)) {
73-
return jsi::String::createFromAscii(runtime, ascii, static_cast<size_t>(length));
91+
return fromAscii(ascii, static_cast<size_t>(length));
7492
}
7593
if (const UniChar *characters = CFStringGetCharactersPtr(string)) {
76-
return jsi::String::createFromUtf16(
77-
runtime,
78-
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
79-
reinterpret_cast<const char16_t *>(characters),
80-
static_cast<size_t>(length));
94+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
95+
return fromUtf16(reinterpret_cast<const char16_t *>(characters), static_cast<size_t>(length));
8196
}
8297
std::u16string utf16(length, u'\0');
8398
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
8499
CFStringGetCharacters(string, CFRangeMake(0, length), reinterpret_cast<UniChar *>(utf16.data()));
85-
return jsi::String::createFromUtf16(runtime, utf16);
100+
return fromUtf16(utf16.data(), utf16.size());
101+
}
102+
103+
// Doesn't retain `value`: the caller holds it, and containers hold what they contain while it is converted.
104+
static jsi::Value convertUnretainedObjCObjectToJSIValue(jsi::Runtime &runtime, __unsafe_unretained id value);
105+
106+
// Reads a container's objects at once. They aren't retained, as the container holds them.
107+
template <typename ReadObjects>
108+
static std::vector<__unsafe_unretained id> getObjects(NSUInteger count, ReadObjects readObjects)
109+
{
110+
std::vector<__unsafe_unretained id> objects(count);
111+
readObjects(objects.data());
112+
return objects;
86113
}
87114

88-
static jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, NSDictionary *value)
115+
static jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, __unsafe_unretained NSDictionary *value)
89116
{
117+
NSUInteger count = value.count;
118+
std::vector<__unsafe_unretained id> keysAndObjects = getObjects(count * 2, [&](__unsafe_unretained id *objects) {
119+
[value getObjects:objects + count andKeys:objects count:count];
120+
});
90121
jsi::Object result = jsi::Object(runtime);
91-
for (NSString *k in value) {
92-
result.setProperty(runtime, convertNSStringToJSIString(runtime, k), convertObjCObjectToJSIValue(runtime, value[k]));
122+
for (NSUInteger i = 0; i < count; i++) {
123+
result.setProperty(
124+
runtime,
125+
convertNSStringToJSI<jsi::PropNameID>(runtime, keysAndObjects[i]),
126+
convertUnretainedObjCObjectToJSIValue(runtime, keysAndObjects[count + i]));
93127
}
94128
return result;
95129
}
96130

97-
static jsi::Array convertNSArrayToJSIArray(jsi::Runtime &runtime, NSArray *value)
131+
static std::vector<__unsafe_unretained id> getObjects(__unsafe_unretained NSArray *value)
132+
{
133+
NSUInteger count = value.count;
134+
return getObjects(
135+
count, [&](__unsafe_unretained id *objects) { [value getObjects:objects range:NSMakeRange(0, count)]; });
136+
}
137+
138+
static jsi::Array convertNSArrayToJSIArray(jsi::Runtime &runtime, __unsafe_unretained NSArray *value)
98139
{
99-
jsi::Array result = jsi::Array(runtime, value.count);
100-
for (size_t i = 0; i < value.count; i++) {
101-
result.setValueAtIndex(runtime, i, convertObjCObjectToJSIValue(runtime, value[i]));
140+
std::vector<__unsafe_unretained id> objects = getObjects(value);
141+
jsi::Array result = jsi::Array(runtime, objects.size());
142+
for (size_t i = 0; i < objects.size(); i++) {
143+
result.setValueAtIndex(runtime, i, convertUnretainedObjCObjectToJSIValue(runtime, objects[i]));
102144
}
103145
return result;
104146
}
105147

106-
static std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime &runtime, NSArray *value)
148+
static std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime &runtime, __unsafe_unretained NSArray *value)
107149
{
150+
std::vector<__unsafe_unretained id> objects = getObjects(value);
108151
std::vector<jsi::Value> result;
109-
for (size_t i = 0; i < value.count; i++) {
110-
result.emplace_back(convertObjCObjectToJSIValue(runtime, value[i]));
152+
result.reserve(objects.size());
153+
for (__unsafe_unretained id object : objects) {
154+
result.emplace_back(convertUnretainedObjCObjectToJSIValue(runtime, object));
111155
}
112156
return result;
113157
}
@@ -152,9 +196,14 @@ size_t size() const override
152196
}
153197

154198
jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value)
199+
{
200+
return convertUnretainedObjCObjectToJSIValue(runtime, value);
201+
}
202+
203+
static jsi::Value convertUnretainedObjCObjectToJSIValue(jsi::Runtime &runtime, __unsafe_unretained id value)
155204
{
156205
if ([value isKindOfClass:[NSString class]]) {
157-
return convertNSStringToJSIString(runtime, (NSString *)value);
206+
return convertNSStringToJSI<jsi::String>(runtime, value);
158207
} else if ([value isKindOfClass:[NSNumber class]]) {
159208
if ([value isKindOfClass:[@YES class]]) {
160209
return convertNSNumberToJSIBoolean(runtime, (NSNumber *)value);
@@ -691,7 +740,7 @@ TraceSection s(
691740
break;
692741
}
693742
case StringKind: {
694-
returnValue = convertNSStringToJSIString(runtime, (NSString *)result);
743+
returnValue = convertNSStringToJSI<jsi::String>(runtime, (NSString *)result);
695744
break;
696745
}
697746
case ObjectKind: {

0 commit comments

Comments
 (0)