Skip to content

Commit 4692bfa

Browse files
committed
perf(runtime): pass the isolate instead of reading it from TLS
Isolate::GetCurrent() and Runtime lookups are thread-local reads (emulated TLS below API 29) that several per-call helpers repeated even though their callers already held the isolate or the runtime. - ArgConverter::ConvertToJavaString, NumericCasts::GetCastValue, MetadataNode::GetNodeFromHandle, the single-value JsArgToArrayConverter and the array element accessors now take the isolate from their caller. - CallJavaMethod looked the runtime up twice per call; it reuses the first lookup. - The instance field getter and setter looked up the runtime only to call the static ObjectManager::IsJsRuntimeObject.
1 parent 1ed0508 commit 4692bfa

15 files changed

Lines changed: 56 additions & 60 deletions

‎test-app/runtime/src/main/cpp/ArgConverter.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ class ArgConverter {
154154

155155
static std::u16string ConvertToUtf16String(const v8::Local<v8::String>& s);
156156

157-
inline static jstring ConvertToJavaString(const v8::Local<v8::Value>& jsValue) {
157+
inline static jstring ConvertToJavaString(v8::Isolate* isolate, const v8::Local<v8::Value>& jsValue) {
158158
JEnv env;
159-
auto isolate = v8::Isolate::GetCurrent();
160159
v8::Local<v8::String> str;
161160
if (jsValue->IsString()) {
162161
str = jsValue.As<v8::String>();

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ using namespace v8;
99
using namespace std;
1010
using namespace tns;
1111

12-
Local<Value> ArrayElementAccessor::GetArrayElement(Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
12+
Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
1313
JEnv env;
1414

15-
Isolate* isolate = v8::Isolate::GetCurrent();
1615
EscapableHandleScope handleScope(isolate);
1716
auto runtime = Runtime::GetRuntime(isolate);
1817
auto objectManager = runtime->GetObjectManager();
@@ -76,10 +75,9 @@ Local<Value> ArrayElementAccessor::GetArrayElement(Local<Context> context, const
7675
return handleScope.Escape(value);
7776
}
7877

79-
void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature, Local<Value>& value) {
78+
void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature, Local<Value>& value) {
8079
JEnv env;
8180

82-
Isolate* isolate = v8::Isolate::GetCurrent();
8381
HandleScope handleScope(isolate);
8482
auto runtime = Runtime::GetRuntime(isolate);
8583
auto objectManager = runtime->GetObjectManager();
@@ -132,7 +130,7 @@ void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<O
132130
if (isReferenceType) {
133131
auto object = value.As<Object>();
134132

135-
JsArgToArrayConverter argConverter(context, value, false, (int) Type::Null);
133+
JsArgToArrayConverter argConverter(isolate, context, value, false, (int) Type::Null);
136134
if (argConverter.IsValid()) {
137135
jobjectArray objArr = static_cast<jobjectArray>(arr);
138136
jobject objectElementValue = argConverter.GetConvertedArg();

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
namespace tns {
1010
class ArrayElementAccessor {
1111
public:
12-
v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature);
12+
v8::Local<v8::Value> GetArrayElement(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature);
1313

14-
void SetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature, v8::Local<v8::Value>& value);
14+
void SetArrayElement(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature, v8::Local<v8::Value>& value);
1515

1616
private:
1717
v8::Local<v8::Value> ConvertToJsValue(v8::Isolate* isolate, ObjectManager* objectManager, JEnv& env, const std::string& elementSignature, const void* value);

‎test-app/runtime/src/main/cpp/CallbackHandlers.cpp‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,17 @@ string CallbackHandlers::ResolveClassName(Isolate *isolate, jclass &clazz) {
336336
return className;
337337
}
338338

339-
Local<Value> CallbackHandlers::GetArrayElement(Local<Context> context, const Local<Object> &array,
339+
Local<Value> CallbackHandlers::GetArrayElement(Isolate *isolate, Local<Context> context,
340+
const Local<Object> &array,
340341
uint32_t index, const string &arraySignature) {
341-
return arrayElementAccessor.GetArrayElement(context, array, index, arraySignature);
342+
return arrayElementAccessor.GetArrayElement(isolate, context, array, index, arraySignature);
342343
}
343344

344-
void CallbackHandlers::SetArrayElement(Local<Context> context, const Local<Object> &array,
345-
uint32_t index,
345+
void CallbackHandlers::SetArrayElement(Isolate *isolate, Local<Context> context,
346+
const Local<Object> &array, uint32_t index,
346347
const string &arraySignature, Local<Value> &value) {
347348

348-
arrayElementAccessor.SetArrayElement(context, array, index, arraySignature, value);
349+
arrayElementAccessor.SetArrayElement(isolate, context, array, index, arraySignature, value);
349350
}
350351

351352
Local<Value> CallbackHandlers::GetJavaField(Isolate *isolate, const Local<Object> &caller,
@@ -386,10 +387,11 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
386387
const MethodCache::CacheMethodInfo *mi = nullptr;
387388

388389
auto isolate = args.GetIsolate();
390+
auto runtime = Runtime::GetRuntime(isolate);
389391

390392
// The Java method may synchronously call back into JS (an overridden
391393
// method on the receiver) - that whole window is a JS-initiated chain.
392-
JavaCallScope javaCallScope(Runtime::GetRuntime(isolate));
394+
JavaCallScope javaCallScope(runtime);
393395

394396
if ((entry != nullptr) && entry->getIsResolved()) {
395397
auto &entrySignature = entry->getSig();
@@ -399,7 +401,7 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
399401
clazz = env.FindClass(className);
400402

401403
if (clazz == nullptr) {
402-
MetadataNode *callerNode = MetadataNode::GetNodeFromHandle(caller);
404+
MetadataNode *callerNode = MetadataNode::GetNodeFromHandle(isolate, caller);
403405
const string callerClassName = callerNode->GetName();
404406

405407
DEBUG_WRITE("Cannot resolve class: %s while calling method: %s callerClassName: %s",
@@ -473,7 +475,7 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
473475
return;
474476
}
475477
} else {
476-
MetadataNode *callerNode = MetadataNode::GetNodeFromHandle(caller);
478+
MetadataNode *callerNode = MetadataNode::GetNodeFromHandle(isolate, caller);
477479
const string callerClassName = callerNode->GetName();
478480
DEBUG_WRITE("Resolving method on caller class: %s.%s on className %s",
479481
callerClassName.c_str(), methodName.c_str(), className.c_str());
@@ -517,7 +519,6 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
517519

518520
jvalue *javaArgs = argConverter.ToArgs();
519521

520-
auto runtime = Runtime::GetRuntimeFromIsolateData(isolate);
521522
auto objectManager = runtime->GetObjectManager();
522523

523524
if (!isStatic) {

‎test-app/runtime/src/main/cpp/CallbackHandlers.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ namespace tns {
8080
static std::string ResolveClassName(v8::Isolate *isolate, jclass &clazz);
8181

8282
static v8::Local<v8::Value>
83-
GetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
83+
GetArrayElement(v8::Isolate *isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
8484
const std::string &arraySignature);
8585

8686
static void
87-
SetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
87+
SetArrayElement(v8::Isolate *isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
8888
const std::string &arraySignature, v8::Local<v8::Value> &value);
8989

9090
static int GetArrayLength(v8::Isolate *isolate, const v8::Local<v8::Object> &arr);

‎test-app/runtime/src/main/cpp/FieldAccessor.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void FieldAccessor::SetJavaField(Isolate* isolate, const Local<Object>& target,
317317
if (!value->IsNull() && !value->IsUndefined()) {
318318
if (isString) {
319319
//TODO: validate valie is a string;
320-
result = ArgConverter::ConvertToJavaString(value);
320+
result = ArgConverter::ConvertToJavaString(isolate, value);
321321
} else {
322322
if (value->IsProxy() && !ObjectManager::UnwrapProxy(value)->IsObject()) {
323323
throw NativeScriptException("Cannot assign a revoked Proxy to field '" + fieldMetadata.getName() + "'");

‎test-app/runtime/src/main/cpp/JsArgConverter.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ bool JsArgConverter::ConvertArg(const Local<Value> &jsArg, int index) {
328328

329329
switch (castType) {
330330
case CastType::Char:
331-
castValue = NumericCasts::GetCastValue(jsObject);
331+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
332332
if (castValue->IsString()) {
333333
m_args[index].c = ArgConverter::ToJavaChar(m_isolate, castValue.As<String>());
334334
success = true;
335335
}
336336
break;
337337

338338
case CastType::Byte:
339-
castValue = NumericCasts::GetCastValue(jsObject);
339+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
340340
if (castValue->IsString()) {
341341
string strValue = ArgConverter::ConvertToString(
342342
castValue->ToString(context).ToLocalChecked());
@@ -351,7 +351,7 @@ bool JsArgConverter::ConvertArg(const Local<Value> &jsArg, int index) {
351351
break;
352352

353353
case CastType::Short:
354-
castValue = NumericCasts::GetCastValue(jsObject);
354+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
355355
if (castValue->IsString()) {
356356
string strValue = ArgConverter::ConvertToString(
357357
castValue->ToString(context).ToLocalChecked());
@@ -366,7 +366,7 @@ bool JsArgConverter::ConvertArg(const Local<Value> &jsArg, int index) {
366366
break;
367367

368368
case CastType::Long:
369-
castValue = NumericCasts::GetCastValue(jsObject);
369+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
370370
if (castValue->IsString()) {
371371
string strValue = ArgConverter::ConvertToString(
372372
castValue->ToString(context).ToLocalChecked());
@@ -381,7 +381,7 @@ bool JsArgConverter::ConvertArg(const Local<Value> &jsArg, int index) {
381381
break;
382382

383383
case CastType::Float:
384-
castValue = NumericCasts::GetCastValue(jsObject);
384+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
385385
if (castValue->IsNumber()) {
386386
double floatArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue(
387387
context).ToChecked();
@@ -391,7 +391,7 @@ bool JsArgConverter::ConvertArg(const Local<Value> &jsArg, int index) {
391391
break;
392392

393393
case CastType::Double:
394-
castValue = NumericCasts::GetCastValue(jsObject);
394+
castValue = NumericCasts::GetCastValue(m_isolate, jsObject);
395395
if (castValue->IsNumber()) {
396396
double doubleArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue(
397397
context).ToChecked();
@@ -564,7 +564,7 @@ bool JsArgConverter::ConvertJavaScriptString(const Local<Value> &jsValue, int in
564564
return true;
565565
}
566566

567-
jstring stringObject = ArgConverter::ConvertToJavaString(jsValue);
567+
jstring stringObject = ArgConverter::ConvertToJavaString(m_isolate, jsValue);
568568
SetConvertedObject(index, stringObject);
569569

570570
return true;
@@ -709,7 +709,7 @@ bool JsArgConverter::ConvertJavaScriptArray(const Local<Object> &source, size_t
709709
if (!elementAt(i, element)) {
710710
return false;
711711
}
712-
JsArgToArrayConverter c(context, element, false, (int) Type::Null);
712+
JsArgToArrayConverter c(m_isolate, context, element, false, (int) Type::Null);
713713
env.SetObjectArrayElement((jobjectArray) objects, i, c.GetConvertedArg());
714714
}
715715
arr = (jarray) objects.Move();

‎test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ using namespace tns;
1717
/*
1818
* Converts a single JavaScript (V8) object to its respective Java representation
1919
*/
20-
JsArgToArrayConverter::JsArgToArrayConverter(Local<Context> context, const v8::Local<Value> &arg,
20+
JsArgToArrayConverter::JsArgToArrayConverter(Isolate *isolate, Local<Context> context,
21+
const v8::Local<Value> &arg,
2122
bool isImplementationObject, int classReturnType)
22-
: m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
23+
: m_isolate(isolate), m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
2324
m_return_type(classReturnType) {
2425
if (!isImplementationObject) {
2526
m_argsLen = 1;
@@ -35,7 +36,7 @@ JsArgToArrayConverter::JsArgToArrayConverter(Local<Context> context, const v8::L
3536
*/
3637
JsArgToArrayConverter::JsArgToArrayConverter(const v8::FunctionCallbackInfo<Value> &args,
3738
bool hasImplementationObject)
38-
: m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
39+
: m_isolate(args.GetIsolate()), m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
3940
m_return_type(static_cast<int>(Type::Null)) {
4041
m_argsLen = !hasImplementationObject ? args.Length() : args.Length() - 2;
4142

@@ -45,7 +46,7 @@ JsArgToArrayConverter::JsArgToArrayConverter(const v8::FunctionCallbackInfo<Valu
4546
m_argsAsObject = new jobject[m_argsLen];
4647
memset(m_argsAsObject, 0, m_argsLen * sizeof(jobject));
4748

48-
auto context = args.GetIsolate()->GetCurrentContext();
49+
auto context = m_isolate->GetCurrentContext();
4950
for (int i = 0; i < m_argsLen; i++) {
5051
success = ConvertArg(context, args[i], i);
5152

@@ -65,7 +66,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
6566
JEnv env;
6667

6768
Type returnType = JType::getClassType(m_return_type);
68-
auto isolate = v8::Isolate::GetCurrent();
69+
auto isolate = m_isolate;
6970

7071
// A Proxy's target is what holds the Java link.
7172
auto arg = jsArg.IsEmpty() ? jsArg : ObjectManager::UnwrapProxy(jsArg);
@@ -136,7 +137,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
136137

137138
success = true;
138139
} else if (arg->IsString() || arg->IsStringObject()) {
139-
auto stringObject = ArgConverter::ConvertToJavaString(arg);
140+
auto stringObject = ArgConverter::ConvertToJavaString(isolate, arg);
140141
SetConvertedObject(env, index, stringObject);
141142

142143
success = true;
@@ -160,7 +161,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
160161

161162
switch (castType) {
162163
case CastType::Char:
163-
castValue = NumericCasts::GetCastValue(jsObj);
164+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
164165
charValue = '\0';
165166
if (castValue->IsString()) {
166167
charValue = ArgConverter::ToJavaChar(isolate, castValue.As<String>());
@@ -171,7 +172,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
171172
break;
172173

173174
case CastType::Byte:
174-
castValue = NumericCasts::GetCastValue(jsObj);
175+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
175176
byteValue = 0;
176177
if (castValue->IsString()) {
177178
string value = ArgConverter::ConvertToString(
@@ -189,7 +190,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
189190
break;
190191

191192
case CastType::Short:
192-
castValue = NumericCasts::GetCastValue(jsObj);
193+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
193194
shortValue = 0;
194195
if (castValue->IsString()) {
195196
string value = ArgConverter::ConvertToString(
@@ -207,7 +208,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
207208
break;
208209

209210
case CastType::Long:
210-
castValue = NumericCasts::GetCastValue(jsObj);
211+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
211212
longValue = 0;
212213
if (castValue->IsString()) {
213214
auto strValue = ArgConverter::ConvertToString(
@@ -223,7 +224,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
223224
break;
224225

225226
case CastType::Float:
226-
castValue = NumericCasts::GetCastValue(jsObj);
227+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
227228
floatValue = 0;
228229
if (castValue->IsNumber()) {
229230
double floatArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue(
@@ -236,7 +237,7 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
236237
break;
237238

238239
case CastType::Double:
239-
castValue = NumericCasts::GetCastValue(jsObj);
240+
castValue = NumericCasts::GetCastValue(isolate, jsObj);
240241
doubleValue = 0;
241242
if (castValue->IsNumber()) {
242243
double doubleArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue(

‎test-app/runtime/src/main/cpp/JsArgToArrayConverter.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class JsArgToArrayConverter {
1212
public:
1313
JsArgToArrayConverter(const v8::FunctionCallbackInfo<v8::Value>& args, bool hasImplementationObject);
1414

15-
JsArgToArrayConverter(v8::Local<v8::Context> context, const v8::Local<v8::Value>& arg, bool isImplementationObject, int classReturnType);
15+
JsArgToArrayConverter(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Value>& arg, bool isImplementationObject, int classReturnType);
1616

1717
~JsArgToArrayConverter();
1818

@@ -42,6 +42,8 @@ class JsArgToArrayConverter {
4242

4343
void SetConvertedObject(JEnv& env, int index, jobject obj, bool isGlobal = false);
4444

45+
v8::Isolate* m_isolate;
46+
4547
int m_argsLen;
4648

4749
int m_return_type;

0 commit comments

Comments
 (0)