From f8c5fa37d9d8465b0881fde4732b2670ecdde313 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Fri, 3 May 2019 12:27:24 -0700 Subject: [PATCH] Fabric: `insertedPairs` in `calculateShadowViewMutations` now stores pointers (not values) Summary: This is a small micro-optimization in Diffing algorithm. Seems we don't need to store full ShadowView objects in `insertedPairs` map, we can store only pointers to them. That can save memory and CPU cycles because we will not need to store full objects and copy shared pointers (which is somewhat expensive). Reviewed By: mdvacca Differential Revision: D15200498 fbshipit-source-id: 2a268c3ee80755555bff3317e10e679be1cf9830 --- ReactCommon/fabric/mounting/Differentiator.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ReactCommon/fabric/mounting/Differentiator.cpp b/ReactCommon/fabric/mounting/Differentiator.cpp index a53c2acb0ea..a0dbe407bdf 100644 --- a/ReactCommon/fabric/mounting/Differentiator.cpp +++ b/ReactCommon/fabric/mounting/Differentiator.cpp @@ -67,8 +67,10 @@ static void calculateShadowViewMutations( return; } - better::map insertedPairs; - int index = 0; + auto index = int{0}; + + // Maps inserted node tags to pointers to them in `newChildPairs`. + auto insertedPairs = better::map{}; ShadowViewMutationList createMutations = {}; ShadowViewMutationList deleteMutations = {}; @@ -118,20 +120,20 @@ static void calculateShadowViewMutations( insertMutations.push_back(ShadowViewMutation::InsertMutation( parentShadowView, newChildPair.shadowView, index)); - insertedPairs.insert({newChildPair.shadowView.tag, newChildPair}); + insertedPairs.insert({newChildPair.shadowView.tag, &newChildPair}); } // Stage 3: Collecting `Delete` and `Remove` mutations for (index = lastIndexAfterFirstStage; index < oldChildPairs.size(); index++) { - const auto &oldChildPair = oldChildPairs[index]; + auto const &oldChildPair = oldChildPairs[index]; // Even if the old view was (re)inserted, we have to generate `remove` // mutation. removeMutations.push_back(ShadowViewMutation::RemoveMutation( parentShadowView, oldChildPair.shadowView, index)); - const auto &it = insertedPairs.find(oldChildPair.shadowView.tag); + auto const it = insertedPairs.find(oldChildPair.shadowView.tag); if (it == insertedPairs.end()) { // The old view was *not* (re)inserted. @@ -151,7 +153,8 @@ static void calculateShadowViewMutations( // The old view *was* (re)inserted. // We have to call the algorithm recursively if the inserted view // is *not* the same as removed one. - const auto &newChildPair = it->second; + auto const &newChildPair = *it->second; + if (newChildPair != oldChildPair) { const auto oldGrandChildPairs = sliceChildShadowNodeViewPairs(*oldChildPair.shadowNode);