Skip to content

Commit 17a5625

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Refactor how we promote shadow tree revisions as latest for JS consistency (#54833)
Summary: Changelog: [General][Changed] - Refactor how shadow tree revisions are promoted as latest for JS consistency Changes `LazyShadowTreeRevisionConsistencyManager::updateCurrentRevision` to pull the latest commited revision for a given surface id instead of accepting the new revision as a parameter. This way, the new revision is read from the same place for every update, which opens up the way to implement commit branching. This change will allow to always read from the JS tree revision, if it exists. Differential Revision: D88151490
1 parent 80e384a commit 17a5625

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ void UIManager::completeSurface(
188188
ShadowTree::CommitOptions commitOptions) {
189189
TraceSection s("UIManager::completeSurface", "surfaceId", surfaceId);
190190

191+
ShadowTree::CommitStatus result;
192+
191193
shadowTreeRegistry_.visit(surfaceId, [&](const ShadowTree& shadowTree) {
192-
auto result = shadowTree.commit(
194+
result = shadowTree.commit(
193195
[&](const RootShadowNode& oldRootShadowNode) {
194196
return std::make_shared<RootShadowNode>(
195197
oldRootShadowNode,
@@ -199,20 +201,19 @@ void UIManager::completeSurface(
199201
});
200202
},
201203
commitOptions);
204+
});
202205

203-
if (result == ShadowTree::CommitStatus::Succeeded) {
204-
// It's safe to update the visible revision of the shadow tree immediately
205-
// after we commit a specific one.
206-
lazyShadowTreeRevisionConsistencyManager_->updateCurrentRevision(
207-
surfaceId, shadowTree.getCurrentRevision().rootShadowNode);
206+
if (result == ShadowTree::CommitStatus::Succeeded) {
207+
// It's safe to update the visible revision of the shadow tree immediately
208+
// after we commit a specific one.
209+
lazyShadowTreeRevisionConsistencyManager_->updateCurrentRevision(surfaceId);
208210

209-
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
210-
if (auto animationBackend = animationBackend_.lock()) {
211-
animationBackend->clearRegistry(surfaceId);
212-
}
211+
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
212+
if (auto animationBackend = animationBackend_.lock()) {
213+
animationBackend->clearRegistry(surfaceId);
213214
}
214215
}
215-
});
216+
}
216217
}
217218

218219
void UIManager::setIsJSResponder(

packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,32 @@ LazyShadowTreeRevisionConsistencyManager::
1515
ShadowTreeRegistry& shadowTreeRegistry)
1616
: shadowTreeRegistry_(shadowTreeRegistry) {}
1717

