Skip to content

Commit f23c1bb

Browse files
committed
perf: remove instantiations and cache runtime in isolate
1 parent aa623b7 commit f23c1bb

File tree

7 files changed

+51
-50
lines changed

7 files changed

+51
-50
lines changed

test-app/runtime/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ include_directories(
5959
if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
6060
set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 -fvisibility=hidden -ffunction-sections -fno-data-sections")
6161
else ()
62-
# set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -g")
63-
set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 -fvisibility=hidden -ffunction-sections -fno-data-sections")
62+
set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -g")
63+
# set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 -fvisibility=hidden -ffunction-sections -fno-data-sections")
6464
endif ()
6565

6666
# Command info: https://cmake.org/cmake/help/v3.4/command/add_library.html

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local<Object> &j
102102
instance = env.NewObject(generatedJavaClass, mi.mid);
103103
} else {
104104
// resolve arguments before passing them on to the constructor
105-
JSToJavaConverter argConverter(argWrapper.args, mi.signature);
105+
JSToJavaConverter argConverter(isolate, argWrapper.args, mi.signature);
106106
auto ctorArgs = argConverter.ToArgs();
107107

108108
instance = env.NewObjectA(generatedJavaClass, mi.mid, ctorArgs);
@@ -209,6 +209,8 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
209209
auto retType = MethodReturnType::Unknown;
210210
MethodCache::CacheMethodInfo mi;
211211

212+
auto isolate = args.GetIsolate();
213+
212214
if ((entry != nullptr) && entry->isResolved) {
213215
isStatic = entry->isStatic;
214216

@@ -320,27 +322,20 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
320322
methodName.c_str());
321323
}
322324

323-
JSToJavaConverter *argConverter;
324-
325-
if (entry != nullptr && entry->isExtensionFunction) {
326-
argConverter = new JSToJavaConverter(args, *sig, entry, caller);
327-
} else {
328-
argConverter = new JSToJavaConverter(args, *sig, entry);
329-
}
325+
JSToJavaConverter argConverter = (entry != nullptr && entry->isExtensionFunction)
326+
? JSToJavaConverter(isolate, args, *sig, entry, caller)
327+
: JSToJavaConverter(isolate, args, *sig, entry);
330328

331329
// if (!argConverter->IsValid()) {
332330
// JSToJavaConverter::Error err = argConverter->GetError();
333331
// throw NativeScriptException(err.msg);
334332
// }
335333

336-
auto isolate = args.GetIsolate();
337-
338334
JniLocalRef callerJavaObject;
339335

340-
jvalue *javaArgs = argConverter->ToArgs();
341-
336+
jvalue *javaArgs = argConverter.ToArgs();
342337

343-
auto runtime = Runtime::GetRuntime(isolate);
338+
auto runtime = Runtime::GetRuntimeFromIsolateData(isolate);
344339
auto objectManager = runtime->GetObjectManager();
345340

346341
if (!isStatic) {
@@ -542,8 +537,6 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
542537
// if ((++adjustMemCount % 2) == 0) {
543538
// AdjustAmountOfExternalAllocatedMemory(env, isolate);
544539
// }
545-
546-
delete argConverter;
547540
}
548541

