Skip to content

Commit 3d1c8b0

Browse files
authored
chore(🧹): remove RNFRuntimeCache (#278)
1 parent b326ac7 commit 3d1c8b0

14 files changed

Lines changed: 152 additions & 250 deletions

‎packages/webgpu/android/CMakeLists.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ add_library(${PACKAGE_NAME} SHARED
4646
../cpp/rnwgpu/RNWebGPUManager.cpp
4747
../cpp/jsi/RNFPromise.cpp
4848
../cpp/jsi/RNFHybridObject.cpp
49-
../cpp/jsi/RNFRuntimeCache.cpp
50-
../cpp/jsi/RNFWorkletRuntimeRegistry.cpp
49+
../cpp/jsi/RNFRuntimeState.cpp
5150
../cpp/rnwgpu/async/AsyncRunner.cpp
5251
../cpp/rnwgpu/async/AsyncTaskHandle.cpp
5352
../cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp

‎packages/webgpu/cpp/jsi/RNFHybridObject.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,22 @@ void HybridObject::ensureInitialized(facebook::jsi::Runtime& runtime) {
129129
if (!_didLoadMethods) {
130130
[[unlikely]];
131131
_creationRuntime = &runtime;
132+
_runtimeState = rnwgpu::RNFRuntimeState::get(runtime);
132133
// lazy-load all exposed methods
133134
loadHybridMethods();
134135
_didLoadMethods = true;
135136
}
136137
}
137138

138139
bool HybridObject::isRuntimeAlive() {
139-
if (_creationRuntime == nullptr) {
140-
[[unlikely]];
141-
return false;
140+
return !_runtimeState.expired();
141+
}
142+
143+
facebook::jsi::Runtime* HybridObject::getCreationRuntime() const {
144+
if (_runtimeState.expired()) {
145+
return nullptr;
142146
}
143-
return RNFWorkletRuntimeRegistry::isRuntimeAlive(_creationRuntime);
147+
return _creationRuntime;
144148
}
145149

146150
} // namespace margelo

‎packages/webgpu/cpp/jsi/RNFHybridObject.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#pragma once
66

77
#include "WGPULogger.h"
8-
#include "RNFWorkletRuntimeRegistry.h"
8+
#include "RNFRuntimeState.h"
99
#include <functional>
1010
#include <jsi/jsi.h>
1111
#include <memory>
@@ -91,8 +91,9 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
9191

9292
protected:
9393
const char* _name = TAG;
94-
// Store a pointer to the runtime. Needed for checking if the runtime is still active, see WorkletRuntimeRegistry.
94+
// Store a pointer to the runtime for convenience; ensure it is only used while the runtime state is alive.
9595
jsi::Runtime* _creationRuntime = nullptr;
96+
std::weak_ptr<rnwgpu::RNFRuntimeState> _runtimeState;
9697

9798
private:
9899
inline void ensureInitialized(facebook::jsi::Runtime& runtime);
@@ -128,6 +129,10 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
128129
};
129130
}
130131

132+
protected:
133+
facebook::jsi::Runtime* getCreationRuntime() const;
134+
std::weak_ptr<rnwgpu::RNFRuntimeState> getRuntimeStateWeak() const { return _runtimeState; }
135+
131136
protected:
132137
template <typename Derived, typename ReturnType, typename... Args>
133138
void registerHybridMethod(std::string name, ReturnType (Derived::*method)(Args...), Derived* derivedInstance, bool override = false) {

‎packages/webgpu/cpp/jsi/RNFJSIConverter.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "RNFEnumMapper.h"
2323
#include "RNFJSIHelper.h"
2424
#include "RNFPromise.h"
25-
#include "RNFWorkletRuntimeRegistry.h"
2625

2726
#include "Unions.h"
2827

‎packages/webgpu/cpp/jsi/RNFJSIHelper.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include "RNFWorkletRuntimeRegistry.h"
7+
#include "RNFRuntimeState.h"
88
#include <jsi/jsi.h>
99
#include <memory>
1010
#include <utility>
@@ -20,8 +20,10 @@ class JSIHelper {
2020
* Every jsi::Function you intend to share or hold should be wrapped using this function.
2121
*/
2222
static std::shared_ptr<jsi::Function> createSharedJsiFunction(jsi::Runtime& runtime, jsi::Function&& function) {
23-
std::shared_ptr<jsi::Function> sharedFunction(new jsi::Function(std::move(function)), [&runtime](jsi::Function* ptr) {
24-
if (RNFWorkletRuntimeRegistry::isRuntimeAlive(&runtime)) {
23+
auto runtimeState = rnwgpu::RNFRuntimeState::get(runtime);
24+
std::weak_ptr<rnwgpu::RNFRuntimeState> runtimeStateWeak = runtimeState;
25+
std::shared_ptr<jsi::Function> sharedFunction(new jsi::Function(std::move(function)), [runtimeStateWeak](jsi::Function* ptr) {
26+
if (!runtimeStateWeak.expired()) {
2527
// Only delete the jsi::Function when the runtime it created is still alive.
2628
// Otherwise leak memory. We do this on purpose, as sometimes we would keep
2729
// references to JSI objects past the lifetime of its runtime (e.g.,

‎packages/webgpu/cpp/jsi/RNFRuntimeCache.cpp‎

Lines changed: 0 additions & 57 deletions
This file was deleted.

‎packages/webgpu/cpp/jsi/RNFRuntimeCache.h‎

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "RNFRuntimeState.h"
2+
3+
namespace rnwgpu {
4+
5+
const facebook::jsi::UUID RNFRuntimeState::kRuntimeStateKey = facebook::jsi::UUID();
6+
7+
std::shared_ptr<RNFRuntimeState> RNFRuntimeState::get(facebook::jsi::Runtime& runtime) {
8+
auto existing = runtime.getRuntimeData(kRuntimeStateKey);
9+
if (existing) {
10+
return std::static_pointer_cast<RNFRuntimeState>(existing);
11+
}
12+
13+
auto state = std::shared_ptr<RNFRuntimeState>(new RNFRuntimeState());
14+
runtime.setRuntimeData(kRuntimeStateKey, state);
15+
return state;
16+
}
17+
18+
} // namespace rnwgpu
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)