Skip to content

Truly independent web browser

LibWeb: Place an unchanged empty box with its committed fragment

Andreas Klinge5bb046474bc
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 11ac798fe1e..eaaa7c769c9 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -348,10 +348,11 @@ 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(
+ if let Some((fragment, reuses_subtree)) = fragments.take_reused_committed_fragment(node) {
+ fragments.attach_committed_fragment(
node,
fragment.clone(),
+ reuses_subtree,
(!containing_block.is_invalid()).then_some(containing_block),
used,
containing_block_is_sealed(records, containing_block),
@@ -419,9 +420,10 @@ pub(crate) fn place_reused_child(
.fragments
.as_deref()
.expect("a reused subtree is placed by a run that builds fragments");
- fragments.attach_reused_subtree(
+ fragments.attach_committed_fragment(
node,
fragment,
+ true,
(!containing_block.is_invalid()).then_some(containing_block),
used,
containing_block_is_sealed(records, containing_block),
@@ -2169,12 +2171,23 @@ pub(crate) fn layout_inside_child(
{
// OPTIMIZATION: An empty atomic block has no formatting-context body output. Size it in the
// parent run, which will also construct its fragment when placing the box.
- run.sizing().dimension_empty_atomic_root(
+ let committed_fragment = run.sizing().dimension_empty_atomic_root(
child,
input.available_space,
input.containing_block_constraints,
layout_mode,
);
+ // OPTIMIZATION: A box that took its metrics from its committed fragment is placed with that fragment, where
+ // it shows the box as the record holds it.
+ if let Some(fragment) = committed_fragment
+ && run.purpose == LayoutPurpose::Commit
+ && let Some(fragments) = run.fragments.as_deref()
+ && fragment.is_plain_box()
+ && used.shows_box_of_fragment(&fragment)
+ && !facts.has_anchor_names()
+ {
+ fragments.hold_committed_fragment_of_empty_box(child, fragment);
+ }
store_derived_baselines(used, DerivedBaselines::default());
note_skipped_child_dependency();
return ChildLayoutOutcome::Skipped;
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 048512a59f4..feceabb2328 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -621,8 +621,15 @@ 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>)>,
+ /// The committed fragments of reused subtree roots whose runs this run took from the cache, and of empty boxes sized
+ /// from them, placed as they are.
+ reused_committed_fragments: HashMap<u32, HeldCommittedFragment>,
+}
+
+struct HeldCommittedFragment {
+ node: crate::layout::node_data::NodeSlotId,
+ fragment: std::sync::Arc<Fragment>,
+ reuses_subtree: bool,
}
impl RunFragmentBuilderInner {
@@ -907,20 +914,43 @@ impl RunFragmentBuilder {
) {
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));
+ inner.reused_committed_fragments.insert(
+ node.slot_index(),
+ HeldCommittedFragment {
+ node,
+ fragment,
+ reuses_subtree: true,
+ },
+ );
}
+ /// Notes that the empty box `node`, sized from its committed fragment, is placed with that fragment as it is. Commit
+ /// keeps a childless box that holds its committed fragment, so the box is no reused subtree root.
+ pub(crate) fn hold_committed_fragment_of_empty_box(
+ &self,
+ node: crate::layout::node_data::NodeSlotId,
+ fragment: std::sync::Arc<Fragment>,
+ ) {
+ self.inner.borrow_mut().reused_committed_fragments.insert(
+ node.slot_index(),
+ HeldCommittedFragment {
+ node,
+ fragment,
+ reuses_subtree: false,
+ },
+ );
+ }
+
+ /// The committed fragment held for `node`, and whether it is the root of a reused subtree.
pub(crate) fn take_reused_committed_fragment(
&self,
node: crate::layout::node_data::NodeSlotId,
- ) -> Option<std::sync::Arc<Fragment>> {
+ ) -> Option<(std::sync::Arc<Fragment>, bool)> {
self.inner
.borrow_mut()
.reused_committed_fragments
.remove(&node.slot_index())
- .map(|(_, fragment)| fragment)
+ .map(|held| (held.fragment, held.reuses_subtree))
}
pub(crate) fn discard_unplaced_subtree(&self, node: crate::layout::node_data::NodeSlotId) {
@@ -1068,13 +1098,14 @@ 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.
+ /// Places the committed fragment of a box as it is. Where the box's 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(
+ pub(crate) fn attach_committed_fragment(
&self,
node: crate::layout::node_data::NodeSlotId,
fragment: std::sync::Arc<Fragment>,
+ reuses_subtree: bool,
containing_block: Option<crate::layout::node_data::NodeSlotId>,
used: &UsedValues,
containing_block_is_sealed: bool,
@@ -1092,7 +1123,9 @@ impl RunFragmentBuilder {
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);
+ if reuses_subtree {
+ inner.reused_subtree_roots.insert(slot);
+ }
}
/// How many abspos containing block contributions the run has collected so far.
@@ -1184,8 +1217,12 @@ impl RunFragmentBuilder {
}
// 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.
+ // An empty box that was never placed has no fragment in this run.
let reused_committed_fragments = std::mem::take(&mut inner.reused_committed_fragments);
- for (_, (node, _)) in reused_committed_fragments {
+ for (_, HeldCommittedFragment { node, .. }) in reused_committed_fragments
+ .into_iter()
+ .filter(|(_, held)| held.reuses_subtree)
+ {
let used = records.used_values(node);
inner.top_scope_links.push(link_fragment(
snapshot_fragment(callbacks, node, Vec::new(), used),
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 2c6ccebd0b1..af95cbd706b 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -1600,23 +1600,26 @@ impl<'pass> SizingContext<'pass> {
used.resolve_box_model(&style, containing_inline_size);
}
+ /// Sizes an empty atomic block. Where a committing layout takes its metrics from its committed fragment as they
+ /// are, returns that fragment.
pub(crate) fn dimension_empty_atomic_root(
&self,
node: Node,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
layout_mode: LayoutMode,
- ) {
+ ) -> Option<std::sync::Arc<fragment_tree::Fragment>> {
if layout_mode == LayoutMode::Normal
&& !self.purpose.is_measurement()
- && self.try_reuse_empty_atomic_root_metrics(node)
+ && let Some(fragment) = self.try_reuse_empty_atomic_root_metrics(node)
{
- return;
+ return Some(fragment);
}
self.dimension_empty_atomic_root_fresh(node, available_space, constraints, layout_mode);
+ None
}
- fn try_reuse_empty_atomic_root_metrics(&self, node: Node) -> bool {
+ fn try_reuse_empty_atomic_root_metrics(&self, node: Node) -> Option<std::sync::Arc<fragment_tree::Fragment>> {
let facts = self.facts(node);
debug_assert!(
formatting_context::formatting_context_type_created_by_box(facts)
@@ -1630,7 +1633,7 @@ impl<'pass> SizingContext<'pass> {
|| facts.is_scroll_container()
|| !self.block_root_inline_size_follows_from_style(node)
{
- return false;
+ return None;
}
let style = self.style(node);
if !(style.width().is_auto() || style.width().is_length())
@@ -1641,18 +1644,20 @@ impl<'pass> SizingContext<'pass> {
|| !(style.max_height().is_none() || style.max_height().is_length())
|| Self::box_edges_contain_percentage(style)
{
- return false;
+ return None;
}
let used = self.used(node);
if used.inline_size_constraint.get() != SizeConstraint::None
|| used.block_size_constraint.get() != SizeConstraint::None
{
- return false;
+ return None;
}
self.callbacks
.arena()
- .with_current_committed_fragment(node, |fragment| used.set_box_metrics_from_fragment(fragment))
- .is_some()
+ .with_current_committed_fragment(node, |fragment| {
+ used.set_box_metrics_from_fragment(fragment);
+ fragment.clone()
+ })
}
fn dimension_empty_atomic_root_fresh(