Skip to content

Commit b248dc4

Browse files
authoredNov 28, 2023
feat: migrate to faster maps and use runtime context (#1793)
* feat: migrate to faster maps and use runtime context * fix: missing include
1 parent ca4b6bd commit b248dc4

22 files changed

+86
-82
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ ArgConverter::TypeLongOperationsCache* ArgConverter::GetTypeLongCache(v8::Isolat
200200
auto itFound = s_type_long_operations_cache.find(isolate);
201201
if (itFound == s_type_long_operations_cache.end()) {
202202
cache = new TypeLongOperationsCache;
203-
s_type_long_operations_cache.insert(make_pair(isolate, cache));
203+
s_type_long_operations_cache.emplace(isolate, cache);
204204
} else {
205205
cache = itFound->second;
206206
}
@@ -230,4 +230,4 @@ void ArgConverter::onDisposeIsolate(Isolate* isolate) {
230230
}
231231
}
232232

233-
std::map<Isolate*, ArgConverter::TypeLongOperationsCache*> ArgConverter::s_type_long_operations_cache;
233+
robin_hood::unordered_map<Isolate*, ArgConverter::TypeLongOperationsCache*> ArgConverter::s_type_long_operations_cache;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class ArgConverter {
124124
* "s_type_long_operations_cache" used to keep function
125125
* dealing with operations concerning java long -> javascript number.
126126
*/
127-
static std::map<v8::Isolate*, TypeLongOperationsCache*> s_type_long_operations_cache;
127+
static robin_hood::unordered_map<v8::Isolate*, TypeLongOperationsCache*> s_type_long_operations_cache;
128128
};
129129
}
130130

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ int CallbackHandlers::RunOnMainThreadFdCallback(int fd, int events, void *data)
685685
Isolate::Scope isolate_scope(isolate);
686686
HandleScope handle_scope(isolate);
687687
Local<v8::Function> cb = it->second.callback_.Get(isolate);
688-
v8::Local<v8::Context> context = cb->GetCreationContextChecked();
688+
Runtime* runtime = Runtime::GetRuntime(isolate);
689+
v8::Local<v8::Context> context = runtime->GetContext();
689690
Context::Scope context_scope(context);
690691
// erase the it here as we're already done with its values and the callback might invalidate the iterator
691692
cache_.erase(it);
@@ -1025,7 +1026,7 @@ void CallbackHandlers::NewThreadCallback(const v8::FunctionCallbackInfo<v8::Valu
10251026

10261027
auto persistentWorker = new Persistent<Object>(isolate, thiz);
10271028

1028-
id2WorkerMap.insert(make_pair(workerId, persistentWorker));
1029+
id2WorkerMap.emplace(workerId, persistentWorker);
10291030

10301031
DEBUG_WRITE("Called Worker constructor id=%d", workerId);
10311032

@@ -1740,7 +1741,7 @@ std::atomic_uint64_t CallbackHandlers::frameCallbackCount_ = {0};
17401741

17411742

17421743
int CallbackHandlers::nextWorkerId = 0;
1743-
std::map<int, Persistent<Object> *> CallbackHandlers::id2WorkerMap;
1744+
robin_hood::unordered_map<int, Persistent<Object> *> CallbackHandlers::id2WorkerMap;
17441745

17451746
short CallbackHandlers::MAX_JAVA_STRING_ARRAY_LENGTH = 100;
17461747
jclass CallbackHandlers::RUNTIME_CLASS = nullptr;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <errno.h>
1919
#include "NativeScriptAssert.h"
2020
#include "NativeScriptException.h"
21+
#include "Runtime.h"
2122

2223
namespace tns {
2324
class CallbackHandlers {
@@ -27,7 +28,7 @@ namespace tns {
2728
* Stores persistent handles of all 'Worker' objects initialized on the main thread
2829
* Note: No isolates different than that of the main thread should access this map
2930
*/
30-
static std::map<int, v8::Persistent<v8::Object> *> id2WorkerMap;
31+
static robin_hood::unordered_map<int, v8::Persistent<v8::Object> *> id2WorkerMap;
3132

3233
static int nextWorkerId;
3334

@@ -364,7 +365,8 @@ namespace tns {
364365
v8::Isolate::Scope isolate_scope(isolate);
365366
v8::HandleScope handle_scope(isolate);
366367
v8::Local<v8::Function> cb = entry->callback_.Get(isolate);
367-
v8::Local<v8::Context> context = cb->GetCreationContextChecked();
368+
Runtime* runtime = Runtime::GetRuntime(isolate);
369+
v8::Local<v8::Context> context = runtime->GetContext();
368370
v8::Context::Scope context_scope(context);
369371
// we're running the callback now, so it's not scheduled anymore
370372
entry->markUnscheduled();

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ jclass JEnv::CheckForClassInCache(const string &className) {
769769

770770
jclass JEnv::InsertClassIntoCache(const string &className, jclass &tmp) {
771771
auto global_class = reinterpret_cast<jclass>(m_env->NewGlobalRef(tmp));
772-
s_classCache.insert(make_pair(className, global_class));
772+
s_classCache.emplace(className, global_class);
773773
m_env->DeleteLocalRef(tmp);
774774

775775
return global_class;
@@ -788,7 +788,7 @@ jthrowable JEnv::CheckForClassMissingCache(const string &className) {
788788

789789
jthrowable JEnv::InsertClassIntoMissingCache(const string &className,const jthrowable &tmp) {
790790
auto throwable = reinterpret_cast<jthrowable>(m_env->NewGlobalRef(tmp));
791-
s_missingClasses.insert(make_pair(className, throwable));
791+
s_missingClasses.emplace(className, throwable);
792792
m_env->DeleteLocalRef(tmp);
793793

794794
return throwable;
@@ -855,8 +855,8 @@ void JEnv::CheckForJavaException() {
855855
}
856856

857857
JavaVM *JEnv::s_jvm = nullptr;
858-
map<string, jclass> JEnv::s_classCache;
859-
map<string, jthrowable> JEnv::s_missingClasses;
858+
robin_hood::unordered_map<string, jclass> JEnv::s_classCache;
859+
robin_hood::unordered_map<string, jthrowable> JEnv::s_missingClasses;
860860
jclass JEnv::RUNTIME_CLASS = nullptr;
861861
jmethodID JEnv::GET_CACHED_CLASS_METHOD_ID = nullptr;
862862

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define JENV_H_
33

44
#include "jni.h"
5-
#include <map>
5+
#include "robin_hood.h"
66
#include <string>
77

88
namespace tns {
@@ -342,8 +342,8 @@ class JEnv {
342342

343343
static jmethodID GET_CACHED_CLASS_METHOD_ID;
344344

345-
static std::map<std::string, jclass> s_classCache;
346-
static std::map<std::string, jthrowable> s_missingClasses;
345+
static robin_hood::unordered_map<std::string, jclass> s_classCache;
346+
static robin_hood::unordered_map<std::string, jthrowable> s_missingClasses;
347347
};
348348
}
349349

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ MetadataNode* MetadataNode::GetOrCreate(const string& className) {
9797

9898
node = GetOrCreateInternal(treeNode);
9999

100-
s_name2NodeCache.insert(make_pair(className, node));
100+
s_name2NodeCache.emplace(className, node);
101101
} else {
102102
node = it->second;
103103
}
@@ -115,7 +115,7 @@ MetadataNode* MetadataNode::GetOrCreateInternal(MetadataTreeNode* treeNode) {
115115
} else {
116116
result = new MetadataNode(treeNode);
117117

118-
s_treeNode2NodeCache.insert(make_pair(treeNode, result));
118+
s_treeNode2NodeCache.emplace(treeNode, result);
119119
}
120120

121121
return result;
@@ -131,7 +131,7 @@ MetadataTreeNode* MetadataNode::GetOrCreateTreeNodeByName(const string& classNam
131131
} else {
132132
result = s_metadataReader.GetOrCreateTreeNodeByName(className);
133133

134-
s_name2TreeNodeCache.insert(make_pair(className, result));
134+
s_name2TreeNodeCache.emplace(className, result);
135135
}
136136

137137
return result;
@@ -993,7 +993,7 @@ Local<FunctionTemplate> MetadataNode::GetConstructorFunctionTemplate(Isolate* is
993993
ctorFuncTemplate.Clear();
994994
auto pft = new Persistent<FunctionTemplate>(isolate, ctorFuncTemplate);
995995
CtorCacheData ctorCacheItem(pft, instanceMethodsCallbackData);
996-
cache->CtorFuncCache.insert(make_pair(treeNode, ctorCacheItem));
996+
cache->CtorFuncCache.emplace(treeNode, ctorCacheItem);
997997
return ctorFuncTemplate;
998998
}
999999

@@ -1060,7 +1060,7 @@ Local<FunctionTemplate> MetadataNode::GetConstructorFunctionTemplate(Isolate* is
10601060
//cache "ctorFuncTemplate"
10611061
auto pft = new Persistent<FunctionTemplate>(isolate, ctorFuncTemplate);
10621062
CtorCacheData ctorCacheItem(pft, instanceMethodsCallbackData);
1063-
cache->CtorFuncCache.insert(make_pair(treeNode, ctorCacheItem));
1063+
cache->CtorFuncCache.emplace(treeNode, ctorCacheItem);
10641064

10651065
SetInnerTypes(isolate, wrappedCtorFunc, treeNode);
10661066

@@ -1735,11 +1735,11 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo<v8::Value
17351735
SetTypeMetadata(isolate, extendFunc, new TypeMetadata(fullExtendedName));
17361736
info.GetReturnValue().Set(extendFunc);
17371737

1738-
s_name2NodeCache.insert(make_pair(fullExtendedName, node));
1738+
s_name2NodeCache.emplace(fullExtendedName, node);
17391739

17401740
ExtendedClassCacheData cacheData(extendFunc, fullExtendedName, node);
17411741
auto cache = GetMetadataNodeCache(isolate);
1742-
cache->ExtendedCtorFuncCache.insert(make_pair(fullExtendedName, cacheData));
1742+
cache->ExtendedCtorFuncCache.emplace(fullExtendedName, cacheData);
17431743

17441744
if (frame.check()) {
17451745
frame.log("Extending: " + node->m_name);
@@ -2027,7 +2027,7 @@ MetadataNode::MetadataNodeCache* MetadataNode::GetMetadataNodeCache(Isolate* iso
20272027
auto itFound = s_metadata_node_cache.find(isolate);
20282028
if (itFound == s_metadata_node_cache.end()) {
20292029
cache = new MetadataNodeCache;
2030-
s_metadata_node_cache.insert(make_pair(isolate, cache));
2030+
s_metadata_node_cache.emplace(isolate, cache);
20312031
} else {
20322032
cache = itFound->second;
20332033
}
@@ -2293,10 +2293,10 @@ void MetadataNode::onDisposeIsolate(Isolate* isolate) {
22932293

22942294
string MetadataNode::TNS_PREFIX = "com/tns/gen/";
22952295
MetadataReader MetadataNode::s_metadataReader;
2296-
std::map<std::string, MetadataNode*> MetadataNode::s_name2NodeCache;
2297-
std::map<std::string, MetadataTreeNode*> MetadataNode::s_name2TreeNodeCache;
2298-
std::map<MetadataTreeNode*, MetadataNode*> MetadataNode::s_treeNode2NodeCache;
2299-
map<Isolate*, MetadataNode::MetadataNodeCache*> MetadataNode::s_metadata_node_cache;
2296+
robin_hood::unordered_map<std::string, MetadataNode*> MetadataNode::s_name2NodeCache;
2297+
robin_hood::unordered_map<std::string, MetadataTreeNode*> MetadataNode::s_name2TreeNodeCache;
2298+
robin_hood::unordered_map<MetadataTreeNode*, MetadataNode*> MetadataNode::s_treeNode2NodeCache;
2299+
robin_hood::unordered_map<Isolate*, MetadataNode::MetadataNodeCache*> MetadataNode::s_metadata_node_cache;
23002300
bool MetadataNode::s_profilerEnabled = false;
2301-
std::map<Isolate*, Persistent<ObjectTemplate>*> MetadataNode::s_arrayObjectTemplates;
2301+
robin_hood::unordered_map<Isolate*, Persistent<ObjectTemplate>*> MetadataNode::s_arrayObjectTemplates;
23022302

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,18 @@ class MetadataNode {
170170
PrototypeTemplateFiller& protoFiller);
171171

172172
MetadataTreeNode* m_treeNode;
173-
std::map<v8::Isolate*, v8::Persistent<v8::Function>*> m_poCtorCachePerIsolate;
173+
robin_hood::unordered_map<v8::Isolate*, v8::Persistent<v8::Function>*> m_poCtorCachePerIsolate;
174174
std::string m_name;
175175
std::string m_implType;
176176
bool m_isArray;
177177

178178
static std::string TNS_PREFIX;
179179
static MetadataReader s_metadataReader;
180-
static std::map<std::string, MetadataNode*> s_name2NodeCache;
181-
static std::map<std::string, MetadataTreeNode*> s_name2TreeNodeCache;
182-
static std::map<MetadataTreeNode*, MetadataNode*> s_treeNode2NodeCache;
183-
static std::map<v8::Isolate*, MetadataNodeCache*> s_metadata_node_cache;
184-
static std::map<v8::Isolate*, v8::Persistent<v8::ObjectTemplate>*> s_arrayObjectTemplates;
180+
static robin_hood::unordered_map<std::string, MetadataNode*> s_name2NodeCache;
181+
static robin_hood::unordered_map<std::string, MetadataTreeNode*> s_name2TreeNodeCache;
182+
static robin_hood::unordered_map<MetadataTreeNode*, MetadataNode*> s_treeNode2NodeCache;
183+
static robin_hood::unordered_map<v8::Isolate*, MetadataNodeCache*> s_metadata_node_cache;
184+
static robin_hood::unordered_map<v8::Isolate*, v8::Persistent<v8::ObjectTemplate>*> s_arrayObjectTemplates;
185185
static bool s_profilerEnabled;
186186

187187
struct MethodCallbackData {
@@ -263,9 +263,9 @@ class MetadataNode {
263263
struct MetadataNodeCache {
264264
v8::Persistent<v8::String>* MetadataKey;
265265

266-
std::map<MetadataTreeNode*, CtorCacheData> CtorFuncCache;
266+
robin_hood::unordered_map<MetadataTreeNode*, CtorCacheData> CtorFuncCache;
267267

268-
std::map<std::string, MetadataNode::ExtendedClassCacheData> ExtendedCtorFuncCache;
268+
robin_hood::unordered_map<std::string, MetadataNode::ExtendedClassCacheData> ExtendedCtorFuncCache;
269269
};
270270
};
271271
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ string MetadataReader::ReadTypeName(MetadataTreeNode* treeNode) {
178178
} else {
179179
name = ReadTypeNameInternal(treeNode);
180180

181-
m_typeNameCache.insert(make_pair(treeNode, name));
181+
m_typeNameCache.emplace(treeNode, name);
182182
}
183183

184184
return name;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <map>
77
#include <string>
88
#include <assert.h>
9+
#include "robin_hood.h"
910
namespace tns {
1011
typedef std::vector<std::string> (*GetTypeMetadataCallback)(const std::string& classname, int index);
1112

@@ -186,7 +187,7 @@ class MetadataReader {
186187
std::vector<MetadataTreeNode*> m_v;
187188
GetTypeMetadataCallback m_getTypeMetadataCallback;
188189

189-
std::map<MetadataTreeNode*, std::string> m_typeNameCache;
190+
robin_hood::unordered_map<MetadataTreeNode*, std::string> m_typeNameCache;
190191
};
191192
}
192193

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ MethodCache::CacheMethodInfo MethodCache::ResolveMethodSignature(const string& c
5454
:
5555
env.GetMethodID(clazz, methodName, signature);
5656

57-
s_mthod_ctor_signature_cache.insert(make_pair(encoded_method_signature, method_info));
57+
s_mthod_ctor_signature_cache.emplace(encoded_method_signature, method_info);
5858
}
5959
} else {
6060
method_info = (*it).second;
@@ -81,7 +81,7 @@ MethodCache::CacheMethodInfo MethodCache::ResolveConstructorSignature(const Args
8181
constructor_info.signature = signature;
8282
constructor_info.mid = env.GetMethodID(javaClass, "<init>", signature);
8383

84-
s_mthod_ctor_signature_cache.insert(make_pair(encoded_ctor_signature, constructor_info));
84+
s_mthod_ctor_signature_cache.emplace(encoded_ctor_signature, constructor_info);
8585
}
8686
} else {
8787
constructor_info = (*it).second;
@@ -261,7 +261,7 @@ string MethodCache::ResolveConstructor(const FunctionCallbackInfo<Value>& args,
261261
return resolvedSignature;
262262
}
263263

264-
map<string, MethodCache::CacheMethodInfo> MethodCache::s_mthod_ctor_signature_cache;
264+
robin_hood::unordered_map<string, MethodCache::CacheMethodInfo> MethodCache::s_mthod_ctor_signature_cache;
265265
jclass MethodCache::RUNTIME_CLASS = nullptr;
266266
jmethodID MethodCache::RESOLVE_METHOD_OVERLOAD_METHOD_ID = nullptr;
267267
jmethodID MethodCache::RESOLVE_CONSTRUCTOR_SIGNATURE_ID = nullptr;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class MethodCache {
5959
* Used for caching the resolved constructor or method signature.
6060
* The encoded signature has template: <className>.S/I.<methodName>.<argsCount>.<arg1class>.<...>
6161
*/
62-
static std::map<std::string, CacheMethodInfo> s_mthod_ctor_signature_cache;
62+
static robin_hood::unordered_map<std::string, CacheMethodInfo> s_mthod_ctor_signature_cache;
6363
};
6464
}
6565

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Local<Function> ModuleInternal::GetRequireFunction(Isolate* isolate, const strin
132132

133133
auto poFunc = new Persistent<Function>(isolate, requireFunc);
134134

135-
m_requireCache.insert(make_pair(dirName, poFunc));
135+
m_requireCache.emplace(dirName, poFunc);
136136
}
137137

138138
return requireFunc;
@@ -446,7 +446,7 @@ Local<Object> ModuleInternal::LoadData(Isolate* isolate, const string& path) {
446446

447447
auto poObj = new Persistent<Object>(isolate, json);
448448

449-
m_loadedModules.insert(make_pair(path, ModuleCacheEntry(poObj, true /* isData */)));
449+
m_loadedModules.emplace(path, ModuleCacheEntry(poObj, true /* isData */));
450450

451451
return json;
452452
}

0 commit comments

Comments
 (0)
Please sign in to comment.