Skip to content

Commit 6729a3e

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Reviving of RN_SHADOW_TREE_INTROSPECTION in DEBUG mode
Summary: Shadow tree introspection was disabled for a while, now we need it back working. This diff also restructures the logic of `MountingCoordinator::pullTransaction()` splitting it into two sections, first one for the base case and the second for the overriding case. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D23374948 fbshipit-source-id: 0b5f1c598975bceb3dcb6a0eaee67ff58ef9dda1
1 parent bc3251c commit 6729a3e

File tree

2 files changed

+79
-78
lines changed

2 files changed

+79
-78
lines changed

ReactCommon/react/renderer/mounting/MountingCoordinator.cpp

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -80,98 +80,98 @@ void MountingCoordinator::resetLatestRevision() const {
8080
lastRevision_.reset();
8181
}
8282

83-
#ifdef RN_SHADOW_TREE_INTROSPECTION
84-
void MountingCoordinator::validateTransactionAgainstStubViewTree(
85-
ShadowViewMutationList const &mutations,
86-
bool assertEquality) const {
87-
std::string line;
88-
89-
std::stringstream ssMutations(getDebugDescription(mutations, {}));
90-
while (std::getline(ssMutations, line, '\n')) {
91-
LOG(ERROR) << "Mutations:" << line;
92-
}
93-
94-
stubViewTree_.mutate(mutations);
95-
auto stubViewTree =
96-
stubViewTreeFromShadowNode(lastRevision_->getRootShadowNode());
83+
better::optional<MountingTransaction> MountingCoordinator::pullTransaction()
84+
const {
85+
std::lock_guard<std::mutex> lock(mutex_);
9786

98-
std::stringstream ssOldTree(
99-
baseRevision_.getRootShadowNode().getDebugDescription());
100-
while (std::getline(ssOldTree, line, '\n')) {
101-
LOG(ERROR) << "Old tree:" << line;
102-
}
87+
auto transaction = better::optional<MountingTransaction>{};
10388

104-
std::stringstream ssNewTree(
105-
lastRevision_->getRootShadowNode().getDebugDescription());
106-
while (std::getline(ssNewTree, line, '\n')) {
107-
LOG(ERROR) << "New tree:" << line;
108-
}
89+
// Base case
90+
if (lastRevision_.has_value()) {
91+
number_++;
10992

110-
if (assertEquality) {
111-
assert(stubViewTree_ == stubViewTree);
112-
}
113-
}
114-
#endif
93+
auto telemetry = lastRevision_->getTelemetry();
11594

116-
better::optional<MountingTransaction> MountingCoordinator::pullTransaction()
117-
const {
118-
std::lock_guard<std::mutex> lock(mutex_);
95+
telemetry.willDiff();
11996

120-
auto mountingOverrideDelegate = mountingOverrideDelegate_.lock();
97+
auto mutations = calculateShadowViewMutations(
98+
baseRevision_.getRootShadowNode(), lastRevision_->getRootShadowNode());
12199

122-
bool shouldOverridePullTransaction = mountingOverrideDelegate &&
123-
mountingOverrideDelegate->shouldOverridePullTransaction();
100+
telemetry.didDiff();
124101

125-
if (!shouldOverridePullTransaction && !lastRevision_.has_value()) {
126-
return {};
127-
}
102+
baseRevision_ = std::move(*lastRevision_);
103+
lastRevision_.reset();
128104

129-
number_++;
130-
131-
ShadowViewMutation::List diffMutations{};
132-
auto telemetry =
133-
(lastRevision_.hasValue() ? lastRevision_->getTelemetry()
134-
: TransactionTelemetry{});
135-
if (!lastRevision_.hasValue()) {
136-
telemetry.willLayout();
137-
telemetry.didLayout();
138-
telemetry.willCommit();
139-
telemetry.didCommit();
140-
}
141-
telemetry.willDiff();
142-
if (lastRevision_.hasValue()) {
143-
diffMutations = calculateShadowViewMutations(
144-
baseRevision_.getRootShadowNode(),
145-
lastRevision_->getRootShadowNode(),
146-
enableReparentingDetection_);
105+
transaction = MountingTransaction{
106+
surfaceId_, number_, std::move(mutations), telemetry};
147107
}
148-
telemetry.didDiff();
149108

150-
better::optional<MountingTransaction> transaction{};
109+
// Override case
110+
auto mountingOverrideDelegate = mountingOverrideDelegate_.lock();
111+
auto shouldOverridePullTransaction = mountingOverrideDelegate &&
112+
mountingOverrideDelegate->shouldOverridePullTransaction();
151113

152-
// The override delegate can provide custom mounting instructions,
153-
// even if there's no `lastRevision_`. Consider cases of animation frames
154-
// in between React tree updates.
155114
if (shouldOverridePullTransaction) {
115+
auto mutations = ShadowViewMutation::List{};
116+
auto telemetry = TransactionTelemetry{};
117+
118+
if (transaction.has_value()) {
119+
mutations = transaction->getMutations();
120+
telemetry = transaction->getTelemetry();
121+
} else {
122+
telemetry.willLayout();
123+
telemetry.didLayout();
124+
telemetry.willCommit();
125+
telemetry.didCommit();
126+
}
127+
156128
transaction = mountingOverrideDelegate->pullTransaction(
157-
surfaceId_, number_, telemetry, std::move(diffMutations));
158-
} else if (lastRevision_.hasValue()) {
159-
transaction = MountingTransaction{
160-
surfaceId_, number_, std::move(diffMutations), telemetry};
129+
surfaceId_, number_, telemetry, std::move(mutations));
161130
}
162131

163-
if (lastRevision_.hasValue()) {
164132
#ifdef RN_SHADOW_TREE_INTROSPECTION
165-
// Only validate non-animated transactions - it's garbage to validate
166-
// animated transactions, since the stub view tree likely won't match
167-
// the committed tree during an animation.
168-
this->validateTransactionAgainstStubViewTree(
169-
transaction->getMutations(), !shouldOverridePullTransaction);
170-
#endif
171-
172-
baseRevision_ = std::move(*lastRevision_);
173-
lastRevision_.reset();
133+
if (transaction.has_value()) {
134+
// We have something to validate.
135+
auto mutations = transaction->getMutations();
136+
137+
// No matter what the source of the transaction is, it must be able to
138+
// mutate the existing stub view tree.
139+
stubViewTree_.mutate(mutations);
140+
141+
// If the transaction was overridden, we don't have a model of the shadow
142+
// tree therefore we cannot validate the validity of the mutation
143+
// instructions.
144+
if (!shouldOverridePullTransaction) {
145+
auto line = std::string{};
146+
147+
auto stubViewTree =
148+
stubViewTreeFromShadowNode(baseRevision_.getRootShadowNode());
149+
150+
if (stubViewTree_ != stubViewTree) {
151+
std::stringstream ssOldTree(
152+
baseRevision_.getRootShadowNode().getDebugDescription());
153+
while (std::getline(ssOldTree, line, '\n')) {
154+
LOG(ERROR) << "Old tree:" << line;
155+
}
156+
157+
std::stringstream ssMutations(getDebugDescription(mutations, {}));
158+
while (std::getline(ssMutations, line, '\n')) {
159+
LOG(ERROR) << "Mutations:" << line;
160+
}
161+
162+
std::stringstream ssNewTree(
163+
lastRevision_->getRootShadowNode().getDebugDescription());
164+
while (std::getline(ssNewTree, line, '\n')) {
165+
LOG(ERROR) << "New tree:" << line;
166+
}
167+
}
168+
169+
assert(
170+
(stubViewTree_ == stubViewTree) &&
171+
"Incorrect set of mutations detected.");
172+
}
174173
}
174+
#endif
175175

176176
return transaction;
177177
}

ReactCommon/react/renderer/mounting/MountingCoordinator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include <react/renderer/mounting/TelemetryController.h>
1818
#include "ShadowTreeRevision.h"
1919

20+
#ifndef NDEBUG
21+
#define RN_SHADOW_TREE_INTROSPECTION 1
22+
#endif
23+
2024
#ifdef RN_SHADOW_TREE_INTROSPECTION
2125
#include <react/renderer/mounting/stubs.h>
2226
#endif
@@ -113,9 +117,6 @@ class MountingCoordinator final {
113117
bool enableReparentingDetection_{false}; // temporary
114118

115119
#ifdef RN_SHADOW_TREE_INTROSPECTION
116-
void validateTransactionAgainstStubViewTree(
117-
ShadowViewMutationList const &mutations,
118-
bool assertEquality) const;
119120
mutable StubViewTree stubViewTree_; // Protected by `mutex_`.
120121
#endif
121122
};

0 commit comments

Comments
 (0)