Files
nitro/packages/react-native-nitro-modules/cpp/utils/PropNameIDCache.cpp
T
Marc RousavyandGitHub 1e22bbeb1e perf: Cache HybridFunction's PropNameID (#1127)
* perf: Cache `HybridFunction`'s `PropNameID`

* fix: Use utf8 prop name
2025-12-23 16:26:18 +01:00

39 lines
1000 B
C++

//
// PropNameIDCache.cpp
// react-native-nitro
//
// Created by Marc Rousavy on 17.10.25.
//
#include "PropNameIDCache.hpp"
#include "JSICache.hpp"
namespace margelo::nitro {
using namespace facebook;
std::unordered_map<jsi::Runtime*, PropNameIDCache::CacheMap> PropNameIDCache::_cache;
const jsi::PropNameID& PropNameIDCache::get(jsi::Runtime& runtime, const std::string& value) {
CacheMap& cache = _cache[&runtime];
const auto& cachedName = cache.find(value);
if (cachedName != cache.end()) {
// cache warm!
const BorrowingReference<jsi::PropNameID>& value = cachedName->second;
return *value;
}
// not cached - create the jsi::PropNameID...
auto propName = jsi::PropNameID::forUtf8(runtime, value);
auto jsiCache = JSICache::getOrCreateCache(runtime);
auto sharedPropName = jsiCache.makeShared(std::move(propName));
// store it in cache...
cache.emplace(value, sharedPropName);
// return it!
return *sharedPropName;
}
} // namespace margelo::nitro