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
This commit is contained in:
Valentin Shergin 2019-05-03 12:27:24 -07:00 коммит произвёл Facebook Github Bot
Родитель 30c3ea5c3f
Коммит f8c5fa37d9
1 изменённых файлов: 9 добавлений и 6 удалений

Просмотреть файл

@ -67,8 +67,10 @@ static void calculateShadowViewMutations(
return; return;
} }
better::map<Tag, ShadowViewNodePair> insertedPairs; auto index = int{0};
int index = 0;
// Maps inserted node tags to pointers to them in `newChildPairs`.
auto insertedPairs = better::map<Tag, ShadowViewNodePair const *>{};
ShadowViewMutationList createMutations = {}; ShadowViewMutationList createMutations = {};
ShadowViewMutationList deleteMutations = {}; ShadowViewMutationList deleteMutations = {};
@ -118,20 +120,20 @@ static void calculateShadowViewMutations(
insertMutations.push_back(ShadowViewMutation::InsertMutation( insertMutations.push_back(ShadowViewMutation::InsertMutation(
parentShadowView, newChildPair.shadowView, index)); parentShadowView, newChildPair.shadowView, index));
insertedPairs.insert({newChildPair.shadowView.tag, newChildPair}); insertedPairs.insert({newChildPair.shadowView.tag, &newChildPair});
} }
// Stage 3: Collecting `Delete` and `Remove` mutations // Stage 3: Collecting `Delete` and `Remove` mutations
for (index = lastIndexAfterFirstStage; index < oldChildPairs.size(); for (index = lastIndexAfterFirstStage; index < oldChildPairs.size();
index++) { index++) {
const auto &oldChildPair = oldChildPairs[index]; auto const &oldChildPair = oldChildPairs[index];
// Even if the old view was (re)inserted, we have to generate `remove` // Even if the old view was (re)inserted, we have to generate `remove`
// mutation. // mutation.
removeMutations.push_back(ShadowViewMutation::RemoveMutation( removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView, oldChildPair.shadowView, index)); parentShadowView, oldChildPair.shadowView, index));
const auto &it = insertedPairs.find(oldChildPair.shadowView.tag); auto const it = insertedPairs.find(oldChildPair.shadowView.tag);
if (it == insertedPairs.end()) { if (it == insertedPairs.end()) {
// The old view was *not* (re)inserted. // The old view was *not* (re)inserted.
@ -151,7 +153,8 @@ static void calculateShadowViewMutations(
// The old view *was* (re)inserted. // The old view *was* (re)inserted.
// We have to call the algorithm recursively if the inserted view // We have to call the algorithm recursively if the inserted view
// is *not* the same as removed one. // is *not* the same as removed one.
const auto &newChildPair = it->second; auto const &newChildPair = *it->second;
if (newChildPair != oldChildPair) { if (newChildPair != oldChildPair) {
const auto oldGrandChildPairs = const auto oldGrandChildPairs =
sliceChildShadowNodeViewPairs(*oldChildPair.shadowNode); sliceChildShadowNodeViewPairs(*oldChildPair.shadowNode);