Files
nitro/packages/react-native-nitro-modules/cpp/utils/PropNameIDCache.cpp
T
Marc RousavyandGitHub c94263763d perf: Cache jsi::PropNameIDs (makes Nitro 30% faster) (#1114)
* feat: Create `PropNameID` Cache

* Impl PropNameID

* fix: Use `const jsi::PropNameID&` instead of `BorrowingReference`

* perf: Use `PropNameIDCache` everywhere internally

* fix: Make use of `PropNameIDCache` in custom user defined structs

* fix: Use `PropNameIDCache` for Hybrid Views too

* fix: `jsi::Runtime` deref

* fix: Allow `JSICache` to use itself by caching itself first

* fix: Remove `allowCache` flag

* perf: Avoid `BorrowingReference<T>` copy and use `const T&`

* perf: Use `PropNameIDCache` inside `defineProperty(...)`

* docs: Add docs
2025-12-18 21:36:27 +01:00

39 lines
994 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, 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::forAscii(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