Skip to content

Commit 0969e95

Browse files
committed
chore: optimized AppPackageJson key lookup
1 parent 9c78f9f commit 0969e95

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

NativeScript/runtime/Runtime.mm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "URLImpl.h"
2929
#include "URLPatternImpl.h"
3030
#include "URLSearchParamsImpl.h"
31+
#include <mutex>
3132

3233
#define STRINGIZE(x) #x
3334
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
@@ -116,6 +117,8 @@ static void InitializeImportMetaObject(Local<Context> context, Local<Module> mod
116117
std::atomic<int> Runtime::nextIsolateId{0};
117118
SimpleAllocator allocator_;
118119
NSDictionary* AppPackageJson = nil;
120+
static std::unordered_map<std::string, id> AppConfigCache; // generic cache for app config values
121+
static std::mutex AppConfigCacheMutex;
119122

120123
// Global flag to track when JavaScript errors occur during execution
121124
bool jsErrorOccurred = false;
@@ -442,7 +445,27 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
442445
}
443446
}
444447

445-
id result = AppPackageJson[[NSString stringWithUTF8String:key.c_str()]];
448+
// Generic cache for all keys to avoid repeated NSString conversion and NSDictionary hashing
449+
{
450+
std::lock_guard<std::mutex> lock(AppConfigCacheMutex);
451+
auto it = AppConfigCache.find(key);
452+
if (it != AppConfigCache.end()) {
453+
return it->second;
454+
}
455+
}
456+
457+
id result = nil;
458+
if (AppPackageJson != nil) {
459+
NSString* nsKey = [NSString stringWithUTF8String:key.c_str()];
460+
result = AppPackageJson[nsKey];
461+
}
462+
463+
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache as-is)
464+
{
465+
std::lock_guard<std::mutex> lock(AppConfigCacheMutex);
466+
AppConfigCache[key] = result;
467+
}
468+
446469
return result;
447470
}
448471

0 commit comments

Comments
 (0)