Files
react-native/ReactCommon/fabric/components/root/RootShadowNode.cpp
T
Valentin Shergin ea8a57116f Fabric: A new way to compute nodes for onLayout event
Summary:
Previously we computed the list of nodes that need to be notified about layout changes using a list of mutation instructions. That was fine, but that's not really compatible with some other changes that I plan to make, so I decided to change it (make it better).

Besides the better design (debatable; fewer dependencies to unrelated moving pieces), here is why I believe the new way is more performant:

* The new approach has no `dynamic_casts`, whereas the previous has tons of them (two per a mutation). If a `dynamic_cast` takes 10 ns, for 500 nodes it can take up to 5ms only for casts. (Non-scientific assumption.)
* After removing dependency to mutation instruction, we can enable flattening for views which have `onLayout` event.

Reviewed By: mdvacca

Differential Revision: D15110725

fbshipit-source-id: 31a657ccfd02441734ad1d71a833653223163289
2019-05-01 16:27:55 -07:00

85 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.
*/
#include "RootShadowNode.h"
#include <react/components/view/conversions.h>
#include <react/debug/SystraceSection.h>
namespace facebook {
namespace react {
const char RootComponentName[] = "RootView";
void RootShadowNode::layout(
std::vector<LayoutableShadowNode const *> *affectedNodes) {
SystraceSection s("RootShadowNode::layout");
ensureUnsealed();
auto layoutContext = getProps()->layoutContext;
layoutContext.affectedNodes = affectedNodes;
layout(layoutContext);
// This is the rare place where shadow node must layout (set `layoutMetrics`)
// itself because there is no a parent node which usually should do it.
if (getHasNewLayout()) {
setLayoutMetrics(layoutMetricsFromYogaNode(yogaNode_));
setHasNewLayout(false);
}
}
UnsharedRootShadowNode RootShadowNode::clone(
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext) const {
auto props = std::make_shared<const RootProps>(
*getProps(), layoutConstraints, layoutContext);
auto newRootShadowNode = std::make_shared<RootShadowNode>(
*this,
ShadowNodeFragment{
/* .tag = */ ShadowNodeFragment::tagPlaceholder(),
/* .surfaceId = */ ShadowNodeFragment::surfaceIdPlaceholder(),
/* .props = */ props,
});
return newRootShadowNode;
}
UnsharedRootShadowNode RootShadowNode::clone(
SharedShadowNode const &oldShadowNode,
SharedShadowNode const &newShadowNode) const {
auto ancestors = oldShadowNode->getAncestors(*this);
if (ancestors.size() == 0) {
return UnsharedRootShadowNode{nullptr};
}
auto childNode = newShadowNode;
for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) {
auto &parentNode = it->first.get();
auto childIndex = it->second;
auto children = parentNode.getChildren();
assert(ShadowNode::sameFamily(*children.at(childIndex), *childNode));
children[childIndex] = childNode;
childNode = parentNode.clone({
ShadowNodeFragment::tagPlaceholder(),
ShadowNodeFragment::surfaceIdPlaceholder(),
ShadowNodeFragment::propsPlaceholder(),
ShadowNodeFragment::eventEmitterPlaceholder(),
std::make_shared<SharedShadowNodeList>(children),
});
}
return std::const_pointer_cast<RootShadowNode>(
std::static_pointer_cast<RootShadowNode const>(childNode));
}
} // namespace react
} // namespace facebook