Skip to content

Commit 2f82403

Browse files
committed
perf(runtime): inline trivial accessors used on every bridge call
Runtime::GetIsolate/GetJavaRuntime/GetObjectManager/GetContext/GetId, JEnv's JNIEnv* conversion and the MetadataEntry getters were out-of-line calls in the runtime's own library, made several times per JS <-> Java call. They are now inline; the MetadataEntry getters inline only the check for an already-read value and keep reading the metadata out of line. The Runtime definitions sit after the class so its declarations are left as they are.
1 parent 4692bfa commit 2f82403

6 files changed

Lines changed: 71 additions & 48 deletions

File tree

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ void JEnv::UnpinThreadEnv() {
4242
s_thread.pinned = false;
4343
}
4444

45-
JEnv::operator JNIEnv* () const {
46-
return m_env;
47-
}
48-
4945
jmethodID JEnv::GetMethodID(jclass clazz, const string &name, const string &sig) {
5046
jmethodID mid = m_env->GetMethodID(clazz, name.c_str(), sig.c_str());
5147
CheckForJavaException();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class JEnv {
1616

1717
~JEnv() = default;
1818

19-
operator JNIEnv* () const;
19+
operator JNIEnv* () const {
20+
return m_env;
21+
}
2022

2123
jclass GetObjectClass(jobject obj);
2224

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ MetadataEntry::MetadataEntry(MetadataTreeNode *m_treeNode, NodeType nodeType) :
1212
paramCount(-1), isFinal(false), isResolved(false), retTypeParsed(false),
1313
isFinalSet(false), isResolvedSet(false) {}
1414

15-
std::string &MetadataEntry::getName() {
16-
if (!name.empty()) return name;
17-
15+
std::string &MetadataEntry::readName() {
1816
auto reader = MetadataNode::getMetadataReader();
1917

2018
if (type == NodeType::Field || type == NodeType::StaticField) {
@@ -26,9 +24,7 @@ std::string &MetadataEntry::getName() {
2624
return name;
2725
}
2826

29-
std::string &MetadataEntry::getSig() {
30-
if (!sig.empty()) return sig;
31-
27+
std::string &MetadataEntry::readSig() {
3228
auto reader = MetadataNode::getMetadataReader();
3329

3430
if (type == NodeType::Field || type == NodeType::StaticField) {
@@ -43,9 +39,7 @@ std::string &MetadataEntry::getSig() {
4339
return sig;
4440
}
4541

46-
std::string &MetadataEntry::getReturnType() {
47-
if (!returnType.empty()) return returnType;
48-
42+
std::string &MetadataEntry::readReturnType() {
4943
auto reader = MetadataNode::getMetadataReader();
5044

5145
if (type == NodeType::Method) {
@@ -59,8 +53,7 @@ std::string &MetadataEntry::getReturnType() {
5953
return returnType;
6054
}
6155

62-
MethodReturnType MetadataEntry::getRetType() {
63-
if (retTypeParsed) return retType;
56+
MethodReturnType MetadataEntry::readRetType() {
6457
auto reader = MetadataNode::getMetadataReader();
6558

6659
if (type == NodeType::Method && !this->getReturnType().empty()) {
@@ -72,9 +65,7 @@ MethodReturnType MetadataEntry::getRetType() {
7265
return retType;
7366
}
7467

75-
std::string &MetadataEntry::getDeclaringType() {
76-
if (!declaringType.empty()) return declaringType;
77-
68+
std::string &MetadataEntry::readDeclaringType() {
7869
auto reader = MetadataNode::getMetadataReader();
7970

8071
if (type == NodeType::StaticField) {
@@ -88,9 +79,7 @@ std::string &MetadataEntry::getDeclaringType() {
8879
return declaringType;
8980
}
9081

91-
int MetadataEntry::getParamCount() {
92-
if (paramCount != -1) return paramCount;
93-
82+
int MetadataEntry::readParamCount() {
9483
auto reader = MetadataNode::getMetadataReader();
9584

9685
if (type == NodeType::Method) {
@@ -105,9 +94,7 @@ int MetadataEntry::getParamCount() {
10594
return paramCount;
10695
}
10796

108-
bool MetadataEntry::getIsFinal() {
109-
if (isFinalSet) return isFinal;
110-
97+
bool MetadataEntry::readIsFinal() {
11198
if (type == NodeType::Field || type == NodeType::StaticField) {
11299
auto reader = MetadataNode::getMetadataReader();
113100
isFinal = (reader->FieldFlags(field) & FieldInfo::FINAL) != 0;
@@ -118,9 +105,7 @@ bool MetadataEntry::getIsFinal() {
118105
return isFinal;
119106
}
120107

121-
bool MetadataEntry::getIsResolved() {
122-
if (isResolvedSet) return isResolved;
123-
108+
bool MetadataEntry::readIsResolved() {
124109
auto reader = MetadataNode::getMetadataReader();
125110
if (type == NodeType::Method) {
126111
isResolved = mi.CheckIsResolved() == 1;

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,37 @@ namespace tns {
6565
return *this;
6666
}
6767

68-
std::string &getName();
68+
std::string &getName() {
69+
return !name.empty() ? name : readName();
70+
}
6971

70-
std::string &getSig();
72+
std::string &getSig() {
73+
return !sig.empty() ? sig : readSig();
74+
}
7175

72-
std::string &getReturnType();
76+
std::string &getReturnType() {
77+
return !returnType.empty() ? returnType : readReturnType();
78+
}
7379

74-
MethodReturnType getRetType();
80+
MethodReturnType getRetType() {
81+
return retTypeParsed ? retType : readRetType();
82+
}
7583

76-
std::string &getDeclaringType();
84+
std::string &getDeclaringType() {
85+
return !declaringType.empty() ? declaringType : readDeclaringType();
86+
}
7787

78-
int getParamCount();
88+
int getParamCount() {
89+
return paramCount != -1 ? paramCount : readParamCount();
90+
}
7991

80-
bool getIsFinal();
92+
bool getIsFinal() {
93+
return isFinalSet ? isFinal : readIsFinal();
94+
}
8195

82-
bool getIsResolved();
96+
bool getIsResolved() {
97+
return isResolvedSet ? isResolved : readIsResolved();
98+
}
8399

84100
MetadataTreeNode *treeNode;
85101
NodeType type;
@@ -105,6 +121,22 @@ namespace tns {
105121

106122
private:
107123

124+
std::string &readName();
125+
126+
std::string &readSig();
127+
128+
std::string &readReturnType();
129+
130+
MethodReturnType readRetType();
131+
132+
std::string &readDeclaringType();
133+
134+
int readParamCount();
135+
136+
bool readIsFinal();
137+
138+
bool readIsResolved();
139+
108140
bool retTypeParsed;
109141
bool isFinalSet;
110142
bool isResolvedSet;

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ ObjectManager* Runtime::GetObjectManager(v8::Isolate* isolate) {
212212
return GetRuntime(isolate)->GetObjectManager();
213213
}
214214

215-
Isolate* Runtime::GetIsolate() const { return m_isolate; }
216-
217-
jobject Runtime::GetJavaRuntime() const { return m_runtime; }
218-
219-
ObjectManager* Runtime::GetObjectManager() const { return m_objectManager; }
220-
221215
void Runtime::Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath,
222216
jstring nativeLibDir, jboolean verboseLoggingEnabled,
223217
jboolean isDebuggable, jstring packageName,
@@ -1406,12 +1400,6 @@ void Runtime::DestroyRuntime() {
14061400
RuntimeGates::Close(m_id);
14071401
}
14081402

1409-
Local<Context> Runtime::GetContext() {
1410-
return this->m_context->Get(this->m_isolate);
1411-
}
1412-
1413-
int Runtime::GetId() { return this->m_id; }
1414-
14151403

14161404
JavaVM* Runtime::s_jvm = nullptr;
14171405
robin_hood::unordered_map<int, Runtime*> Runtime::s_id2RuntimeCache;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,26 @@ class Runtime {
478478
std::mutex m_fileWriteMutex;
479479
#endif
480480
};
481+
482+
inline v8::Isolate* Runtime::GetIsolate() const {
483+
return m_isolate;
484+
}
485+
486+
inline jobject Runtime::GetJavaRuntime() const {
487+
return m_runtime;
488+
}
489+
490+
inline ObjectManager* Runtime::GetObjectManager() const {
491+
return m_objectManager;
492+
}
493+
494+
inline int Runtime::GetId() {
495+
return m_id;
496+
}
497+
498+
inline v8::Local<v8::Context> Runtime::GetContext() {
499+
return m_context->Get(m_isolate);
500+
}
481501
}
482502

483503
#endif /*#ifndef RUNTIME_H_*/

0 commit comments

Comments
 (0)