layout: Box block_margins_collapsed_with_children member of BoxFragment (#36474)

This reduces the size of `BoxFragment` by around 20 bytes.

Testing: This just changes the layout of a data structure, so is
covered by existing WPT tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2025-04-12 10:52:30 +02:00 committed by GitHub
parent 3c4ec17180
commit a4a308e434
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 29 additions and 42 deletions

View file

@ -256,8 +256,6 @@ impl FlexLineItem<'_> {
size: item_used_size, size: item_used_size,
}, },
); );
let margin = flex_context.sides_to_flow_relative(item_margin);
let collapsed_margin = CollapsedBlockMargins::from_margin(&margin);
if let Some(item_baseline) = self.layout_result.baseline_relative_to_margin_box.as_ref() { if let Some(item_baseline) = self.layout_result.baseline_relative_to_margin_box.as_ref() {
let item_baseline = *item_baseline + item_content_cross_start_position - let item_baseline = *item_baseline + item_content_cross_start_position -
@ -294,9 +292,10 @@ impl FlexLineItem<'_> {
flex_context flex_context
.sides_to_flow_relative(self.item.border) .sides_to_flow_relative(self.item.border)
.to_physical(container_writing_mode), .to_physical(container_writing_mode),
margin.to_physical(container_writing_mode), flex_context
.sides_to_flow_relative(item_margin)
.to_physical(container_writing_mode),
None, /* clearance */ None, /* clearance */
collapsed_margin,
); );
// If this flex item establishes a containing block for absolutely-positioned // If this flex item establishes a containing block for absolutely-positioned

View file

