|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <jsi/jsi.h> |
| 4 | + |
| 5 | +#include <memory> |
| 6 | +#include <mutex> |
| 7 | +#include <unordered_map> |
| 8 | +#include <unordered_set> |
| 9 | + |
| 10 | +namespace rnwgpu { |
| 11 | + |
| 12 | +namespace jsi = facebook::jsi; |
| 13 | + |
| 14 | +/** |
| 15 | + * Runtime state management using Hermes' runtime.setRuntimeData API. |
| 16 | + * This replaces the old RuntimeLifecycleMonitor/RuntimeAwareCache system. |
| 17 | + */ |
| 18 | +class RNFRuntimeState { |
| 19 | +public: |
| 20 | + // UUID key for storing our runtime state |
| 21 | + static const jsi::UUID kRuntimeStateKey; |
| 22 | + |
| 23 | + /** |
| 24 | + * Get or create the runtime state for the given runtime |
| 25 | + */ |
| 26 | + static std::shared_ptr<RNFRuntimeState> get(jsi::Runtime& runtime); |
| 27 | + |
| 28 | + /** |
| 29 | + * Template cache that can store any type T per object pointer |
| 30 | + */ |
| 31 | + template <typename T> |
| 32 | + class ObjectCache { |
| 33 | + public: |
| 34 | + std::shared_ptr<T> getOrCreate(void* object) { |
| 35 | + std::lock_guard<std::mutex> lock(mutex_); |
| 36 | + auto it = cache_.find(object); |
| 37 | + if (it != cache_.end()) { |
| 38 | + return it->second; |
| 39 | + } |
| 40 | + |
| 41 | + auto value = std::make_shared<T>(); |
| 42 | + cache_[object] = value; |
| 43 | + return value; |
| 44 | + } |
| 45 | + |
| 46 | + void remove(void* object) { |
| 47 | + std::lock_guard<std::mutex> lock(mutex_); |
| 48 | + cache_.erase(object); |
| 49 | + } |
| 50 | + |
| 51 | + void clear() { |
| 52 | + std::lock_guard<std::mutex> lock(mutex_); |
| 53 | + cache_.clear(); |
| 54 | + } |
| 55 | + |
| 56 | + private: |
| 57 | + std::mutex mutex_; |
| 58 | + std::unordered_map<void*, std::shared_ptr<T>> cache_; |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * Get or create a cache for a specific type T |
| 63 | + */ |
| 64 | + template <typename T> |
| 65 | + std::shared_ptr<ObjectCache<T>> getCache() { |
| 66 | + std::lock_guard<std::mutex> lock(mutex_); |
| 67 | + |
| 68 | + // Use type_info as key for the cache type |
| 69 | + const std::type_info& typeId = typeid(T); |
| 70 | + auto it = typeCaches_.find(&typeId); |
| 71 | + |
| 72 | + if (it != typeCaches_.end()) { |
| 73 | + return std::static_pointer_cast<ObjectCache<T>>(it->second); |
| 74 | + } |
| 75 | + |
| 76 | + auto cache = std::make_shared<ObjectCache<T>>(); |
| 77 | + typeCaches_[&typeId] = cache; |
| 78 | + return cache; |
| 79 | + } |
| 80 | + |
| 81 | +private: |
| 82 | + RNFRuntimeState() = default; |
| 83 | + |
| 84 | + std::mutex mutex_; |
| 85 | + // Map from type_info to cache instance |
| 86 | + std::unordered_map<const std::type_info*, std::shared_ptr<void>> typeCaches_; |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * Template helper for runtime-aware caching compatible with the old API |
| 91 | + * This provides a migration path from RuntimeAwareCache |
| 92 | + */ |
| 93 | +template <typename T> |
| 94 | +class RuntimeAwareCache { |
| 95 | +public: |
| 96 | + T& get(jsi::Runtime& rt) { |
| 97 | + auto state = RNFRuntimeState::get(rt); |
| 98 | + auto cache = state->getCache<T>(); |
| 99 | + |
| 100 | + // For compatibility, we use the runtime pointer as the object key |
| 101 | + auto ptr = cache->getOrCreate(&rt); |
| 102 | + return *ptr; |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +} // namespace rnwgpu |
0 commit comments