Fabric: Optimizing yogaNodeCloneCallbackConnector in case of correct childIndex

Summary:
YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector is a hot path.
Previous implementation did not use provided `childIndex` which is correct for most cases.
See comments in code for more details.

Reviewed By: fkgozali

Differential Revision: D8070120

fbshipit-source-id: d1a6abe82688387752b66a57b13dc356abb22c96
This commit is contained in:
Valentin Shergin 2018-05-24 17:53:26 -07:00 committed by Facebook Github Bot
parent d5cfda51c4
commit b13add753b
1 changed files with 21 additions and 4 deletions

View File

@ -143,11 +143,26 @@ YGNode *YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector(YGNode *oldYoga
// ... but we have to address this by `shared_ptr`. We cannot create a new `shared_ptr` for it because we will end up with two shared pointers to
// single object which will cause preluminary destroyng the object.
// Another approaches to consider:
// * Create a new `shared_ptr` with empty deleter.
// * Using `childIndex` to find exact node.
auto &&layoutableChildNodes = parentShadowNodeRawPtr->getLayoutableChildNodes();
SharedLayoutableShadowNode oldShadowNode = nullptr;
for (auto child : parentShadowNodeRawPtr->getLayoutableChildNodes()) {
// We cannot rely on `childIndex` all the time because `childNodes` can
// contain non-layoutable shadow nodes, however chances are good that
// `childIndex` points to the right shadow node.
// Optimistic attempt (in case if `childIndex` is valid):
if (childIndex < layoutableChildNodes.size()) {
oldShadowNode = layoutableChildNodes[childIndex];
if (oldShadowNode.get() == oldShadowNodeRawPtr) {
goto found;
} else {
oldShadowNode = nullptr;
}
}
// General solution:
for (auto child : layoutableChildNodes) {
if (child.get() == oldShadowNodeRawPtr) {
oldShadowNode = child;
break;
@ -156,6 +171,8 @@ YGNode *YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector(YGNode *oldYoga
assert(oldShadowNode);
found:
// The new one does not exist yet. So, we have to clone and replace this using `cloneAndReplaceChild`.
SharedYogaLayoutableShadowNode newShadowNode =
std::dynamic_pointer_cast<const YogaLayoutableShadowNode>(parentShadowNodeRawPtr->cloneAndReplaceChild(oldShadowNode));