From d8fe0d2ee28cc5739134f01879bc197d51f7a09c Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 24 Apr 2025 05:32:48 -0700 Subject: [PATCH] simplify parent-child flattening-unflattening (#50893) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50893 changelog: [internal] In the two branches changed by this diff, it is unnecessary to check `reparentMode` because `reparentMode` and `childReparentMode` will always mismatch. When `childReparentMode` is unflatten, `reparentMode` will be flatten and vice versa. This is guaranteed by a `if else` statement above: https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp#L674 This slightly simplifies code and lowers the burden on the reader. Reviewed By: javache, rubennorte Differential Revision: D73571160 fbshipit-source-id: 8623d991f7b063f70c95720ca0d3b63318dfc08b --- .../renderer/mounting/Differentiator.cpp | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp index 97ec74788db..9cbbeef5422 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp @@ -699,9 +699,10 @@ static void calculateShadowViewMutationsFlattener( } } - // Unflatten parent, flatten child if (childReparentMode == ReparentMode::Flatten) { - auto parentTagForUpdateWhenUnflattened = + // Unflatten parent, flatten child + react_native_assert(reparentMode == ReparentMode::Unflatten); + auto fixedParentTagForUpdate = ReactNativeFeatureFlags:: enableFixForParentTagDuringReparenting() ? newTreeNodePair.shadowView.tag @@ -715,22 +716,18 @@ static void calculateShadowViewMutationsFlattener( scope, ReparentMode::Flatten, mutationContainer, - (reparentMode == ReparentMode::Flatten - ? parentTag - : newTreeNodePair.shadowView.tag), + newTreeNodePair.shadowView.tag, unvisitedRecursiveChildPairs, oldTreeNodePair, - (reparentMode == ReparentMode::Flatten - ? oldTreeNodePair.shadowView.tag - : parentTagForUpdateWhenUnflattened), + fixedParentTagForUpdate, subVisitedNewMap, subVisitedOldMap, - cullingContext.adjustCullingContextIfNeeded(oldTreeNodePair)); - } - // Flatten parent, unflatten child - else { + adjustedNewCullingContext); + } else { + // Flatten parent, unflatten child + react_native_assert(reparentMode == ReparentMode::Flatten); // Unflatten old list into new tree - auto parentTagForUpdateWhenFlattened = + auto fixedParentTagForUpdate = ReactNativeFeatureFlags:: enableFixForParentTagDuringReparenting() ? parentTagForUpdate @@ -739,21 +736,13 @@ static void calculateShadowViewMutationsFlattener( scope, /* reparentMode */ ReparentMode::Unflatten, mutationContainer, - /* parentTag */ - (reparentMode == ReparentMode::Flatten - ? parentTag - : newTreeNodePair.shadowView.tag), + parentTag, /* unvisitedOtherNodes */ unvisitedRecursiveChildPairs, /* node */ newTreeNodePair, - /* parentTagForUpdate */ - (reparentMode == ReparentMode::Flatten - ? parentTagForUpdateWhenFlattened - : parentTag), + /* parentTagForUpdate */ fixedParentTagForUpdate, /* parentSubVisitedOtherNewNodes */ subVisitedNewMap, /* parentSubVisitedOtherOldNodes */ subVisitedOldMap, - reparentMode == ReparentMode::Flatten - ? adjustedOldCullingContext - : adjustedNewCullingContext); + /* cullingContext */ adjustedOldCullingContext); // If old nodes were not visited, we know that we can delete them // now. They will be removed from the hierarchy by the outermost