@ -23,9 +23,7 @@ use webrender_api::FontInstanceKey;
use super::inline_box::{InlineBoxContainerState, InlineBoxIdentifier, InlineBoxTreePathToken}; use super::inline_box::{InlineBoxContainerState, InlineBoxIdentifier, InlineBoxTreePathToken};
use super::{InlineFormattingContextLayout, LineBlockSizes}; use super::{InlineFormattingContextLayout, LineBlockSizes};
use crate::cell::ArcRefCell; use crate::cell::ArcRefCell;
use crate::fragment_tree::{ use crate::fragment_tree::{BaseFragmentInfo, BoxFragment, Fragment, TextFragment};
BaseFragmentInfo, BoxFragment, CollapsedBlockMargins, Fragment, TextFragment,
};
use crate::geom::{LogicalRect, LogicalVec2, PhysicalRect, ToLogical}; use crate::geom::{LogicalRect, LogicalVec2, PhysicalRect, ToLogical};
use crate::positioned::{ use crate::positioned::{
AbsolutelyPositionedBox, PositioningContext, PositioningContextLength, relative_adjustement, AbsolutelyPositionedBox, PositioningContext, PositioningContextLength, relative_adjustement,
@ -465,7 +463,6 @@ impl LineItemLayout<'_, '_> {
border.to_physical(ifc_writing_mode), border.to_physical(ifc_writing_mode),
margin.to_physical(ifc_writing_mode), margin.to_physical(ifc_writing_mode),
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
); );
let offset_from_parent_ifc = LogicalVec2 { let offset_from_parent_ifc = LogicalVec2 {

View file

@ -355,7 +355,6 @@ impl OutsideMarker {
PhysicalSides::zero(), PhysicalSides::zero(),
PhysicalSides::zero(), PhysicalSides::zero(),
None, None,
CollapsedBlockMargins::zero(),
))) )))
} }
} }
@ -1105,9 +1104,9 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
pbm.border.to_physical(containing_block_writing_mode), pbm.border.to_physical(containing_block_writing_mode),
margin.to_physical(containing_block_writing_mode), margin.to_physical(containing_block_writing_mode),
clearance, clearance,
block_margins_collapsed_with_children,
) )
.with_baselines(flow_layout.baselines) .with_baselines(flow_layout.baselines)
.with_block_margins_collapsed_with_children(block_margins_collapsed_with_children)
} }
impl IndependentNonReplacedContents { impl IndependentNonReplacedContents {
@ -1211,10 +1210,10 @@ impl IndependentNonReplacedContents {
pbm.border.to_physical(containing_block_writing_mode), pbm.border.to_physical(containing_block_writing_mode),
margin.to_physical(containing_block_writing_mode), margin.to_physical(containing_block_writing_mode),
None, /* clearance */ None, /* clearance */
block_margins_collapsed_with_children,
) )
.with_baselines(layout.baselines) .with_baselines(layout.baselines)
.with_specific_layout_info(layout.specific_layout_info) .with_specific_layout_info(layout.specific_layout_info)
.with_block_margins_collapsed_with_children(block_margins_collapsed_with_children)
} }
/// Lay out a normal in flow non-replaced block that establishes an independent /// Lay out a normal in flow non-replaced block that establishes an independent
@ -1516,10 +1515,10 @@ impl IndependentNonReplacedContents {
pbm.border.to_physical(containing_block_writing_mode), pbm.border.to_physical(containing_block_writing_mode),
margin.to_physical(containing_block_writing_mode), margin.to_physical(containing_block_writing_mode),
clearance, clearance,
CollapsedBlockMargins::from_margin(&margin),
) )
.with_baselines(layout.baselines) .with_baselines(layout.baselines)
.with_specific_layout_info(layout.specific_layout_info) .with_specific_layout_info(layout.specific_layout_info)
.with_block_margins_collapsed_with_children(CollapsedBlockMargins::from_margin(&margin))
} }
} }
@ -1653,8 +1652,8 @@ impl ReplacedContents {
pbm.border.to_physical(containing_block_writing_mode), pbm.border.to_physical(containing_block_writing_mode),
margin.to_physical(containing_block_writing_mode), margin.to_physical(containing_block_writing_mode),
clearance, clearance,
CollapsedBlockMargins::from_margin(&margin),
) )
.with_block_margins_collapsed_with_children(CollapsedBlockMargins::from_margin(&margin))
} }
} }
@ -2038,7 +2037,7 @@ impl<'container> PlacementState<'container> {
return; return;
} }
let fragment_block_margins = &fragment.block_margins_collapsed_with_children; let fragment_block_margins = fragment.block_margins_collapsed_with_children();
let mut fragment_block_size = fragment let mut fragment_block_size = fragment
.border_rect() .border_rect()
.size .size
@ -2352,7 +2351,6 @@ impl IndependentFormattingContext {
// so there's no need to store it explicitly in the fragment. // so there's no need to store it explicitly in the fragment.
// And atomic inlines don't have clearance. // And atomic inlines don't have clearance.
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
); );
IndependentFloatOrAtomicLayoutResult { IndependentFloatOrAtomicLayoutResult {

View file

@ -76,7 +76,7 @@ pub(crate) struct BoxFragment {
/// to things such as tables and inline formatting contexts. /// to things such as tables and inline formatting contexts.
baselines: Baselines, baselines: Baselines,
pub block_margins_collapsed_with_children: CollapsedBlockMargins, block_margins_collapsed_with_children: Option<Box<CollapsedBlockMargins>>,
/// The scrollable overflow of this box fragment. /// The scrollable overflow of this box fragment.
pub scrollable_overflow_from_children: PhysicalRect<Au>, pub scrollable_overflow_from_children: PhysicalRect<Au>,
@ -103,7 +103,6 @@ impl BoxFragment {
border: PhysicalSides<Au>, border: PhysicalSides<Au>,
margin: PhysicalSides<Au>, margin: PhysicalSides<Au>,
clearance: Option<Au>, clearance: Option<Au>,
block_margins_collapsed_with_children: CollapsedBlockMargins,
) -> BoxFragment { ) -> BoxFragment {
let scrollable_overflow_from_children = let scrollable_overflow_from_children =
children.iter().fold(PhysicalRect::zero(), |acc, child| { children.iter().fold(PhysicalRect::zero(), |acc, child| {
@ -120,7 +119,7 @@ impl BoxFragment {
margin, margin,
clearance, clearance,
baselines: Baselines::default(), baselines: Baselines::default(),
block_margins_collapsed_with_children, block_margins_collapsed_with_children: None,
scrollable_overflow_from_children, scrollable_overflow_from_children,
resolved_sticky_insets: AtomicRefCell::default(), resolved_sticky_insets: AtomicRefCell::default(),
background_mode: BackgroundMode::Normal, background_mode: BackgroundMode::Normal,
@ -182,6 +181,14 @@ impl BoxFragment {
self self
} }
pub fn with_block_margins_collapsed_with_children(
mut self,
collapsed_margins: CollapsedBlockMargins,
) -> Self {
self.block_margins_collapsed_with_children = Some(collapsed_margins.into());
self
}
pub fn scrollable_overflow(&self) -> PhysicalRect<Au> { pub fn scrollable_overflow(&self) -> PhysicalRect<Au> {
let physical_padding_rect = self.padding_rect(); let physical_padding_rect = self.padding_rect();
let content_origin = self.content_rect.origin.to_vector(); let content_origin = self.content_rect.origin.to_vector();
@ -374,4 +381,11 @@ impl BoxFragment {
_ => false, _ => false,
} }
} }
pub(crate) fn block_margins_collapsed_with_children(&self) -> CollapsedBlockMargins {
match self.block_margins_collapsed_with_children.as_ref() {
Some(collapsed_block_margins) => *(collapsed_block_margins).clone(),
_ => CollapsedBlockMargins::zero(),
}
}
} }

View file

@ -21,8 +21,7 @@ use crate::formatting_contexts::{
IndependentFormattingContext, IndependentFormattingContextContents, IndependentFormattingContext, IndependentFormattingContextContents,
}; };
use crate::fragment_tree::{ use crate::fragment_tree::{
BoxFragment, CollapsedBlockMargins, Fragment, FragmentFlags, HoistedSharedFragment, BoxFragment, Fragment, FragmentFlags, HoistedSharedFragment, SpecificLayoutInfo,
SpecificLayoutInfo,
}; };
use crate::geom::{ use crate::geom::{
AuOrAuto, LengthPercentageOrAuto, LogicalRect, LogicalSides, LogicalSides1D, LogicalVec2, AuOrAuto, LengthPercentageOrAuto, LogicalRect, LogicalSides, LogicalSides1D, LogicalVec2,
@ -697,9 +696,6 @@ impl HoistedAbsolutelyPositionedBox {
pbm.border.to_physical(containing_block_writing_mode), pbm.border.to_physical(containing_block_writing_mode),
margin.to_physical(containing_block_writing_mode), margin.to_physical(containing_block_writing_mode),
None, /* clearance */ None, /* clearance */
// We do not set the baseline offset, because absolutely positioned
// elements are not inflow.
CollapsedBlockMargins::zero(),
) )
.with_specific_layout_info(specific_layout_info) .with_specific_layout_info(specific_layout_info)
}; };

View file

@ -1785,7 +1785,6 @@ impl<'a> TableLayout<'a> {
self.pbm.border.to_physical(table_writing_mode), self.pbm.border.to_physical(table_writing_mode),
PhysicalSides::zero(), PhysicalSides::zero(),
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
) )
.with_specific_layout_info(self.specific_layout_info_for_grid()); .with_specific_layout_info(self.specific_layout_info_for_grid());
} }
@ -1911,7 +1910,6 @@ impl<'a> TableLayout<'a> {
self.pbm.border.to_physical(table_writing_mode), self.pbm.border.to_physical(table_writing_mode),
PhysicalSides::zero(), PhysicalSides::zero(),
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
) )
.with_baselines(baselines) .with_baselines(baselines)
.with_specific_layout_info(self.specific_layout_info_for_grid()) .with_specific_layout_info(self.specific_layout_info_for_grid())
@ -2338,7 +2336,6 @@ impl<'a> RowFragmentLayout<'a> {
PhysicalSides::zero(), /* border */ PhysicalSides::zero(), /* border */
PhysicalSides::zero(), /* margin */ PhysicalSides::zero(), /* margin */
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
); );
row_fragment.set_does_not_paint_background(); row_fragment.set_does_not_paint_background();
@ -2404,7 +2401,6 @@ impl RowGroupFragmentLayout {
PhysicalSides::zero(), /* border */ PhysicalSides::zero(), /* border */
PhysicalSides::zero(), /* margin */ PhysicalSides::zero(), /* margin */
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
); );
row_group_fragment.set_does_not_paint_background(); row_group_fragment.set_does_not_paint_background();
@ -2887,7 +2883,6 @@ impl TableSlotCell {
layout.border.to_physical(table_style.writing_mode), layout.border.to_physical(table_style.writing_mode),
PhysicalSides::zero(), /* margin */ PhysicalSides::zero(), /* margin */
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(),
) )
.with_baselines(layout.layout.baselines) .with_baselines(layout.layout.baselines)
.with_specific_layout_info(specific_layout_info) .with_specific_layout_info(specific_layout_info)

