Files
yoga/yoga/algorithm/FlexLine.h
Nick Gerleman 8f69ac776e Minor display: contents optimizations (#1736)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1736

X-link: https://github.com/facebook/react-native/pull/47358

`LayoutableChildren<yoga::Node>::Iterator` showed up to a surprising extent on a recent trace. Part of this was during pixel grid rounding, which does full tree traversal (we should fix that...), where the iterator is the first thing to read from the node.

I ran Yoga microbenchmark with Yoga compiled with `-O2`, where we saw a regression of synthetic performance by ~10%, but it turns out this build also had ASAN and some other heavy bits enabled, so the real impact was quite lower (~6%).

I was able to make some optimizations in the meantime against that, which still show some minor wins, reducing that overhead to ~4% in the properly optimized build (and a bit more before that). This is still measurable on the beefy server, and the code is a bit cleaner, so let's commit these!

Note that, in real scenarios, measure functions may dominate layout time, so display: contents does not mean end-to-end 4% regression, even after this change.

This change makes a few different optimizations
1. Removes redundant copies
2. Removes redundant index keeping
3. Mark which branches are likely vs unlikely
4. Shrink iterator size from 6 pointers to 3 pointers
5. Avoid usage in pixel grid rounding (so we don't need to have cache read for style)

In "Huge nested layout" example

| Before display: contents support | After display: contents support | After optimizations |
| 9.77ms | 10.39ms | 10.17ms |

Changelog: [Internal]

Reviewed By: rozele

Differential Revision: D65336148

fbshipit-source-id: 01c592771ed7accf2d87dddd5a3a9e0225098b56
2024-11-05 11:28:37 -08:00

76 lines
2.6 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and 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 <vector>
#include <yoga/Yoga.h>
#include <yoga/node/Node.h>
namespace facebook::yoga {
struct FlexLineRunningLayout {
// Total flex grow factors of flex items which are to be laid in the current
// line. This is decremented as free space is distributed.
float totalFlexGrowFactors{0.0f};
// Total flex shrink factors of flex items which are to be laid in the current
// line. This is decremented as free space is distributed.
float totalFlexShrinkScaledFactors{0.0f};
// The amount of available space within inner dimensions of the line which may
// still be distributed.
float remainingFreeSpace{0.0f};
// The size of the mainDim for the row after considering size, padding, margin
// and border of flex items. This is used to calculate maxLineDim after going
// through all the rows to decide on the main axis size of owner.
float mainDim{0.0f};
// The size of the crossDim for the row after considering size, padding,
// margin and border of flex items. Used for calculating containers crossSize.
float crossDim{0.0f};
};
struct FlexLine {
// List of children which are part of the line flow. This means they are not
// positioned absolutely, or with `display: "none"`, and do not overflow the
// available dimensions.
const std::vector<yoga::Node*> itemsInFlow{};
// Accumulation of the dimensions and margin of all the children on the
// current line. This will be used in order to either set the dimensions of
// the node if none already exist or to compute the remaining space left for
// the flexible children.
const float sizeConsumed{0.0f};
// Number of edges along the line flow with an auto margin.
const size_t numberOfAutoMargins{0};
// Layout information about the line computed in steps after line-breaking
FlexLineRunningLayout layout{};
};
// Calculates where a line starting at a given index should break, returning
// information about the collective children on the liune.
//
// This function assumes that all the children of node have their
// computedFlexBasis properly computed(To do this use
// computeFlexBasisForChildren function).
FlexLine calculateFlexLine(
yoga::Node* node,
Direction ownerDirection,
float ownerWidth,
float mainAxisownerSize,
float availableInnerWidth,
float availableInnerMainDim,
Node::LayoutableChildren::Iterator& iterator,
size_t lineCount);
} // namespace facebook::yoga