549542
void CallbackHandlers::AdjustAmountOfExternalAllocatedMemory(JEnv &env, Isolate *isolate) {

test-app/runtime/src/main/cpp/MetadataNode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,6 @@ Local<FunctionTemplate> MetadataNode::GetConstructorFunctionTemplate(Isolate* is
922922

923923
auto node = GetOrCreateInternal(treeNode);
924924

925-
926925
JEnv env;
927926
// if we already have an exception (which will be rethrown later)
928927
// then we don't want to ignore the next exception
@@ -2172,6 +2171,7 @@ void MetadataNode::SymbolHasInstanceCallback(const v8::FunctionCallbackInfo<v8::
21722171
auto isolate = info.GetIsolate();
21732172
auto context = isolate->GetCurrentContext();
21742173
auto runtime = Runtime::GetRuntime(isolate);
2174+
21752175
auto objectManager = runtime->GetObjectManager();
21762176
auto obj = objectManager->GetJavaObjectByJsObject(arg->ToObject(context).ToLocalChecked());
21772177

test-app/runtime/src/main/cpp/Runtime.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ Runtime* Runtime::GetRuntime(int runtimeId) {
127127
}
128128

129129
Runtime* Runtime::GetRuntime(v8::Isolate* isolate) {
130-
auto itFound = s_isolate2RuntimesCache.find(isolate);
131-
auto runtime = (itFound != s_isolate2RuntimesCache.end())
132-
? itFound->second
133-
: nullptr;
130+
auto runtime = s_isolate2RuntimesCache.at(isolate);
134131

135132
if (runtime == nullptr) {
136133
stringstream ss;
@@ -141,19 +138,21 @@ Runtime* Runtime::GetRuntime(v8::Isolate* isolate) {
141138
return runtime;
142139
}
143140

144-
ObjectManager* Runtime::GetObjectManager(v8::Isolate* isolate) {
145-
auto itFound = s_isolate2RuntimesCache.find(isolate);
146-
auto runtime = (itFound != s_isolate2RuntimesCache.end())
147-
? itFound->second
148-
: nullptr;
141+
Runtime* Runtime::GetRuntimeFromIsolateData(v8::Isolate* isolate) {
142+
void* maybeRuntime = isolate->GetData((uint32_t)Runtime::IsolateData::RUNTIME);
143+
auto runtime = static_cast<Runtime*>(maybeRuntime);
149144

150145
if (runtime == nullptr) {
151146
stringstream ss;
152147
ss << "Cannot find runtime for isolate: " << isolate;
153148
throw NativeScriptException(ss.str());
154149
}
155150

156-
return runtime->GetObjectManager();
151+
return runtime;
152+
}
153+
154+
ObjectManager* Runtime::GetObjectManager(v8::Isolate* isolate) {
155+
return GetRuntime(isolate)->GetObjectManager();
157156
}
158157

159158
Isolate* Runtime::GetIsolate() const {
@@ -606,7 +605,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
606605
auto isolate = Isolate::New(create_params);
607606
isolateFrame.log("Isolate.New");
608607

609-
s_isolate2RuntimesCache.insert(make_pair(isolate, this));
608+
s_isolate2RuntimesCache[isolate] = this;
610609
v8::Locker locker(isolate);
611610
Isolate::Scope isolate_scope(isolate);
612611
HandleScope handleScope(isolate);
@@ -615,6 +614,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
615614

616615
// Sets a structure with v8 String constants on the isolate object at slot 1
617616
auto consts = new V8StringConstants::PerIsolateV8Constants(isolate);
617+
isolate->SetData((uint32_t)Runtime::IsolateData::RUNTIME, this);
618618
isolate->SetData((uint32_t)Runtime::IsolateData::CONSTANTS, consts);
619619

620620
V8::SetFlagsFromString(Constants::V8_STARTUP_FLAGS.c_str(), Constants::V8_STARTUP_FLAGS.size());
@@ -874,7 +874,7 @@ int Runtime::GetReader(){
874874
JavaVM* Runtime::s_jvm = nullptr;
875875
jmethodID Runtime::GET_USED_MEMORY_METHOD_ID = nullptr;
876876
map<int, Runtime*> Runtime::s_id2RuntimeCache;
877-
map<Isolate*, Runtime*> Runtime::s_isolate2RuntimesCache;
877+
unordered_map<Isolate*, Runtime*> Runtime::s_isolate2RuntimesCache;
878878
bool Runtime::s_mainThreadInitialized = false;
879879
v8::Platform* Runtime::platform = nullptr;
880880
int Runtime::m_androidVersion = Runtime::GetAndroidVersion();

test-app/runtime/src/main/cpp/Runtime.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Runtime {
2929

3030
static Runtime* GetRuntime(v8::Isolate* isolate);
3131

32+
static Runtime* GetRuntimeFromIsolateData(v8::Isolate* isolate);
33+
3234
static ObjectManager* GetObjectManager(v8::Isolate* isolate);
3335

3436
static void Init(JavaVM* vm, void* reserved);
@@ -117,7 +119,7 @@ class Runtime {
117119

118120
static std::map<int, Runtime*> s_id2RuntimeCache;
119121

120-
static std::map<v8::Isolate*, Runtime*> s_isolate2RuntimesCache;
122+
static std::unordered_map<v8::Isolate*, Runtime*> s_isolate2RuntimesCache;
121123

122124
static JavaVM* s_jvm;
123125

test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.cpp

+22-19
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ using namespace std;
1919
using namespace tns;
2020

2121
JSToJavaConverter::JSToJavaConverter(
22+
v8::Isolate* isolate,
2223
const FunctionCallbackInfo<v8::Value> &args,
2324
const string &methodSignature,
2425
MetadataEntry *entry)
2526
:
26-
m_isolate(args.GetIsolate()),
27+
m_isolate(isolate),
2728
m_methodSignature(methodSignature) {
2829
m_argsLen = args.Length();
2930

@@ -39,10 +40,9 @@ JSToJavaConverter::JSToJavaConverter(
3940
std::vector<std::string> parsed = parser.Parse();
4041
m_tokens = new std::vector<std::string>();
4142
m_tokens->reserve(parsed.size());
42-
for (std::string& p : parsed) {
43+
for (std::string &p: parsed) {
4344
m_tokens->push_back(p);
4445
}
45-
// std::copy(parsed.begin(), parsed.end(), m_tokens->begin());
4646
}
4747

4848
for (int i = 0; i < m_argsLen; i++) {
@@ -52,12 +52,13 @@ JSToJavaConverter::JSToJavaConverter(
5252
}
5353

5454
JSToJavaConverter::JSToJavaConverter(
55+
v8::Isolate* isolate,
5556
const v8::FunctionCallbackInfo<Value> &args,
5657
const string &methodSignature,
5758
MetadataEntry *entry,
5859
const v8::Local<v8::Object> kotlinExtensionFunctionThis)
5960
:
60-
m_isolate(args.GetIsolate()),
61+
m_isolate(isolate),
6162
m_methodSignature(methodSignature) {
6263

6364
int v8ProvidedArgumentsLength = args.Length();
@@ -75,7 +76,7 @@ JSToJavaConverter::JSToJavaConverter(
7576
auto parsed = parser.Parse();
7677
m_tokens = new std::vector<std::string>();
7778
m_tokens->reserve(parsed.size());
78-
for (std::string& p : parsed) {
79+
for (std::string &p: parsed) {
7980
m_tokens->push_back(p);
8081
}
8182
}
@@ -89,18 +90,19 @@ JSToJavaConverter::JSToJavaConverter(
8990
}
9091

9192
JSToJavaConverter::JSToJavaConverter(
93+
v8::Isolate* isolate,
9294
const v8::FunctionCallbackInfo<Value> &args,
9395
const string &methodSignature)
9496
:
95-
m_isolate(args.GetIsolate()),
97+
m_isolate(isolate),
9698
m_methodSignature(methodSignature) {
9799
m_argsLen = args.Length();
98100

99101
JniSignatureParser parser(m_methodSignature);
100102
auto parsed = parser.Parse();
101103
m_tokens = new std::vector<std::string>();
102104
m_tokens->reserve(parsed.size());
103-
for (std::string& p : parsed) {
105+
for (std::string &p: parsed) {
104106
m_tokens->push_back(p);
105107
}
106108

@@ -110,22 +112,20 @@ JSToJavaConverter::JSToJavaConverter(
110112
}
111113

112114
bool JSToJavaConverter::ConvertArg(const Local<Value> &arg, int index) {
113-
const auto &typeSignature = m_tokens->at(index);
114115

115-
if (arg->IsArray() && typeSignature[0] == '[') {
116-
return tns::ConvertJavaScriptArray(
116+
117+
if (arg->IsNumber() || arg->IsNumberObject()) {
118+
return tns::ConvertJavaScriptNumber(
117119
m_isolate,
118120
arg,
119121
index,
120122
m_tokens,
121123
m_jniArgRefsState
122124
);
123-
} else if (arg->IsNumber() || arg->IsNumberObject()) {
124-
return tns::ConvertJavaScriptNumber(
125-
m_isolate,
125+
} else if (arg->IsString() || arg->IsStringObject()) {
126+
return tns::ConvertJavaScriptString(
126127
arg,
127128
index,
128-
m_tokens,
129129
m_jniArgRefsState
130130
);
131131
} else if (arg->IsBoolean() || arg->IsBooleanObject()) {
@@ -136,10 +136,12 @@ bool JSToJavaConverter::ConvertArg(const Local<Value> &arg, int index) {
136136
m_tokens,
137137
m_jniArgRefsState
138138
);
139-
} else if (arg->IsString() || arg->IsStringObject()) {
140-
return tns::ConvertJavaScriptString(
139+
} else if (arg->IsArray() && m_tokens->at(index)[0] == '[') {
140+
return tns::ConvertJavaScriptArray(
141+
m_isolate,
141142
arg,
142143
index,
144+
m_tokens,
143145
m_jniArgRefsState
144146
);
145147
} else if (arg->IsObject()) {
@@ -155,10 +157,11 @@ bool JSToJavaConverter::ConvertArg(const Local<Value> &arg, int index) {
155157
} else if (arg->IsUndefined() || arg->IsNull()) {
156158
m_jniArgRefsState.SetConvertedObject(index, nullptr);
157159
return true;
158-
} else {
159-
m_jniArgRefsState.SetConvertedObject(index, nullptr);
160-
return false;
161160
}
161+
162+
m_jniArgRefsState.SetConvertedObject(index, nullptr);
163+
return false;
164+
162165
}
163166

164167
int JSToJavaConverter::Length() const {

test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.h

+3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ namespace tns {
1616
public:
1717

1818
JSToJavaConverter(
19+
v8::Isolate* isolate,
1920
const v8::FunctionCallbackInfo<v8::Value> &args,
2021
const std::string &methodSignature,
2122
MetadataEntry *entry);
2223

2324
JSToJavaConverter(
25+
v8::Isolate* isolate,
2426
const v8::FunctionCallbackInfo<v8::Value> &args,
2527
const std::string &methodSignature,
2628
MetadataEntry *entry,
2729
const v8::Local<v8::Object> kotlinExtensionFunctionThis);
2830

2931
JSToJavaConverter(
32+
v8::Isolate* isolate,
3033
const v8::FunctionCallbackInfo<v8::Value> &args,
3134
const std::string &methodSignature);
3235

0 commit comments

Comments
 (0)