From 67387c3c91a743f89dcdbfac9d4e9456c25573b8 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 12 Sep 2023 06:55:37 -0700 Subject: [PATCH] Implement findDescendantNode without cloning the tree (#39356) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39356 changelog: [internal] `findDescendantNode` shouldn't clone the tree. Reviewed By: javache Differential Revision: D49095341 fbshipit-source-id: 4cd6aa6f9ba0f96d708e6a6c6b0cd55f03d8b5b1 --- .../tests/StateReconciliationTest.cpp | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/tests/StateReconciliationTest.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/tests/StateReconciliationTest.cpp index 9b74efad03ee0c..bba5f9a5b533b6 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/tests/StateReconciliationTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/tests/StateReconciliationTest.cpp @@ -36,23 +36,31 @@ class DummyShadowTreeDelegate : public ShadowTreeDelegate { bool mountSynchronously) const override{}; }; -inline const ShadowNode* findDescendantNode( +namespace { +const ShadowNode* findDescendantNode( const ShadowNode& shadowNode, const ShadowNodeFamily& family) { - const ShadowNode* result = nullptr; - shadowNode.cloneTree(family, [&](const ShadowNode& oldShadowNode) { - result = &oldShadowNode; - return oldShadowNode.clone({}); - }); - return result; + if (&shadowNode.getFamily() == &family) { + return &shadowNode; + } + + for (auto childNode : shadowNode.getChildren()) { + auto descendant = findDescendantNode(*childNode, family); + if (descendant != nullptr) { + return descendant; + } + } + + return nullptr; } -inline const ShadowNode* findDescendantNode( +const ShadowNode* findDescendantNode( const ShadowTree& shadowTree, const ShadowNodeFamily& family) { return findDescendantNode( *shadowTree.getCurrentRevision().rootShadowNode, family); } +} // namespace TEST(StateReconciliationTest, testStateReconciliation) { auto builder = simpleComponentBuilder();