Skip to content

Treat a min-content available height as max-content in compute_cached_layout - #1404

Open
YeungKC wants to merge 1 commit into
DioxusLabs:mainfrom
YeungKC:perf/min-content-block-size
Open

YeungKC wants to merge 1 commit into
DioxusLabs:mainfrom
YeungKC:perf/min-content-block-size

Conversation

@YeungKC

@YeungKC YeungKC commented Nov 9, 2026

Copy link
Copy Markdown

Objective

Fixes #1403

Lay out a child once instead of twice when the only difference between two queries for it is whether its available height is MinContent or MaxContent.

The min-content block size of a box is its max-content block size (css-sizing-3 §5.1), so the two queries always give the same result. The layout cache keys them separately. In nested flex and grid trees, many children get both queries, and each runs the full layout of the child's subtree.

Context

#1284 applies the same rule in grid track sizing, for the expansion of flexible tracks. This PR applies it once, at the entry point of every cached layout:

 compute_cached_layout(tree, node, inputs, compute_uncached)
+  if inputs.available_space.height == MinContent
+    inputs.available_space.height = MaxContent
   if cache hit
     return cached
   compute_uncached(...)
   store in cache

The width axis does not change. There, min-content and max-content are different constraints.

Cache misses for one fresh layout of a 2047-node flex tree (alternating row and column, auto sizes), grouped by input shape. Each pair differs only in the available height:

1358  ComputeSize Vertical    known=(w, -)  available=(definite, min-content)
1358  ComputeSize Vertical    known=(w, -)  available=(definite, max-content)
 680  ComputeSize Vertical    known=(-, -)  available=(max-content, min-content)
 680  ComputeSize Vertical    known=(-, -)  available=(max-content, max-content)
 ...

Tests

  • cargo test --all-features passes. All generated Chrome fixtures pass with no changed expectation.
  • Four tests in tests/hand_written/caching.rs assert measure-function call counts. The counts go down:
Test Before After
measure_count_nested_grid 6 5
measure_count_grid_item_with_known_width 3 2
measure_count_grid_item_in_fixed_track 3 2
measure_count_flex_in_grid (row / column) 9 / 10 6 / 7

Performance

Criterion, cargo bench -- --baseline main, on Apple Silicon. An A/A run of main against the same baseline drifts by −1% to +4%.

Benchmark main PR Change
relayout/deep/flex/8191 76.1 µs 17.8 µs −77%
relayout/balanced/flex/11111 305 µs 81.3 µs −73%
mixed_flex_grid/mixed/depth_4_width_4 4.29 ms 3.26 ms −24%
mixed_flex_grid/mixed/depth_4_width_8 66.7 ms 52.3 ms −22%
PanGUI/fit_nesting 18.2 ms 14.6 ms −20%
mixed_flex_grid/mixed/depth_2_width_4 307 µs 249 µs −19%
relayout/deep/mixed/8191 29.6 µs 25.9 µs −12%
grid/deep/2x2/1024 832 µs 734 µs −12%
grid/deep/3x3/6561 3.85 ms 3.43 ms −11%
grid/superdeep/1x1/1000 1.25 ms 1.14 ms −9%
relayout/wide/grid/10101 95.9 µs 88.1 µs −8%

Most other benchmarks stay within the A/A drift. The flexbox benchmarks (fresh layout of trees with mostly definite sizes) are about 1% slower than in the A/A run. Three small PanGUI flex benchmarks are +5% to +6%, about 2% above the A/A range: padding_and_margin, wide_no_wrap_simple_few and wide_wrapping. These trees have no duplicate queries to remove, so they only pay for the extra comparison.

Feedback wanted

Measure functions and custom layout algorithms called through compute_cached_layout no longer receive AvailableSpace::MinContent in the height axis; they receive MaxContent. The built-in algorithms give the same result for both, and the Chrome fixtures confirm this. A measure function that returns a different height for the two constraints would change behavior. I documented this in the CHANGELOG.md entry.

An alternative is to stop producing a min-content height at each source, like Blink does: GridLayoutAlgorithm returns BlockContributionSize() for both the min-content and the max-content contribution in the block axis. In Taffy, a min-content height comes from several places: GridItem::min_content_contribution, the flex basis and automatic minimum size measurements, block.rs, the min-content sizing keyword, containers that pass their own available height on to their children, and the caller of compute_layout. A fix at each source would need to cover all of them, and custom algorithms would still create the duplicates. That is why I chose the entry point. Is this acceptable, or would you prefer the fix at the sources?

…d_layout`

The min-content block size of a box is its max-content block size
(css-sizing-3 §5.1), so layouts with either available height give the
same result. Map the height constraint once at the entry point of every
cached layout, so that both queries share one cache entry and the child
is laid out once.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Layout cache treats a min-content and a max-content available height as different inputs

1 participant