Skip to content

Represent aspect-ratio: auto && <ratio> and apply it to the content box - #1347

Open
nicoburns wants to merge 4 commits into
mainfrom
devin/1792599215-aspect-ratio-auto
Open

nicoburns wants to merge 4 commits into
mainfrom
devin/1792599215-aspect-ratio-auto

Conversation

@nicoburns

@nicoburns nicoburns commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Objective

Support aspect-ratio: auto && <ratio>. For a non-replaced box, the ratio then applies to the content box regardless of box-sizing (https://drafts.csswg.org/css-sizing-4/#aspect-ratio). Previously embedders could only pass an Option<f32>, so auto was lost.

This is the bottom of the stack, based on main. #1346 (aspect-ratio automatic minimum and min/max transfer) builds on it.

Changes

  • New public AspectRatio style type, a 4-byte newtype over f32. The ratio is negated for auto && <ratio>, and AspectRatio::NONE is NaN. Constructors: new, auto_and. Accessors: ratio() -> Option<f32>, is_auto(). Degenerate ratios (not finite and positive) become NONE. PartialEq compares bits, so NONE == NONE. Serde uses a { ratio, auto } repr. From<f32> and From<Option<f32>> are provided.
  • With the parse feature, AspectRatio: FromStr parses auto || <ratio>, where <ratio> is <number [0,∞]> [ / <number [0,∞]> ]?. 0, 1 / 0 and 0 / 0 parse to NONE; negative numbers are rejected. The XML test loader uses it.
  • Breaking: Style::aspect_ratio and CoreStyle::aspect_ratio() now use AspectRatio. Style shrinks by 8 bytes (552/520 bytes).
  • Compute: a new crate-internal compute/common/aspect_ratio.rs has ResolvedAspectRatio, which holds the ratio plus an inset. The inset is the padding+border for auto on a border-box box, and zero otherwise. Every ratio transfer goes through ApplyAspectRatio::apply_aspect_ratio / width_from_height / height_from_width instead of Size::maybe_apply_aspect_ratio, so behaviour without auto is unchanged.
  • ResolvedAspectRatio::new checks for NONE inline and builds the ratio out of line (new_unchecked), and flex items compute padding+border only for that call, so boxes without a ratio do less work (commit 42bc3dc9).
  • gentest supports auto <ratio>. There are 6 new fixtures: block width/height/content-box, abspos, flex and grid.

Context

Performance vs main. Ir is layout-only callgrind (--toggle-collect=*Bencher*iter*). Wall time is the min of per-round Criterion medians over 7 interleaved rounds, pinned to one core on an otherwise idle VM; treat about ±2% wall as noise (grid wide 316² switches between two speeds on this VM in every version, so its wall figure is unreliable). Measured on the pre-rebase base (Taffy 4e59968e); later rebases (now onto c2560562) don't change this PR's diff.

bench main Ir this PR Ir Ir vs main main wall this PR wall wall vs main
flex Deep auto 12-level 58.86M 59.30M +0.74% 3.884ms 3.959ms +1.93%
flex Deep auto 14-level 146.46M 147.43M +0.66% 9.961ms 10.109ms +1.48%
flex Deep random 12-level 22.98M 23.10M +0.53% 1.861ms 1.870ms +0.50%
flex Wide tree 10k 67.82M 68.04M +0.32% 5.841ms 5.908ms +1.15%
flex Abspos leaves 10k 44.44M 44.54M +0.23% 4.248ms 4.257ms +0.20%
flex super deep 100 5.06M 5.08M +0.55% 0.284ms 0.293ms +3.22%
grid wide 316² 642.96M 643.33M +0.06% 49.745ms 50.843ms +2.21%
PanGUI pixels_with_min_expand 211.95M 213.04M +0.51% 15.501ms 15.400ms −0.65%
PanGUI flex_expand_weights 77.29M 77.72M +0.55% 5.758ms 5.743ms −0.26%
PanGUI percentage_and_ratio (has ratios) 34.92M 35.51M +1.69% 2.575ms 2.559ms −0.61%

Boxes without a ratio check for AspectRatio::NONE inline; building a ResolvedAspectRatio is out of line and #[cold]. The remaining cost has no single hotspot.

Feedback wanted

  • Taffy can't know whether a measured replaced leaf has a natural ratio, so auto there still uses the style ratio. Blitz handles replaced elements itself.

Link to Devin session: https://dioxus.staging.devinenterprise.com/sessions/0236734c2d3c402e860b874de0a2b8cb
Open in Devin Desktop: https://dioxus.staging.devinenterprise.com/desktop/session/0236734c2d3c402e860b874de0a2b8cb?variant=devin-insiders
Requested by: @nicoburns

@staging-devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

@staging-devin-ai-integration
staging-devin-ai-integration Bot force-pushed the devin/1792599215-aspect-ratio-auto branch from 76aea57 to d880567 Compare October 26, 2026 21:43

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.

1 participant