View file

@ -23,8 +23,8 @@ use crate::fragment_tree::{
BoxFragment, CollapsedBlockMargins, Fragment, FragmentFlags, SpecificLayoutInfo, BoxFragment, CollapsedBlockMargins, Fragment, FragmentFlags, SpecificLayoutInfo,
}; };
use crate::geom::{ use crate::geom::{
LogicalSides, LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSides, PhysicalSize, Size, LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSides, PhysicalSize, Size, SizeConstraint,
SizeConstraint, Sizes, Sizes,
}; };
use crate::layout_box_base::CacheableLayoutResult; use crate::layout_box_base::CacheableLayoutResult;
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext, PositioningContextLength}; use crate::positioned::{AbsolutelyPositionedBox, PositioningContext, PositioningContextLength};
@ -486,15 +486,6 @@ impl TaffyContainer {
.map(|child| (**child).borrow_mut()) .map(|child| (**child).borrow_mut())
.enumerate() .enumerate()
.map(|(child_id, mut child)| { .map(|(child_id, mut child)| {
fn rect_to_logical_sides<T>(rect: taffy::Rect<T>) -> LogicalSides<T> {
LogicalSides {
inline_start: rect.left,
inline_end: rect.right,
block_start: rect.top,
block_end: rect.bottom,
}
}
fn rect_to_physical_sides<T>(rect: taffy::Rect<T>) -> PhysicalSides<T> { fn rect_to_physical_sides<T>(rect: taffy::Rect<T>) -> PhysicalSides<T> {
PhysicalSides::new(rect.top, rect.right, rect.bottom, rect.left) PhysicalSides::new(rect.top, rect.right, rect.bottom, rect.left)
} }
@ -514,8 +505,6 @@ impl TaffyContainer {
let padding = rect_to_physical_sides(layout.padding.map(Au::from_f32_px)); let padding = rect_to_physical_sides(layout.padding.map(Au::from_f32_px));
let border = rect_to_physical_sides(layout.border.map(Au::from_f32_px)); let border = rect_to_physical_sides(layout.border.map(Au::from_f32_px));
let margin = rect_to_physical_sides(layout.margin.map(Au::from_f32_px)); let margin = rect_to_physical_sides(layout.margin.map(Au::from_f32_px));
let logical_margin = rect_to_logical_sides(layout.margin.map(Au::from_f32_px));
let collapsed_margin = CollapsedBlockMargins::from_margin(&logical_margin);
// Compute content box size and position. // Compute content box size and position.
// //
@ -576,7 +565,6 @@ impl TaffyContainer {
border, border,
margin, margin,
None, /* clearance */ None, /* clearance */
collapsed_margin,
) )
.with_baselines(Baselines { .with_baselines(Baselines {
first: output.first_baselines.y.map(Au::from_f32_px), first: output.first_baselines.y.map(Au::from_f32_px),