mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
92091b8b31
Summary:
# What is this?
For a very long time, we've discussed the possibility of detecting Node Reparenting in the Fabric Differ. Practically, from the developer perspective, ReactJS and React Native do not allow reparenting: nodes cannot be reparented, only deleted and then recreated with entirely new tags.
However, Fabric introduced the idea of View Flattening where views deemed unnecessary would be removed from the View hierarchy entirely. This is great and improves memory usage, except for one issue: if a View becomes unflattened, or becomes flattened, the entire tree underneath it must be rebuilt.
In a past diff we introduced a mechanism to detect sibling reordering cleverly, and produce a minimal instruction set. This diff is very similar: we know the invariants around flattening and unflattening of views and we take advantage of them to produce an optimal set of instructions efficiently.
# What's different from previous attempts?
No global maps! Those are slow!
This seems to work and (hopefully) might even improve performance, since way less work is being done on the UI thread in cases when views are (un)flattened.
This *only* does extra work when flattening/unflattening happens, which gives product engineers a little more control over perf.
# So, how's it work?
This algorithm is intuitively simple (I think) but tricky to pull off, because there are lots of edge-cases.
In short: In the past, that information was hidden from the Differ: the differ didn't know if views were being reparented, it would see them
as entirely new views or as views being deleted if a View was flattened or unflattened. We very subtly change the information given to the differ:
all nodes are visible to the differ, but marked as Flattened or Unflattened. Thus, when the differ compares two nodes in the "old" and "new" tree,
it can tell not just if there are updates to the node but if it has been unflattened or flattened as well.
For example, take this tree, where * indicates that a View is flattened:
```
A
+
+----+---+
B* X
+ +
| |
+---+--+ +
E F Y
```
When the Differ asks for the children of A, in the past it would get a list `[E, F, X]`. That is, B* and X are both its children, but since B is flattened, it is omitted entirely from the list and
its children are substituted.
Now, when the Differ asks for the children of A, we give it this list instead: `[B*, E, F, X]`. That is: we give it a list which includes B, but B is marked as flattened.
Another wrinkle: A node `X` could have its children flattened, but still be a concrete view: so flattening/unflattening is a different operation from making a view "concrete" or "unconcrete", which can change independently of flattening.
There is one additional wrinkle: because of zIndex/stacking order, the children of `B` might not actually appear after `B` in the list. Depending on zIndex, a tree that looks like this:
```
A
+
+------+------+
B* C*
+ +
| |
+--+--+ +--+--+
D E F G
```
Could actually be linearized as: `[D G B* F C* E]` (as an extreme example; but basically all permutations as possible).
This is the reason, and the *only* reason that the inner Flattener/Unflattener
## The cases we need to handle
There are 7 cases/edge-cases of flattening and unflattening that we need to handle. Practically, all cases of reordering + flattening/unflattening, and taking recursive cases into account:
1. View A and A' (A in the old tree, A' in the new tree) are matched in the differ, and A* has been flattened or unflattened. These two cases are the easiest to handle.
2. View A' has been reordered with its siblings, and has been flattened or unflattened. These cases are slightly trickier to handle.
3. While flattening or unflattening, we encounter a child that has also been unflattened or flattened. So we need to handle four cases here in total: Flatten-Flatten, Flatten-Unflatten, Unflatten-Flatten, and Unflatten-Unflatten.
Other things to think about, also covered above:
1. Ordering. Views can be reordered and flattened/unflattened at the same time.
2. zIndex ordering: children in a certain order from the ShadowNode perspective may be stacked differently from a View perspective. We use the zIndex ordering for everything in the differ, and this prevents us from performing certain optimizations (see above: we cannot assume that children come after their parent in a list; they may come before, may be interwoven with children from other parents, etc).
# Perf Implications?
Practically, there should be very little negative overhead. There is some overhead in actually performing a flattening/unflattening operation, but... not much more than before. We don't use global maps, so the cost of flattening/unflattening is basically `O(number of nodes reparented)` - note that that's direct nodes reparented, *not* descendants.
tl;dr the perf hit should be similar to reordering, which is non-zero, but close to zero, and zero-cost for any diff operations on parts of the tree that don't involve flattening/unflattening. AFAICT this is very close to an ideal solution for that reason (but I wish it was simpler overall).
# In Summary?
I hope this works out and I think it could improve a number of things downstream: perf, LayoutAnimations, Bindings, certain crashes because of platform assumptions about mutations, etc.
Is it worth it? This new implementation is substantially harder to reason about, harder to read, and harder to understand. This is an important consideration. All I can say there is that I trust the test suite I've been using, but
the decreased readability is a big negative. Hopefully we can improve this in the future.
The rest is fiddly implementation details that I sincerely hope can be improved and simplified in the future.
# Followups?
The part that makes this algorithm the most expensive is that because of zIndex ordering, we cannot assume that children are linearized after their parents and so we rely more heavily on maps for the flattening/unflattening. Our TinyMap implementation should make these `find` operations fast enough unless trees' children are constantly being reordered, but it's still worth thinking of ways to make this even faster.
Changelog: [Internal]
Reviewed By: shergin, mdvacca
Differential Revision: D23259341
fbshipit-source-id: 35d9b90caf262d601a31996ea2cb37e329c61ffc
103 lines
2.5 KiB
C++
103 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <better/small_vector.h>
|
|
#include <folly/Hash.h>
|
|
#include <react/renderer/core/EventEmitter.h>
|
|
#include <react/renderer/core/LayoutMetrics.h>
|
|
#include <react/renderer/core/Props.h>
|
|
#include <react/renderer/core/ReactPrimitives.h>
|
|
#include <react/renderer/core/ShadowNode.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* Describes a view that can be mounted.
|
|
* This is exposed to the mounting layer.
|
|
*/
|
|
struct ShadowView final {
|
|
ShadowView() = default;
|
|
ShadowView(ShadowView const &shadowView) = default;
|
|
ShadowView(ShadowView &&shadowView) noexcept = default;
|
|
|
|
/*
|
|
* Constructs a `ShadowView` from given `ShadowNode`.
|
|
*/
|
|
explicit ShadowView(ShadowNode const &shadowNode);
|
|
|
|
ShadowView &operator=(ShadowView const &other) = default;
|
|
ShadowView &operator=(ShadowView &&other) = default;
|
|
|
|
bool operator==(ShadowView const &rhs) const;
|
|
bool operator!=(ShadowView const &rhs) const;
|
|
|
|
ComponentName componentName{};
|
|
ComponentHandle componentHandle{};
|
|
Tag tag{};
|
|
Props::Shared props{};
|
|
EventEmitter::Shared eventEmitter{};
|
|
LayoutMetrics layoutMetrics{EmptyLayoutMetrics};
|
|
State::Shared state{};
|
|
};
|
|
|
|
#if RN_DEBUG_STRING_CONVERTIBLE
|
|
|
|
std::string getDebugName(ShadowView const &object);
|
|
std::vector<DebugStringConvertibleObject> getDebugProps(
|
|
ShadowView const &object,
|
|
DebugStringConvertibleOptions options);
|
|
|
|
#endif
|
|
|
|
/*
|
|
* Describes pair of a `ShadowView` and a `ShadowNode`.
|
|
* This is not exposed to the mounting layer.
|
|
*
|
|
*/
|
|
struct ShadowViewNodePair final {
|
|
using List = better::
|
|
small_vector<ShadowViewNodePair, kShadowNodeChildrenSmallVectorSize>;
|
|
|
|
ShadowView shadowView;
|
|
ShadowNode const *shadowNode;
|
|
bool flattened{false};
|
|
bool isConcreteView{true};
|
|
|
|
int mountIndex{0};
|
|
|
|
bool inOtherTree{false};
|
|
|
|
/*
|
|
* The stored pointer to `ShadowNode` represents an identity of the pair.
|
|
*/
|
|
bool operator==(const ShadowViewNodePair &rhs) const;
|
|
bool operator!=(const ShadowViewNodePair &rhs) const;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
struct hash<facebook::react::ShadowView> {
|
|
size_t operator()(const facebook::react::ShadowView &shadowView) const {
|
|
return folly::hash::hash_combine(
|
|
0,
|
|
shadowView.componentHandle,
|
|
shadowView.tag,
|
|
shadowView.props,
|
|
shadowView.eventEmitter,
|
|
shadowView.state);
|
|
}
|
|
};
|
|
|
|
} // namespace std
|