Repository navigation
Lay out a baseline-aligned item before a measured row reads its baseline (#2153) - #60587
Merged
Merged
Conversation
…ine (react#2153) Summary: Why === A row with baseline-aligned items takes each line's cross size from the items' baselines ([css-flexbox-1 §9.4, step 8](https://www.w3.org/TR/css-flexbox-1/#algo-cross-line)). `calculateBaseline` reads an item's baseline from the positions of its descendants, and only a layout pass sets positions. When the row is measured and not laid out, as it is for its flex basis in a column, its items are only measured too: an item with a definite size is sized by `measureNodeWithFixedSize` without a visit to its descendants, and any other item is measured without positions. The row's height then comes from whatever positions the descendants last had, which on a first layout is none: ```html <div style="width: 400px"> <div style="flex-direction: row; align-items: baseline"> <div style="width: 50px; height: 50px"> <div style="height: 10px"></div> </div> <div style="width: 50px; height: 50px"></div> </div> <div style="height: 10px"></div> </div> ``` The first item's baseline is 10 below its top and the second's is 50, so the line is 90 high in Chrome and the last box is at 90. Yoga measures the row as 50 high, lays it out at that height, and puts the last box at 50, over the first item. How === When a node that is being measured sizes a baseline-aligned item in the second free-space pass, it now lays the item out instead of measuring it, so that the baseline it reads next is the item's own. It does so only for an item whose baseline comes from descendants (one with children and no baseline function). It also does so only when the node is dirty. A layout pass writes the positions and sizes that the embedder reads, and sets `hasNewLayout`, on the item's subtree. That is safe when the node's own layout pass follows in the same `YGNodeCalculateLayout`, where it lays the item out at its final size and visits everything the measurement touched. A dirty node always gets that pass, because every node above a dirty node in the tree being laid out is dirty too, so none of them can answer from its layout cache: `markDirtyAndPropagate` dirties a node's owners within one tree, and React Native marks a parent dirty whenever one of its children is dirty when it clones the parent (`updateYogaChildren`). A clean node that is measured again may not get one. Widening a parent, for example, measures a content-sized child again and then finds the child's cached layout still valid; a layout written during that measurement would stay, with `hasNewLayout` set under a node that has none and a padding resolved against the wrong width. A clean node's items therefore keep the layout they have, and the baselines read from it are those of the last layout, as before this change. No erratum gates this. The size of a measured baseline row already depended on what its items' descendants held from an earlier layout, so a first layout and a later one of the same tree could differ, and no application can rely on the first. Cost, counted in passes with the `LayoutPassEnd` event for a first layout of baseline rows nested eight deep in a 400-wide column, each row's first item a column that holds the next row: | Tree | Layouts and measures, before | After | | --- | --- | --- | | A 10 by 10 leaf in every item and a 10 by 10 sibling in every row | 33 and 1880 | 41 and 2207 | | A leaf in every item, no sibling | 25 and 602 | 33 and 684 | | A leaf in the innermost item only, no sibling | 18 and 108 | 26 and 117 | Where the result does not change, as in these three, a first layout costs about a quarter to two fifths more layouts and up to 17% more measures. These are pass counts; no time was measured. Limits, all of which read the baselines of an earlier layout, as `main` does everywhere: - A clean row measured at a size its items were not last laid out at. A resize does this: a row whose item has a percentage padding above its baseline is 70 high in a fresh layout at one width, and keeps the height of the earlier width after a resize to it. - A baseline row inside a baseline-aligned item, on a first layout. The outer row's measurement lays the item out, which clears the dirty flags below it, so the inner row is clean when it is measured again at another size. - On `main` the line's size also counts the baselines of items that are not baseline-aligned, which this change does not lay out; sizing a line from its baseline-aligned items alone is a separate change. Removing the first two needs a baseline that does not come from a layout: one recorded with each cached measurement, for the constraints it was measured at. That is a larger change, to the cache. Test Plan ========= `yarn gentest -f YGAlignItemsTest` adds two fixtures with Chrome's values, and `cmake --build tests/build && tests/build/yogatests` passes. With `yoga/algorithm/CalculateLayout.cpp` reverted, both fail: in `align_baseline_row_of_fixed_size_items_in_column` (the markup above) the row is 50 high where Chrome has 90, and in `align_baseline_row_of_content_sized_items_in_column`, whose first item has no height of its own, the row is 50 high where Chrome has 60. The hand-written `measuring_clean_baseline_row_keeps_layout_of_its_items` in `YGAlignBaselineTest.cpp` covers the check that the node is dirty: it widens a parent so that a clean row is measured again while its owner's cached layout holds. Without the check, the row's item is left with `hasNewLayout` set and its child 30 lower than the layout the tree keeps. The two checks on the item (children, no baseline function) only avoid work. `yarn format-check`, `yarn lint`, `yarn tsc` and `yarn gentest-validate` pass. Not run: the generated Java and JavaScript tests. Changelog: [General][Fixed] - Fix Yoga making a row with baseline-aligned items too short, so that it overlaps what follows it, when the row's height comes from its content Reviewed By: javache Differential Revision: D135550892
|
@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D135550892. |
Collaborator
|
This pull request was successfully merged by @javache in eb37db0 When will my fix make it into a release? | How to file a pick request? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Why
A row with baseline-aligned items takes each line's cross size from the items' baselines (css-flexbox-1 §9.4, step 8).
calculateBaselinereads an item's baseline from the positions of its descendants, and only a layout pass sets positions. When the row is measured and not laid out, as it is for its flex basis in a column, its items are only measured too: an item with a definite size is sized bymeasureNodeWithFixedSizewithout a visit to its descendants, and any other item is measured without positions. The row's height then comes from whatever positions the descendants last had, which on a first layout is none:The first item's baseline is 10 below its top and the second's is 50, so the line is 90 high in Chrome and the last box is at 90. Yoga measures the row as 50 high, lays it out at that height, and puts the last box at 50, over the first item.
How
When a node that is being measured sizes a baseline-aligned item in the second free-space pass, it now lays the item out instead of measuring it, so that the baseline it reads next is the item's own. It does so only for an item whose baseline comes from descendants (one with children and no baseline function).
It also does so only when the node is dirty. A layout pass writes the positions and sizes that the embedder reads, and sets
hasNewLayout, on the item's subtree. That is safe when the node's own layout pass follows in the sameYGNodeCalculateLayout, where it lays the item out at its final size and visits everything the measurement touched. A dirty node always gets that pass, because every node above a dirty node in the tree being laid out is dirty too, so none of them can answer from its layout cache:markDirtyAndPropagatedirties a node's owners within one tree, and React Native marks a parent dirty whenever one of its children is dirty when it clones the parent (updateYogaChildren). A clean node that is measured again may not get one. Widening a parent, for example, measures a content-sized child again and then finds the child's cached layout still valid; a layout written during that measurement would stay, withhasNewLayoutset under a node that has none and a padding resolved against the wrong width. A clean node's items therefore keep the layout they have, and the baselines read from it are those of the last layout, as before this change.No erratum gates this. The size of a measured baseline row already depended on what its items' descendants held from an earlier layout, so a first layout and a later one of the same tree could differ, and no application can rely on the first.
Cost, counted in passes with the
LayoutPassEndevent for a first layout of baseline rows nested eight deep in a 400-wide column, each row's first item a column that holds the next row:Where the result does not change, as in these three, a first layout costs about a quarter to two fifths more layouts and up to 17% more measures. These are pass counts; no time was measured.
Limits, all of which read the baselines of an earlier layout, as
maindoes everywhere:mainthe line's size also counts the baselines of items that are not baseline-aligned, which this change does not lay out; sizing a line from its baseline-aligned items alone is a separate change.Removing the first two needs a baseline that does not come from a layout: one recorded with each cached measurement, for the constraints it was measured at. That is a larger change, to the cache.
Test Plan
yarn gentest -f YGAlignItemsTestadds two fixtures with Chrome's values, andcmake --build tests/build && tests/build/yogatestspasses. Withyoga/algorithm/CalculateLayout.cppreverted, both fail: inalign_baseline_row_of_fixed_size_items_in_column(the markup above) the row is 50 high where Chrome has 90, and inalign_baseline_row_of_content_sized_items_in_column, whose first item has no height of its own, the row is 50 high where Chrome has 60.The hand-written
measuring_clean_baseline_row_keeps_layout_of_its_itemsinYGAlignBaselineTest.cppcovers the check that the node is dirty: it widens a parent so that a clean row is measured again while its owner's cached layout holds. Without the check, the row's item is left withhasNewLayoutset and its child 30 lower than the layout the tree keeps. The two checks on the item (children, no baseline function) only avoid work.yarn format-check,yarn lint,yarn tscandyarn gentest-validatepass. Not run: the generated Java and JavaScript tests.Changelog:
[General][Fixed] - Fix Yoga making a row with baseline-aligned items too short, so that it overlaps what follows it, when the row's height comes from its content
Reviewed By: javache
Differential Revision: D135550892