Skip to content

Commit de6506b

Browse files
rigor789NathanWalker
authored andcommitted
perf: cache swizzled selector construction (#173)
1 parent 7a78cca commit de6506b

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ v8
4545
.npmrc
4646

4747
llvm/
48+
49+
# v8 build files...
50+
.gclient_entries
51+
.gclient
52+
.cipd/

NativeScript/NativeScript-Prefix.pch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef NativeScript_Prefix_pch
22
#define NativeScript_Prefix_pch
33

4-
#define NATIVESCRIPT_VERSION "8.3.1-alpha.0"
4+
#define NATIVESCRIPT_VERSION "8.3.1-alpha.1"
55

66
#ifdef DEBUG
77
#define SIZEOF_OFF_T 8

NativeScript/runtime/Helpers.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ BaseDataWrapper* GetValue(v8::Isolate* isolate, const v8::Local<v8::Value>& val)
3737
void DeleteValue(v8::Isolate* isolate, const v8::Local<v8::Value>& val);
3838
std::vector<v8::Local<v8::Value>> ArgsToVector(const v8::FunctionCallbackInfo<v8::Value>& info);
3939

40-
bool IsString(v8::Local<v8::Value> value);
41-
bool IsNumber(v8::Local<v8::Value> value);
42-
bool IsBool(v8::Local<v8::Value> value);
43-
bool IsArrayOrArrayLike(v8::Isolate* isolate, v8::Local<v8::Value> value);
44-
void* TryGetBufferFromArrayBuffer(v8::Local<v8::Value> value, bool& isArrayBuffer);
40+
inline bool IsString(const v8::Local<v8::Value>& value) {
41+
return !value.IsEmpty() && (value->IsString() || value->IsStringObject());
42+
}
43+
44+
inline bool IsNumber(const v8::Local<v8::Value>& value) {
45+
return !value.IsEmpty() && (value->IsNumber() || value->IsNumberObject());
46+
}
47+
48+
inline bool IsBool(const v8::Local<v8::Value>& value) {
49+
return !value.IsEmpty() && (value->IsBoolean() || value->IsBooleanObject());
50+
}
51+
52+
53+
bool IsArrayOrArrayLike(v8::Isolate* isolate, const v8::Local<v8::Value>& value);
54+
void* TryGetBufferFromArrayBuffer(const v8::Local<v8::Value>& value, bool& isArrayBuffer);
4555

4656
void ExecuteOnMainThread(std::function<void ()> func, bool async = true);
4757

NativeScript/runtime/Helpers.mm

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,7 @@
337337
return args;
338338
}
339339

340-
bool tns::IsString(Local<Value> value) {
341-
return !value.IsEmpty() && (value->IsString() || value->IsStringObject());
342-
}
343-
344-
bool tns::IsNumber(Local<Value> value) {
345-
return !value.IsEmpty() && (value->IsNumber() || value->IsNumberObject());
346-
}
347-
348-
bool tns::IsBool(Local<Value> value) {
349-
return !value.IsEmpty() && (value->IsBoolean() || value->IsBooleanObject());
350-
}
351-
352-
bool tns::IsArrayOrArrayLike(Isolate* isolate, Local<Value> value) {
340+
bool tns::IsArrayOrArrayLike(Isolate* isolate, const Local<Value>& value) {
353341
if (value->IsArray()) {
354342
return true;
355343
}
@@ -365,7 +353,7 @@
365353
return obj->Has(context, ToV8String(isolate, "length")).FromMaybe(false);
366354
}
367355

368-
void* tns::TryGetBufferFromArrayBuffer(v8::Local<v8::Value> value, bool& isArrayBuffer) {
356+
void* tns::TryGetBufferFromArrayBuffer(const v8::Local<v8::Value>& value, bool& isArrayBuffer) {
369357
isArrayBuffer = false;
370358

371359
if (value.IsEmpty() || (!value->IsArrayBuffer() && !value->IsArrayBufferView())) {

NativeScript/runtime/Interop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class Interop {
148148
static bool IsNumbericType(BinaryTypeEncodingType type);
149149
static v8::Local<v8::Object> GetInteropType(v8::Local<v8::Context> context, BinaryTypeEncodingType type);
150150
static std::vector<std::string> GetAdditionalProtocols(const TypeEncoding* typeEncoding);
151+
static SEL GetSwizzledMethodSelector(SEL selector);
151152

152153
template <typename T>
153154
static inline void SetValue(void* dest, T value) {
@@ -192,6 +193,7 @@ class Interop {
192193

193194
static JSBlockDescriptor kJSBlockDescriptor;
194195
} JSBlock;
196+
195197
};
196198

197199
}

NativeScript/runtime/Interop.mm

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "SymbolIterator.h"
1919
#include "UnmanagedType.h"
2020
#include "OneByteStringResource.h"
21+
#include "robin_hood.h"
2122

2223
using namespace v8;
2324

@@ -1366,6 +1367,26 @@
13661367
return result.As<v8::Array>();
13671368
}
13681369

1370+
SEL Interop::GetSwizzledMethodSelector(SEL selector) {
1371+
static robin_hood::unordered_map<SEL, SEL> *swizzledMethodSelectorCache = new robin_hood::unordered_map<SEL, SEL>();
1372+
1373+
SEL swizzledMethodSelector = NULL;
1374+
1375+
try {
1376+
swizzledMethodSelector = swizzledMethodSelectorCache->at(selector);
1377+
} catch(const std::out_of_range&) {
1378+
// ignore...
1379+
}
1380+
1381+
if(!swizzledMethodSelector) {
1382+
swizzledMethodSelector = sel_registerName((Constants::SwizzledPrefix + std::string(sel_getName(selector))).c_str());
1383+
// save to cache
1384+
swizzledMethodSelectorCache->emplace(selector, swizzledMethodSelector);
1385+
}
1386+
1387+
return swizzledMethodSelector;
1388+
}
1389+
13691390
Local<Value> Interop::CallFunctionInternal(MethodCall& methodCall) {
13701391
int initialParameterIndex = methodCall.isPrimitiveFunction_ ? 0 : 2;
13711392

@@ -1401,9 +1422,7 @@
14011422

14021423
SEL selector = methodCall.selector_;
14031424
if (isInstanceMethod) {
1404-
NSString* selectorStr = NSStringFromSelector(selector);
1405-
NSString* swizzledMethodSelectorStr = [NSString stringWithFormat:@"%s%@", Constants::SwizzledPrefix.c_str(), selectorStr];
1406-
SEL swizzledMethodSelector = NSSelectorFromString(swizzledMethodSelectorStr);
1425+
SEL swizzledMethodSelector = Interop::GetSwizzledMethodSelector(selector);
14071426
if ([methodCall.target_ respondsToSelector:swizzledMethodSelector]) {
14081427
selector = swizzledMethodSelector;
14091428
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nativescript/ios",
33
"description": "NativeScript Runtime for iOS",
4-
"version": "8.3.1-alpha.0",
4+
"version": "8.3.1-alpha.1",
55
"keywords": [
66
"NativeScript",
77
"iOS",

0 commit comments

Comments
 (0)