18-
void LazyShadowTreeRevisionConsistencyManager::updateCurrentRevision(
19-
SurfaceId surfaceId,
20-
RootShadowNode::Shared rootShadowNode) {
18+
std::shared_ptr<const RootShadowNode>
19+
LazyShadowTreeRevisionConsistencyManager::updateCurrentRevision(
20+
SurfaceId surfaceId) {
21+
// This method is only going to be called from JS, so we don't need to protect
22+
// the access to the shadow tree registry as well.
23+
// If this was multi-threaded, we would need to protect it to avoid capturing
24+
// root shadow nodes concurrently.
25+
RootShadowNode::Shared rootShadowNode;
26+
shadowTreeRegistry_.visit(surfaceId, [&](const ShadowTree& shadowTree) {
27+
rootShadowNode = shadowTree.getCurrentRevision().rootShadowNode;
28+
});
29+
2130
std::unique_lock lock(capturedRootShadowNodesForConsistencyMutex_);
2231

2332
// We don't need to store the revision if we haven't locked.
2433
// We can resolve lazily when requested.
2534
if (lockCount > 0) {
26-
capturedRootShadowNodesForConsistency_[surfaceId] =
27-
std::move(rootShadowNode);
35+
capturedRootShadowNodesForConsistency_[surfaceId] = rootShadowNode;
2836
}
37+
38+
return rootShadowNode;
2939
}
3040

3141
#pragma mark - ShadowTreeRevisionProvider
3242

33-
RootShadowNode::Shared
43+
std::shared_ptr<const RootShadowNode>
3444
LazyShadowTreeRevisionConsistencyManager::getCurrentRevision(
3545
SurfaceId surfaceId) {
3646
{
@@ -43,23 +53,7 @@ LazyShadowTreeRevisionConsistencyManager::getCurrentRevision(
4353
}
4454
}
4555

46-
// This method is only going to be called from JS, so we don't need to protect
47-
// the access to the shadow tree registry as well.
48-
// If this was multi-threaded, we would need to protect it to avoid capturing
49-
// root shadow nodes concurrently.
50-
RootShadowNode::Shared rootShadowNode;
51-
shadowTreeRegistry_.visit(surfaceId, [&](const ShadowTree& shadowTree) {
52-
rootShadowNode = shadowTree.getCurrentRevision().rootShadowNode;
53-
});
54-
55-
{
56-
std::unique_lock lock(capturedRootShadowNodesForConsistencyMutex_);
57-
if (lockCount > 0) {
58-
capturedRootShadowNodesForConsistency_[surfaceId] = rootShadowNode;
59-
}
60-
}
61-
62-
return rootShadowNode;
56+
return updateCurrentRevision(surfaceId);
6357
}
6458

6559
#pragma mark - ConsistentShadowTreeRevisionProvider

packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class LazyShadowTreeRevisionConsistencyManager : public ShadowTreeRevisionConsis
2828
public:
2929
explicit LazyShadowTreeRevisionConsistencyManager(ShadowTreeRegistry &shadowTreeRegistry);
3030

31-
void updateCurrentRevision(SurfaceId surfaceId, RootShadowNode::Shared rootShadowNode);
31+
std::shared_ptr<const RootShadowNode> updateCurrentRevision(SurfaceId surfaceId);
3232

3333
#pragma mark - ShadowTreeRevisionProvider
3434

35-
RootShadowNode::Shared getCurrentRevision(SurfaceId surfaceId) override;
35+
std::shared_ptr<const RootShadowNode> getCurrentRevision(SurfaceId surfaceId) override;
3636

3737
#pragma mark - ShadowTreeRevisionConsistencyManager
3838

packages/react-native/ReactCommon/react/renderer/uimanager/consistency/tests/LazyShadowTreeRevisionConsistencyManagerTest.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ TEST_F(
141141

142142
EXPECT_EQ(consistencyManager_.getCurrentRevision(0), nullptr);
143143

144-
consistencyManager_.updateCurrentRevision(0, newRootShadowNode);
144+
consistencyManager_.updateCurrentRevision(0);
145145

146146
EXPECT_NE(consistencyManager_.getCurrentRevision(0), nullptr);
147147
EXPECT_EQ(
@@ -176,7 +176,7 @@ TEST_F(
176176

177177
EXPECT_EQ(consistencyManager_.getCurrentRevision(0), nullptr);
178178

179-
consistencyManager_.updateCurrentRevision(0, newRootShadowNode);
179+
consistencyManager_.updateCurrentRevision(0);
180180

181181
EXPECT_EQ(
182182
consistencyManager_.getCurrentRevision(0).get(), newRootShadowNode.get());
@@ -192,7 +192,7 @@ TEST_F(
192192
{});
193193
});
194194

195-
consistencyManager_.updateCurrentRevision(0, newRootShadowNode2);
195+
consistencyManager_.updateCurrentRevision(0);
196196

197197
EXPECT_EQ(
198198
consistencyManager_.getCurrentRevision(0).get(),
@@ -265,7 +265,7 @@ TEST_F(
265265
EXPECT_EQ(
266266
consistencyManager_.getCurrentRevision(0).get(), newRootShadowNode.get());
267267

268-
consistencyManager_.updateCurrentRevision(0, newRootShadowNode2);
268+
consistencyManager_.updateCurrentRevision(0);
269269

270270
// Updated
271271
EXPECT_EQ(
@@ -344,7 +344,9 @@ TEST_F(LazyShadowTreeRevisionConsistencyManagerTest, testUpdateToUnmounted) {
344344
EXPECT_EQ(
345345
consistencyManager_.getCurrentRevision(0).get(), newRootShadowNode.get());
346346

347-
consistencyManager_.updateCurrentRevision(0, nullptr);
347+
shadowTreeRegistry_.remove(0);
348+
349+
consistencyManager_.updateCurrentRevision(0);
348350

349351
// Updated
350352
EXPECT_EQ(consistencyManager_.getCurrentRevision(0).get(), nullptr);

0 commit comments

Comments
 (0)