Truly independent web browser
LibWeb: Place a cached run's reused subtree with its committed fragment
Andreas Kling003ce8aaaff1
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex c92f44857c8..195a1f50997 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -72,6 +72,14 @@ impl FcRunCacheKey {}}+ pub(super) fn fc_type(&self) -> formatting_context::FormattingContextType {+ self.fc_type+ }++ pub(super) fn has_no_table_cell_intrinsic_block_padding(&self) -> bool {+ self.input.sizing.table_cell_intrinsic_block_padding.is_none()+ }+/// The key with the intrinsic block padding a table hands its cell taken out of the root's block paddings.fn without_table_cell_intrinsic_block_padding(&self) -> Self {let mut key = *self;diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex c12b76d884e..11ac798fe1e 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -348,6 +348,24 @@ pub(crate) fn place_child_in_containing_block(let used = records.used_values(node);set_placement(used, containing_block, offset);let fragments = run.fragments.as_deref()?;+ if let Some(fragment) = fragments.take_reused_committed_fragment(node) {+ fragments.attach_reused_subtree(+ node,+ fragment.clone(),+ (!containing_block.is_invalid()).then_some(containing_block),+ used,+ containing_block_is_sealed(records, containing_block),+ resolve_containing_line_box_index(records, callbacks, node, containing_block, containing_line_box_fragment),+ point_add(+ offset,+ committed_offset_delta_at_placement(run.purpose, records, callbacks, node, containing_block, used),+ ),+ );+ return Some(fragment_tree::PlacedFragment {+ fragment,+ exports_payloads: false,+ });+ }fragments.normalize_arrivals_for_placement(node, callbacks);loop {let batch = fragments.take_drainable_abspos(node, records, callbacks);@@ -407,6 +425,7 @@ pub(crate) fn place_reused_child((!containing_block.is_invalid()).then_some(containing_block),used,containing_block_is_sealed(records, containing_block),+ None,point_add(offset,committed_offset_delta_at_placement(run.purpose, records, callbacks, node, containing_block, used),@@ -1627,6 +1646,17 @@ pub(super) fn run_formatting_context() {Ok(attempt) => attempt,Err(entry) => {+ if let Some(result) = reuse_committed_subtree_as_it_is(+ purpose,+ parent_fragments,+ parent_used,+ box_,+ &callbacks,+ &cache_key,+ &entry,+ ) {+ return result;+ }let reuses_committed_subtree = entry.can_reuse_committed_subtree();let _trace = callbacks.arena()@@ -1673,6 +1703,48 @@ pub(super) fn run_formatting_context(absorb_run_outputs(parent_fragments, parent_used, box_, outputs, false)}+/// Takes a committing run of `box_` from the cache entry `entry` with nothing replayed: its root record gets what the+/// stored run left, and the run's placement attaches the box's committed fragment as it is. That is exact where the+/// committed fragment is the one the stored run produced, which a plain box shows by holding the record's metrics and+/// the very line data the run left, and where nothing from the stored run reaches past its root.+fn reuse_committed_subtree_as_it_is(+ purpose: LayoutPurpose,+ parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,+ root_used: &UsedValues,+ box_: Node,+ callbacks: &LayoutPass<'_>,+ cache_key: &fc_run_cache::FcRunCacheKey,+ entry: &fc_run_cache::FcRunCacheEntry,+) -> Option<ChildLayoutResult> {+ let fragments = parent_fragments?;+ if purpose != LayoutPurpose::Commit+ || !entry.can_reuse_committed_subtree()+ || entry.is_uncommitted()+ || !cache_key.has_no_table_cell_intrinsic_block_padding()+ || entry.outputs.root.as_ref().is_some_and(|root| !root.carries_nothing())+ || NodeFacts::new(callbacks, box_).has_anchor_names()+ {+ return None;+ }+ let fragment = callbacks+ .arena()+ .with_current_committed_fragment(box_, |fragment| fragment.is_plain_box().then(|| fragment.clone()))+ .flatten()?;+ entry.outputs.root_outcome.clone().apply_to_record(root_used);+ if !root_used.shows_box_of_fragment(&fragment) {+ return None;+ }+ let _trace = callbacks.arena().layout_trace.run(+ box_,+ cache_key.fc_type(),+ purpose,+ LayoutMode::Normal,+ || "REUSE SUBTREE",+ );+ fragments.hold_reused_committed_fragment(box_, fragment);+ Some(entry.outputs.result)+}+#[expect(clippy::too_many_arguments)]fn execute_formatting_context_run(purpose: LayoutPurpose,diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 64b6cf6ca28..1c05e8719e1 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -77,6 +77,25 @@ impl Fragment {.all(|(link, previous_link)| link.places_same_fragment_identically_to(previous_link))}+ /// Whether the fragment holds nothing beyond a plain box's metrics, line data and children: no table, grid, flex,+ /// SVG or line clamp state.+ pub(crate) fn is_plain_box(&self) -> bool {+ !self.is_collapsed_borders_table_box+ && self.table_column_index == 0+ && self.table_column_span == 0+ && !self.hidden_by_collapsed_columns+ && self.collapsed_table_borders.is_none()+ && self.grid_layout_data.is_none()+ && self.flex_layout_data.is_none()+ && self.used_grid_tracks.is_none()+ && self.svg == CommittedSvgFacts::default()+ && self.computed_svg_path.is_none()+ && self.svg_text_character_cells.is_none()+ && !self.has_line_clamp_point+ && !self.is_invisible_for_line_clamp+ && !self.clamped_content_is_scrollable_overflow+ }+/// Box properties excluding inline content and child fragments. Painting compares those/// separately: a new descendant fragment does not necessarily change this box's commands.pub(crate) fn has_same_box_properties(&self, previous: &Fragment) -> bool {@@ -518,6 +537,18 @@ pub(crate) struct UnplacedRootFragment {pub(crate) propagated_abspos_containing_block_info: Vec<AbsposContainingBlockInfoContribution>,}+impl UnplacedRootFragment {+ /// Whether the root holds nothing past its box: no descendant fragment, reuse marker or payload for an ancestor.+ pub(crate) fn carries_nothing(&self) -> bool {+ self.scoped_descendants.is_empty()+ && self.reused_subtree_roots.is_empty()+ && self.propagated_pending_abspos.is_empty()+ && self.propagated_anchor_candidates.is_empty()+ && self.propagated_inline_containing_block_rects.is_empty()+ && self.propagated_abspos_containing_block_info.is_empty()+ }+}+pub(crate) struct CompletedPassFragments {pub(crate) roots: Vec<FragmentLink>,pub(crate) reused_subtree_roots: HashSet<u32>,@@ -589,6 +620,8 @@ struct RunFragmentBuilderInner {abspos_containing_block_info_contributions_received: u64,top_scope_links: Vec<FragmentLink>,reused_subtree_roots: HashSet<u32>,+ /// The committed fragments of reused subtree roots whose runs this run took from the cache, placed as they are.+ reused_committed_fragments: HashMap<u32, (crate::layout::node_data::NodeSlotId, std::sync::Arc<Fragment>)>,}impl RunFragmentBuilderInner {@@ -859,7 +892,34 @@ impl RunFragmentBuilder {}pub(crate) fn clear_reused_subtree_root(&self, node: crate::layout::node_data::NodeSlotId) {- self.inner.borrow_mut().reused_subtree_roots.remove(&node.slot_index());+ let mut inner = self.inner.borrow_mut();+ inner.reused_subtree_roots.remove(&node.slot_index());+ inner.reused_committed_fragments.remove(&node.slot_index());+ }++ /// Notes that the subtree of `node`, whose run this run took from the cache, is reused with its committed fragment,+ /// which its placement attaches as it is.+ pub(crate) fn hold_reused_committed_fragment(+ &self,+ node: crate::layout::node_data::NodeSlotId,+ fragment: std::sync::Arc<Fragment>,+ ) {+ let mut inner = self.inner.borrow_mut();+ inner.reused_subtree_roots.insert(node.slot_index());+ inner+ .reused_committed_fragments+ .insert(node.slot_index(), (node, fragment));+ }++ pub(crate) fn take_reused_committed_fragment(+ &self,+ node: crate::layout::node_data::NodeSlotId,+ ) -> Option<std::sync::Arc<Fragment>> {+ self.inner+ .borrow_mut()+ .reused_committed_fragments+ .remove(&node.slot_index())+ .map(|(_, fragment)| fragment)}pub(crate) fn discard_unplaced_subtree(&self, node: crate::layout::node_data::NodeSlotId) {@@ -871,6 +931,7 @@ impl RunFragmentBuilder {}}inner.reused_subtree_roots.remove(&slot);+ inner.reused_committed_fragments.remove(&slot);inner.pending_fragments.remove(&slot);}@@ -1008,6 +1069,7 @@ impl RunFragmentBuilder {/// Places the committed fragment of a box whose subtree is reused as it is. Commit stops at the box, so its/// committed descendants stay as they are.+ #[expect(clippy::too_many_arguments)]pub(crate) fn attach_reused_subtree(&self,node: crate::layout::node_data::NodeSlotId,@@ -1015,6 +1077,7 @@ impl RunFragmentBuilder {containing_block: Option<crate::layout::node_data::NodeSlotId>,used: &UsedValues,containing_block_is_sealed: bool,+ containing_line_box_index: Option<usize>,committed_offset: FfiCssPixelPoint,) {let mut inner = self.inner.borrow_mut();@@ -1023,7 +1086,10 @@ impl RunFragmentBuilder {!inner.child_roots_awaiting_placement.contains_key(&slot) && !inner.pending_fragments.contains_key(&slot),"a reused subtree's box has fragments of its own in this run (slot {slot})");- let link = link_fragment(fragment, PlacementData::from_record(used, None, committed_offset));+ let link = link_fragment(+ fragment,+ PlacementData::from_record(used, containing_line_box_index, committed_offset),+ );self.attach(&mut inner, link, containing_block, containing_block_is_sealed);inner.reused_subtree_roots.insert(slot);}@@ -1115,6 +1181,16 @@ impl RunFragmentBuilder {PlacementData::from_record(used, None, used.content_offset.get()),));}+ // NB: A reused subtree root its run never placed has no children of its own in this run, as an empty held+ // root has, and closes the same way.+ let reused_committed_fragments = std::mem::take(&mut inner.reused_committed_fragments);+ for (_, (node, _)) in reused_committed_fragments {+ let used = records.used_values(node);+ inner.top_scope_links.push(link_fragment(+ snapshot_fragment(callbacks, node, Vec::new(), used),+ PlacementData::from_record(used, None, used.content_offset.get()),+ ));+ }let child_roots_awaiting_placement = std::mem::take(&mut inner.child_roots_awaiting_placement);for (_, root) in child_roots_awaiting_placement {let used = records.used_values(root.node);diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex 230bb8112fb..9c3c7865274 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -685,6 +685,31 @@ impl UsedValues {self.padding_bottom.set(fragment.padding_bottom);}+ /// Whether `fragment` shows this record's box as it stands: its metrics, and the very line data it holds.+ pub(crate) fn shows_box_of_fragment(&self, fragment: &fragment_tree::Fragment) -> bool {+ let same_line_data = match (&fragment.line_data, &*self.line_data.borrow()) {+ (None, LineDataState::Empty) => true,+ (Some(left), LineDataState::Finished(right)) => std::sync::Arc::ptr_eq(left, right),+ _ => false,+ };+ same_line_data+ && fragment.content_inline_size == self.content_inline_size.get()+ && fragment.content_block_size == self.content_block_size.get()+ && fragment.margin_left == self.margin_left.get()+ && fragment.margin_right == self.margin_right.get()+ && fragment.margin_top == self.margin_top.get()+ && fragment.margin_bottom == self.margin_bottom.get()+ && fragment.border_left == self.border_left.get()+ && fragment.border_right == self.border_right.get()+ && fragment.border_top == self.border_top.get()+ && fragment.border_bottom == self.border_bottom.get()+ && fragment.padding_left == self.padding_left.get()+ && fragment.padding_right == self.padding_right.get()+ && fragment.padding_top == self.padding_top.get()+ && fragment.padding_bottom == self.padding_bottom.get()+ && fragment.uses_collapsing_borders_model == self.uses_collapsing_borders_model.get()+ }+fn collapsed_border_share(&self, width: CssPixels, start_edge: bool) -> CssPixels {collapsed_border_share(width, start_edge, self.is_collapsed_borders_table_box.get())}diff --git a/Tests/LibWeb/Text/input/display_list/paint-cache-scrollability-flip-scope.html b/Tests/LibWeb/Text/input/display_list/paint-cache-scrollability-flip-scope.htmlindex 78e112bcb3b..ddb42ac00de 100644--- a/Tests/LibWeb/Text/input/display_list/paint-cache-scrollability-flip-scope.html+++ b/Tests/LibWeb/Text/input/display_list/paint-cache-scrollability-flip-scope.html@@ -123,7 +123,7 @@ displayListTest({mutate: () => { filler.className = "tall"; },check: equal => { equal(nestedElementScrollers(), 1, "inner reports outer as its parent"); },expect: `- damage: rows=8 moved=0 order=1 eligibility=0 all=false+ damage: rows=8 moved=0 order=0 eligibility=0 all=false@canvas RECORD (empty)@viewport ASSEMBLE@viewport/preamble COPY@@ -146,7 +146,7 @@ displayListTest({BlockContainer<BODY>#body/scroll-metadata COPYBlockContainer<BODY>#body/background COPYBlockContainer<BODY>#body/border COPY- Box<DIV>#row.row/descendants(background-and-borders) REPLAN+ Box<DIV>#row.row/descendants(background-and-borders) ASSEMBLEBox<DIV>#row.row/background/hit-test COPYBox<DIV>#row.row/scroll-metadata COPYBox<DIV>#row.row/background COPY@@ -160,7 +160,7 @@ displayListTest({BlockContainer<BODY>#body/descendants(foreground) ASSEMBLEBlockContainer<BODY>#body/foreground/hit-test COPYBlockContainer<BODY>#body/foreground COPY- Box<DIV>#row.row/descendants(foreground) REPLAN+ Box<DIV>#row.row/descendants(foreground) ASSEMBLEBox<DIV>#row.row/foreground/hit-test COPYBox<DIV>#row.row/foreground COPYBlockContainer<DIV>#row-0.box/descendants(foreground) COPYdiff --git a/Tests/LibWeb/Text/input/display_list/paint-cache-unchanged-relayout.html b/Tests/LibWeb/Text/input/display_list/paint-cache-unchanged-relayout.htmlindex f291c548c63..2d8c804bc6e 100644--- a/Tests/LibWeb/Text/input/display_list/paint-cache-unchanged-relayout.html+++ b/Tests/LibWeb/Text/input/display_list/paint-cache-unchanged-relayout.html@@ -42,60 +42,9 @@ displayListTest({equal(partial.getBoundingClientRect().width, 100, "canvas width");},expect: `- damage: rows=4 moved=0 order=1 eligibility=0 all=false+ damage: rows=0 moved=0 order=0 eligibility=0 all=false@canvas RECORD (empty)- @viewport ASSEMBLE- @viewport/preamble COPY- @viewport/background/hit-test COPY- @viewport/scroll-metadata COPY- @viewport/background COPY- @viewport/border COPY- BlockContainer<HTML>/descendants(background-and-borders) ASSEMBLE- @viewport/foreground/hit-test COPY- @viewport/foreground COPY- BlockContainer<HTML>/descendants(foreground) ASSEMBLE- BlockContainer<HTML> ASSEMBLE- BlockContainer<HTML>/preamble COPY- BlockContainer<HTML>/background/hit-test COPY- BlockContainer<HTML>/scroll-metadata COPY- BlockContainer<HTML>/background COPY- BlockContainer<HTML>/border COPY- BlockContainer<BODY>/descendants(background-and-borders) ASSEMBLE- BlockContainer<BODY>/background/hit-test COPY- BlockContainer<BODY>/scroll-metadata COPY- BlockContainer<BODY>/background COPY- BlockContainer<BODY>/border COPY- CanvasBox<CANVAS>#full/descendants(background-and-borders) COPY- BlockContainer<DIV>#boundary/descendants(background-and-borders) REPLAN- BlockContainer<BODY>/descendants(inline-background-and-borders) ASSEMBLE- CanvasBox<CANVAS>#full/descendants(inline-background-and-borders) COPY- BlockContainer<DIV>#boundary/descendants(inline-background-and-borders) REPLAN- BlockContainer<HTML>/foreground/hit-test COPY- BlockContainer<HTML>/foreground COPY- BlockContainer<BODY>/descendants(foreground) ASSEMBLE- BlockContainer<BODY>/foreground/hit-test COPY- BlockContainer<BODY>/foreground COPY- CanvasBox<CANVAS>#full/descendants(foreground) COPY- BlockContainer<DIV>#boundary/descendants(foreground) REPLAN- BlockContainer<BODY>/outline COPY- BlockContainer<BODY>/overlay/hit-test COPY- BlockContainer<BODY>/overlay COPY- BlockContainer<DIV>#boundary REPLAN- BlockContainer<DIV>#boundary/background/hit-test COPY- BlockContainer<DIV>#boundary/scroll-metadata COPY- BlockContainer<DIV>#boundary/background COPY- BlockContainer<DIV>#boundary/border COPY- CanvasBox<CANVAS>#partial/descendants(background-and-borders) COPY- CanvasBox<CANVAS>#partial/descendants(floats) COPY- CanvasBox<CANVAS>#partial/descendants(inline-background-and-borders) COPY- BlockContainer<DIV>#boundary/foreground/hit-test COPY- BlockContainer<DIV>#boundary/foreground COPY- CanvasBox<CANVAS>#partial/descendants(foreground) COPY- BlockContainer<DIV>#boundary/outline COPY- BlockContainer<DIV>#boundary/overlay/hit-test COPY- BlockContainer<DIV>#boundary/overlay COPY- BlockContainer<HTML>/outline COPY- @viewport/outline COPY+ @viewport COPY`,},],diff --git a/Tests/LibWeb/Text/input/display_list/paint-damage-scroller-content-move.html b/Tests/LibWeb/Text/input/display_list/paint-damage-scroller-content-move.htmlindex 2bec4af4dd0..56da6394d5c 100644--- a/Tests/LibWeb/Text/input/display_list/paint-damage-scroller-content-move.html+++ b/Tests/LibWeb/Text/input/display_list/paint-damage-scroller-content-move.html@@ -20,7 +20,7 @@ displayListTest({mutate: () => { row1.style.marginTop = "10px"; },check: equal => { equal(scroller.scrollHeight, 190, "scroll height"); },expect: `- damage: rows=6 moved=2 order=1 eligibility=0 all=false+ damage: rows=6 moved=2 order=0 eligibility=0 all=false@canvas RECORD (empty)@viewport ASSEMBLE@viewport/preamble COPY@@ -43,7 +43,7 @@ displayListTest({BlockContainer<BODY>#body/scroll-metadata COPYBlockContainer<BODY>#body/background COPYBlockContainer<BODY>#body/border COPY- BlockContainer<DIV>#scroller/descendants(background-and-borders) REPLAN+ BlockContainer<DIV>#scroller/descendants(background-and-borders) ASSEMBLEBlockContainer<DIV>#scroller/background/hit-test COPYBlockContainer<DIV>#scroller/scroll-metadata RECORDBlockContainer<DIV>#scroller/background COPY@@ -64,7 +64,7 @@ displayListTest({BlockContainer<BODY>#body/descendants(foreground) ASSEMBLEBlockContainer<BODY>#body/foreground/hit-test COPYBlockContainer<BODY>#body/foreground COPY- BlockContainer<DIV>#scroller/descendants(foreground) REPLAN+ BlockContainer<DIV>#scroller/descendants(foreground) ASSEMBLEBlockContainer<DIV>#scroller/foreground/hit-test COPYBlockContainer<DIV>#scroller/foreground COPYBlockContainer<DIV>#row0.row/descendants(foreground) COPYdiff --git a/Tests/LibWeb/Text/input/display_list/paint-damage-snap-area-move.html b/Tests/LibWeb/Text/input/display_list/paint-damage-snap-area-move.htmlindex af51a88ce10..50992a564e4 100644--- a/Tests/LibWeb/Text/input/display_list/paint-damage-snap-area-move.html+++ b/Tests/LibWeb/Text/input/display_list/paint-damage-snap-area-move.html@@ -27,7 +27,7 @@ displayListTest({name: "move the second snap area",mutate: () => { area1.style.marginTop = "10px"; },expect: `- damage: rows=6 moved=2 order=1 eligibility=0 all=false+ damage: rows=6 moved=2 order=0 eligibility=0 all=false@canvas RECORD (empty)@viewport ASSEMBLE@viewport/preamble COPY@@ -50,7 +50,7 @@ displayListTest({BlockContainer<BODY>#body/scroll-metadata COPYBlockContainer<BODY>#body/background COPYBlockContainer<BODY>#body/border COPY- BlockContainer<DIV>#snap/descendants(background-and-borders) REPLAN+ BlockContainer<DIV>#snap/descendants(background-and-borders) ASSEMBLEBlockContainer<DIV>#snap/background/hit-test COPYBlockContainer<DIV>#snap/scroll-metadata RECORDBlockContainer<DIV>#snap/background COPY@@ -71,7 +71,7 @@ displayListTest({BlockContainer<BODY>#body/descendants(foreground) ASSEMBLEBlockContainer<BODY>#body/foreground/hit-test COPYBlockContainer<BODY>#body/foreground COPY- BlockContainer<DIV>#snap/descendants(foreground) REPLAN+ BlockContainer<DIV>#snap/descendants(foreground) ASSEMBLEBlockContainer<DIV>#snap/foreground/hit-test COPYBlockContainer<DIV>#snap/foreground COPYBlockContainer<DIV>#area0.area/descendants(foreground) COPY