Skip to content

Commit 86a52cc

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
Scope LongLivedObjectCollection per runtime [3/n] (#43410)
Summary: Pull Request resolved: #43410 Changelog: [General] [Breaking] - Make `LongLivedObjectCollection::get` accept a Runtime reference as parameter. # Context Approach 1 as described in [RFC post](https://fb.workplace.com/groups/615693552291894/permalink/1693347124526526/). # This diff * Replace the `LongLivedObjectCollection` singleton with a map from `Runtime -> LongLivedObjectCollection` so that each RN instance has its own collection. * Update MSFT fork accordingly Reviewed By: javache, RSNara Differential Revision: D54649209 fbshipit-source-id: ecd2ab3917843ca82388b7b9cce06c05679f2d60
1 parent da21799 commit 86a52cc

8 files changed

Lines changed: 31 additions & 14 deletions

File tree

packages/react-native/ReactCommon/react/bridging/CallbackWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CallbackWrapper : public LongLivedObject {
3838
std::shared_ptr<CallInvoker> jsInvoker) {
3939
auto wrapper = std::shared_ptr<CallbackWrapper>(new CallbackWrapper(
4040
std::move(callback), runtime, std::move(jsInvoker)));
41-
LongLivedObjectCollection::get().add(wrapper);
41+
LongLivedObjectCollection::get(runtime).add(wrapper);
4242
return wrapper;
4343
}
4444

packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
*/
77

88
#include "LongLivedObject.h"
9+
#include <unordered_map>
910

1011
namespace facebook::react {
1112

1213
// LongLivedObjectCollection
13-
LongLivedObjectCollection& LongLivedObjectCollection::get() {
14-
static LongLivedObjectCollection instance;
15-
return instance;
14+
15+
LongLivedObjectCollection& LongLivedObjectCollection::get(
16+
jsi::Runtime& runtime) {
17+
static std::unordered_map<void*, std::shared_ptr<LongLivedObjectCollection>>
18+
instances;
19+
void* key = static_cast<void*>(&runtime);
20+
auto entry = instances.find(key);
21+
if (entry == instances.end()) {
22+
entry =
23+
instances.emplace(key, std::make_shared<LongLivedObjectCollection>())
24+
.first;
25+
}
26+
return *(entry->second);
1627
}
1728

1829
void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) {
@@ -43,7 +54,7 @@ size_t LongLivedObjectCollection::size() const {
4354
// LongLivedObject
4455

4556
void LongLivedObject::allowRelease() {
46-
LongLivedObjectCollection::get().remove(this);
57+
LongLivedObjectCollection::get(runtime_).remove(this);
4758
}
4859

4960
} // namespace facebook::react

packages/react-native/ReactCommon/react/bridging/LongLivedObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class LongLivedObject {
4242
*/
4343
class LongLivedObjectCollection {
4444
public:
45-
static LongLivedObjectCollection& get();
45+
static LongLivedObjectCollection& get(jsi::Runtime& runtime);
4646

4747
LongLivedObjectCollection() = default;
4848
LongLivedObjectCollection(const LongLivedObjectCollection&) = delete;

packages/react-native/ReactCommon/react/bridging/Promise.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AsyncPromise {
3636

3737
auto promiseHolder =
3838
std::make_shared<PromiseHolder>(rt, promise.asObject(rt));
39-
LongLivedObjectCollection::get().add(promiseHolder);
39+
LongLivedObjectCollection::get(rt).add(promiseHolder);
4040

4141
// The shared state can retain the promise holder weakly now.
4242
state_->promiseHolder = promiseHolder;

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ TEST_F(BridgingTest, asyncCallbackInvalidation) {
316316
[](jsi::Runtime& rt, jsi::Function& f) { f.call(rt, "hello"); });
317317

318318
// LongLivedObjectCollection goes away before callback is executed
319-
LongLivedObjectCollection::get().clear();
319+
LongLivedObjectCollection::get(rt).clear();
320320

321321
flushQueue();
322322

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class BridgingTest : public ::testing::Test {
4343
rt(*runtime) {}
4444

4545
~BridgingTest() {
46-
LongLivedObjectCollection::get().clear();
46+
LongLivedObjectCollection::get(rt).clear();
4747
}
4848

4949
void TearDown() override {
5050
flushQueue();
5151

5252
// After flushing the invoker queue, we shouldn't leak memory.
53-
EXPECT_EQ(0, LongLivedObjectCollection::get().size());
53+
EXPECT_EQ(0, LongLivedObjectCollection::get(rt).size());
5454
}
5555

5656
jsi::Value eval(const std::string& js) {

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ class BridgelessNativeModuleProxy : public jsi::HostObject {
6767
*/
6868

6969
TurboModuleBinding::TurboModuleBinding(
70+
jsi::Runtime& runtime,
7071
TurboModuleProviderFunctionType&& moduleProvider,
7172
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection)
72-
: moduleProvider_(std::move(moduleProvider)),
73+
: runtime_(runtime),
74+
moduleProvider_(std::move(moduleProvider)),
7375
longLivedObjectCollection_(std::move(longLivedObjectCollection)) {}
7476

7577
void TurboModuleBinding::install(
@@ -85,7 +87,7 @@ void TurboModuleBinding::install(
8587
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
8688
1,
8789
[binding = TurboModuleBinding(
88-
std::move(moduleProvider), longLivedObjectCollection)](
90+
runtime, std::move(moduleProvider), longLivedObjectCollection)](
8991
jsi::Runtime& rt,
9092
const jsi::Value& thisVal,
9193
const jsi::Value* args,
@@ -102,7 +104,9 @@ void TurboModuleBinding::install(
102104
bool rnTurboInterop = legacyModuleProvider != nullptr;
103105
auto turboModuleBinding = legacyModuleProvider
104106
? std::make_unique<TurboModuleBinding>(
105-
std::move(legacyModuleProvider), longLivedObjectCollection)
107+
runtime,
108+
std::move(legacyModuleProvider),
109+
longLivedObjectCollection)
106110
: nullptr;
107111
auto nativeModuleProxy = std::make_shared<BridgelessNativeModuleProxy>(
108112
std::move(turboModuleBinding));
@@ -119,7 +123,7 @@ TurboModuleBinding::~TurboModuleBinding() {
119123
if (longLivedObjectCollection_) {
120124
longLivedObjectCollection_->clear();
121125
} else {
122-
LongLivedObjectCollection::get().clear();
126+
LongLivedObjectCollection::get(runtime_).clear();
123127
}
124128
}
125129

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TurboModuleBinding {
3434
nullptr);
3535

3636
TurboModuleBinding(
37+
jsi::Runtime& runtime,
3738
TurboModuleProviderFunctionType&& moduleProvider,
3839
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection);
3940

@@ -49,6 +50,7 @@ class TurboModuleBinding {
4950
jsi::Value getModule(jsi::Runtime& runtime, const std::string& moduleName)
5051
const;
5152

53+
jsi::Runtime& runtime_;
5254
TurboModuleProviderFunctionType moduleProvider_;
5355
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection_;
5456
};

0 commit comments

Comments
 (0)