Truly independent web browser
LibWeb: Stop keeping a parent's copy of a child's record
Aliaksandr Kalenik182efdf47a9d
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex 2f3a467e0b9..bb1257eeff8 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -1073,7 +1073,6 @@ mod tests {root,root_outcome: formatting_context::RunRootOutcome {input_cells_left_by_run: Some(used_values::RootInputCells::capture(&UsedValues::default())),- own_metrics_sealed: false,line_data: None,},// NB: As a run whose result depends on no percentage block size proves.diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex fdf4e335e7b..fa7ef9f307c 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -129,7 +129,6 @@ pub(crate) fn run_root_without_parent_fragments(fragment_arrival,..} = run_formatting_context(- None,None,root_cells,node,@@ -1070,7 +1069,6 @@ pub(crate) enum OwnBlockSizeObservation {pub(crate) struct RunRootOutcome {/// The input cells the run left its root, unless its parent owns them.pub(super) input_cells_left_by_run: Option<used_values::RootInputCells>,- pub(super) own_metrics_sealed: bool,pub(super) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,}@@ -1096,23 +1094,6 @@ impl RunRootOutcome {orthogonal_flow_baselines: result.orthogonal_flow_baselines,}}-- fn apply_to_record(self, record: &UsedValues, content_block_size_adopted_after_run: Option<CssPixels>) {- if let Some(cells) = self.input_cells_left_by_run {- cells.apply_to_record(record);- }- if let Some(block_size) = content_block_size_adopted_after_run- && record.content_block_size.get() != clamp_to_max_dimension_value(block_size.max(CssPixels::default()))- {- record.set_content_block_size(block_size);- }- if let Some(line_data) = self.line_data {- record.set_finished_line_data(line_data);- }- if self.own_metrics_sealed {- record.seal_own_metrics();- }- }}#[derive(Clone)]@@ -1370,7 +1351,6 @@ impl<'pass> FormattingContextRun<'pass> {let root_outcome = RunRootOutcome {input_cells_left_by_run: (!parent_owns_root_input_cells).then(|| used_values::RootInputCells::capture(record)),- own_metrics_sealed: record.own_metrics_are_sealed(),line_data: record.finish_line_data(&self.callbacks),};RunOutputs {@@ -1995,11 +1975,9 @@ impl ChildRunOutcome {}#[expect(clippy::too_many_arguments)]-/// Runs the formatting context of `box_` with the input cells `root_cells` its parent sized it in. A parent that still-/// keeps a record of the box hands it as `parent_used`, which takes the cells and what the run hands back.+/// Runs the formatting context of `box_` with the input cells `root_cells` its parent sized it in.pub(super) fn run_formatting_context(parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,- parent_used: Option<&UsedValues>,root_cells: used_values::RootInputCells,box_: Node,root_containing_block: Node,@@ -2010,9 +1988,6 @@ pub(super) fn run_formatting_context(input: LayoutInput,table_inline_layout: Option<table_formatting_context::TableInlineLayout>,) -> ChildRunOutcome {- if let Some(parent_used) = parent_used {- root_cells.apply_to_record(parent_used);- }let builds_fragments = run_builds_fragments(&input, parent_fragments.is_some());assert!(parent_fragments.is_none() || builds_fragments,@@ -2033,16 +2008,7 @@ pub(super) fn run_formatting_context(table_inline_layout,None,);- return absorb_run_outputs(- &callbacks,- parent_fragments,- parent_used,- box_,- &input,- root_cells,- outputs,- false,- );+ return child_run_outcome_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false);}let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, input, root_cells);let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(@@ -2058,7 +2024,6 @@ pub(super) fn run_formatting_context(Err(hit) => {if let Some(outcome) = reuse_committed_subtree_as_it_is(parent_fragments,- parent_used,box_,&input,root_cells,@@ -2082,10 +2047,9 @@ pub(super) fn run_formatting_context(} else {hit.entry.outputs_without_fragments()};- return absorb_run_outputs(+ return child_run_outcome_from_outputs(&callbacks,parent_fragments,- parent_used,box_,&input,root_cells,@@ -2158,25 +2122,14 @@ pub(super) fn run_formatting_context();}cache_attempt.conclude(&callbacks, box_, cache_key, &input, &outputs);- absorb_run_outputs(- &callbacks,- parent_fragments,- parent_used,- box_,- &input,- root_cells,- outputs,- false,- )+ child_run_outcome_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false)}/// Takes a committing run of `box_` from the cache hit `hit` with nothing replayed: the box 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/// subtree is the one the stored run produced, the box is plain, and nothing from the stored run reaches past its root.-#[expect(clippy::too_many_arguments)]fn reuse_committed_subtree_as_it_is(parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,- root_used: Option<&UsedValues>,box_: Node,input: &LayoutInput,root_cells: used_values::RootInputCells,@@ -2215,13 +2168,6 @@ fn reuse_committed_subtree_as_it_is(if !geometry.shows_box_of_fragment(&fragment) {return None;}- if let Some(root_used) = root_used {- entry- .outputs- .root_outcome- .clone()- .apply_to_record(root_used, content_block_size_adopted_after_run);- }let _trace = callbacks.arena().layout_trace@@ -2879,8 +2825,8 @@ pub(crate) fn layout_inside_child()}-/// Lays out `child` with the input cells its parent sized it to, which its record takes until the parent no longer-/// keeps one for the child.+/// Lays out `child` with the input cells its parent sized it to: hands them to the child's run, or gives them to the+/// child's record where the parent lays the child out itself.pub(crate) fn layout_inside_child_with_input_cells(run: &FormattingContextRun,parent_block: Option<&block_formatting_context::BlockFormattingContext>,@@ -2971,9 +2917,12 @@ pub(crate) fn layout_inside_child_with_input_cells(}return ChildLayout::without_fragments(ChildLayoutOutcome::ReenterCurrent);};+ assert!(+ record_kept_by_parent.is_none(),+ "{child:?} is handed to a run of its own by a parent that lays it out itself"+ );let (result, fragment_arrival) = run_formatting_context(run.fragments.as_deref(),- record_kept_by_parent,child_input_cells,child,root_containing_block,@@ -3311,11 +3260,9 @@ pub(super) fn run_reads_content_block_size_adopted_after_it(&& fc_type.registers_abspos_children_against_its_final_block_size()}-#[expect(clippy::too_many_arguments)]-fn absorb_run_outputs(+fn child_run_outcome_from_outputs(callbacks: &LayoutPass<'_>,parent_fragments: Option<&fragment_tree::RunFragmentBuilder>,- parent_used: Option<&UsedValues>,child: Node,input: &LayoutInput,root_cells: used_values::RootInputCells,@@ -3333,10 +3280,7 @@ fn absorb_run_outputs(let content_block_size_adopted_after_run =content_block_size_adopted_after_run(input, result.automatic_content_block_size);let laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);- let line_data = root_outcome.line_data.clone();- if let Some(parent_used) = parent_used {- root_outcome.apply_to_record(parent_used, content_block_size_adopted_after_run);- }+ let line_data = root_outcome.line_data;let fragment_arrival = parent_fragments.and(root).map(|root| {debug_assert!(root.node == child, "a child run returned a root for a different box");fragment_tree::ChildFragmentArrival::RunRoot {@@ -3617,7 +3561,6 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layolet fc_type = independent_formatting_context_type(root_for_layout, &callbacks);let (_, fragment_arrival) = run_formatting_context(Some(&entry_fragments),- None,root_cells,root_for_layout,callbacks.in_flow_containing_block(root_for_layout),@@ -3772,7 +3715,6 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {.expect("partial relayout root must establish an independent formatting context");let (_, fragment_arrival) = run_formatting_context(Some(fragments),- None,root_cells,root,callbacks.in_flow_containing_block(root),diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 29e164d7347..5f64c9ee2f7 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -468,11 +468,6 @@ pub(crate) struct FragmentGeometry {}impl FragmentGeometry {- /// Whether `other` has the same cells and the same lines, as the same allocation.- pub(crate) fn is_identical_to(&self, other: &Self) -> bool {- self.cells == other.cells && same_allocation(self.line_data.as_ref(), other.line_data.as_ref())- }-pub(crate) fn shows_box_of_fragment(&self, fragment: &Fragment) -> bool {let cells = &self.cells;same_allocation(self.line_data.as_ref(), fragment.line_data.as_ref())diff --git a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rsindex 315b660126d..d5c397cb18d 100644--- a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs@@ -48,7 +48,6 @@ pub(super) fn layout_replaced_with_children(let (wrapper_result, fragment_arrival) = formatting_context::run_formatting_context(run.fragments.as_deref(),- None,wrapper_cells,wrapper,run.callbacks.in_flow_containing_block(wrapper),diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex bca9268b589..e5147c0612e 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -133,23 +133,14 @@ struct KeptBox {}enum KeptBoxHolding {- /// The box's record on the record stack, and what the run of the box handed back once it- /// returned, for a child laid out by a run of its own.- Record {- record: u32,- handed_back_by_its_run: Option<ChildLaidOutByItsOwnRun>,- },+ /// A box this run lays out itself, with its record on the record stack.+ LaidOutByThisRun { record: u32 },/// A child this run hands to a run of its own, with the cells it starts out with, until that run returns.ChildAwaitingItsRun {cells_before_its_run: used_values::RootInputCells,},/// A child laid out by a run of its own, as that run handed it back.- ChildLaidOutByItsOwnRun(ChildLaidOutByItsOwnRun),-}--/// Whether a run checks the boxes it keeps for its children against their records, which it keeps as well for now.-fn checks_kept_boxes_against_records() -> bool {- cfg!(debug_assertions) || super::run_repeatability::verification_is_enabled()+ ChildHandedBack(ChildLaidOutByItsOwnRun),}impl Default for LayoutScratch {@@ -663,13 +654,7 @@ impl<'arena> RunRecords<'arena> {pub(crate) fn register(&self, node: Node, used: UsedValues) -> &UsedValues {self.assert_registers_in_innermost_run(node);let (index, record) = self.scratch.run_record_stack.push(used);- self.keep(- node,- KeptBoxHolding::Record {- record: index,- handed_back_by_its_run: None,- },- );+ self.keep(node, KeptBoxHolding::LaidOutByThisRun { record: index });// SAFETY: The record stays on the stack until this run returns, which ends this borrow of the run.unsafe { record.as_ref() }}@@ -705,7 +690,7 @@ impl<'arena> RunRecords<'arena> {};!matches!(self.scratch.kept_boxes.borrow()[index as usize].holding,- KeptBoxHolding::Record { .. }+ KeptBoxHolding::LaidOutByThisRun { .. })}@@ -724,15 +709,12 @@ impl<'arena> RunRecords<'arena> {let (record_index, record) = self.scratch.run_record_stack.push(cells.materialize_record());let holding = &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding;assert!(- !matches!(holding, KeptBoxHolding::Record { .. }),+ !matches!(holding, KeptBoxHolding::LaidOutByThisRun { .. }),"slot {} already has a record in the run rooted at slot {}",child.slot_index(),self.root.slot_index());- *holding = KeptBoxHolding::Record {- record: record_index,- handed_back_by_its_run: None,- };+ *holding = KeptBoxHolding::LaidOutByThisRun { record: record_index };// SAFETY: The record stays on the stack until this run returns, which ends this borrow of the run.unsafe { record.as_ref() }}@@ -800,10 +782,10 @@ impl<'arena> RunRecords<'arena> {HeldBox::RootWithRecord => self.root_used.as_ref(),HeldBox::Kept(index) => match self.scratch.kept_boxes.borrow()[index as usize].holding {// SAFETY: This run registered the record, and it stays on the stack until this run returns.- KeptBoxHolding::Record { record, .. } => {+ KeptBoxHolding::LaidOutByThisRun { record } => {Some(unsafe { self.scratch.run_record_stack.get(record).as_ref() })}- KeptBoxHolding::ChildAwaitingItsRun { .. } | KeptBoxHolding::ChildLaidOutByItsOwnRun(_) => None,+ KeptBoxHolding::ChildAwaitingItsRun { .. } | KeptBoxHolding::ChildHandedBack(_) => None,},}}@@ -1068,21 +1050,18 @@ impl<'arena> RunRecords<'arena> {}}- /// Keeps what the run of `child`, a box this run holds the record of, handed back once it returned.+ /// Keeps what the run of `child`, a box this run keeps for that run, handed back once it returned.#[track_caller]pub(crate) fn receive_laid_out_child_box(&self, child: Node, handed_back: ChildLaidOutByItsOwnRun) {let Some(HeldBox::Kept(index)) = self.held_box(child) else {self.panic_for_box_not_held(child)};let holding = &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding;- match holding {- KeptBoxHolding::Record {- handed_back_by_its_run, ..- } => *handed_back_by_its_run = Some(handed_back),- KeptBoxHolding::ChildAwaitingItsRun { .. } | KeptBoxHolding::ChildLaidOutByItsOwnRun(_) => {- *holding = KeptBoxHolding::ChildLaidOutByItsOwnRun(handed_back);- }- }+ assert!(+ !matches!(holding, KeptBoxHolding::LaidOutByThisRun { .. }),+ "{child:?} is handed back by a run of its own to a parent that lays it out itself"+ );+ *holding = KeptBoxHolding::ChildHandedBack(handed_back);}/// The sizes, size constraints, box sides and baselines of `node`, a box this run lays out or a child of one laid@@ -1102,34 +1081,19 @@ impl<'arena> RunRecords<'arena> {HeldBox::RootWithRecord => return self.root_used.as_ref().map(BoxMetrics::LaidOutByThisRun),HeldBox::Kept(index) => index,};- let (record, laid_out) = match &self.scratch.kept_boxes.borrow()[index as usize].holding {- KeptBoxHolding::Record {- record,- handed_back_by_its_run,- } => (- *record,- handed_back_by_its_run.as_ref().map(|handed_back| handed_back.laid_out),- ),+ let record = match &self.scratch.kept_boxes.borrow()[index as usize].holding {+ KeptBoxHolding::LaidOutByThisRun { record } => *record,KeptBoxHolding::ChildAwaitingItsRun { cells_before_its_run } => {return Some(BoxMetrics::BeingSizedByItsParent(*cells_before_its_run));}- KeptBoxHolding::ChildLaidOutByItsOwnRun(handed_back) => {+ KeptBoxHolding::ChildHandedBack(handed_back) => {return Some(BoxMetrics::LaidOutByItsOwnRun(handed_back.laid_out));}};// SAFETY: This run registered the record, and it stays on the stack until this run returns.- let record = unsafe { self.scratch.run_record_stack.get(record).as_ref() };- let Some(laid_out) = laid_out else {- return Some(BoxMetrics::LaidOutByThisRun(record));- };- if checks_kept_boxes_against_records() {- assert_eq!(- laid_out.cells,- used_values::RootInputCells::capture(record),- "the parent's record of {node:?} changed after its run handed the box back"- );- }- Some(BoxMetrics::LaidOutByItsOwnRun(laid_out))+ Some(BoxMetrics::LaidOutByThisRun(unsafe {+ self.scratch.run_record_stack.get(record).as_ref()+ }))}/// Moves the sizes, size constraints and box sides this run holds for `node`, if it holds the box, from frame `from`@@ -1145,35 +1109,28 @@ impl<'arena> RunRecords<'arena> {}Some(HeldBox::Kept(index)) => index,};- let mut kept_boxes = self.scratch.kept_boxes.borrow_mut();- let handed_back_by_its_run = match &mut kept_boxes[index as usize].holding {- KeptBoxHolding::Record {- record,- handed_back_by_its_run,- } => {+ match &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding {+ KeptBoxHolding::LaidOutByThisRun { record } => {// SAFETY: This run registered the record, and it stays on the stack until this run returns.unsafe { self.scratch.run_record_stack.get(*record).as_ref() }.move_box_metrics_to_frame(from, to);- handed_back_by_its_run.as_mut()}KeptBoxHolding::ChildAwaitingItsRun { .. } => {debug_assert!(false,"a child's box moves to another frame only once its run handed it back");- None}- KeptBoxHolding::ChildLaidOutByItsOwnRun(handed_back) => Some(handed_back),- };- if let Some(ChildLaidOutByItsOwnRun { laid_out, frame, .. }) = handed_back_by_its_run {- debug_assert_eq!(*frame, from, "a kept box moves out of the frame it is in");- // NB: The baselines stay those of the parent's frame, which every reader of them is in.- laid_out.cells = used_values::UsedValuesCellState {- inputs: laid_out.cells,- baselines: laid_out.baselines,+ KeptBoxHolding::ChildHandedBack(ChildLaidOutByItsOwnRun { laid_out, frame, .. }) => {+ debug_assert_eq!(*frame, from, "a kept box moves out of the frame it is in");+ // NB: The baselines stay those of the parent's frame, which every reader of them is in.+ laid_out.cells = used_values::UsedValuesCellState {+ inputs: laid_out.cells,+ baselines: laid_out.baselines,+ }+ .in_frame(from, to)+ .inputs;+ *frame = to;}- .in_frame(from, to)- .inputs;- *frame = to;}}@@ -1188,11 +1145,11 @@ impl<'arena> RunRecords<'arena> {};match &self.scratch.kept_boxes.borrow()[index as usize].holding {KeptBoxHolding::ChildAwaitingItsRun { .. } => None,- KeptBoxHolding::ChildLaidOutByItsOwnRun(handed_back) => handed_back+ KeptBoxHolding::ChildHandedBack(handed_back) => handed_back.line_data.as_ref().and_then(|content| content.lines.last().copied()),- KeptBoxHolding::Record { .. } => unreachable!("a box with a record has its lines in the record"),+ KeptBoxHolding::LaidOutByThisRun { .. } => unreachable!("a box with a record has its lines in the record"),}}@@ -1215,40 +1172,26 @@ impl<'arena> RunRecords<'arena> {Some(HeldBox::Kept(index)) => index,None => self.panic_for_box_not_held(node),};- let (record, handed_back) = match &self.scratch.kept_boxes.borrow()[index as usize].holding {- KeptBoxHolding::Record {- record,- handed_back_by_its_run,- } => (Some(*record), handed_back_by_its_run.clone()),+ let handed_back = match &self.scratch.kept_boxes.borrow()[index as usize].holding {+ KeptBoxHolding::LaidOutByThisRun { record } => {+ // SAFETY: This run registered the record, and it stays on the stack until this run returns.+ return geometry_of_record(unsafe { self.scratch.run_record_stack.get(*record).as_ref() });+ }KeptBoxHolding::ChildAwaitingItsRun { .. } => {panic!("the fragment of {node:?} is built before its run handed the box back")}- KeptBoxHolding::ChildLaidOutByItsOwnRun(handed_back) => (None, Some(handed_back.clone())),- };- // SAFETY: This run registered the record, and it stays on the stack until this run returns.- let record = record.map(|record| unsafe { self.scratch.run_record_stack.get(record).as_ref() });- let Some(handed_back) = handed_back else {- return geometry_of_record(record.expect("a kept box without a handed back box has a record"));+ KeptBoxHolding::ChildHandedBack(handed_back) => handed_back.clone(),};- if checks_kept_boxes_against_records() {+ if cfg!(debug_assertions) || super::run_repeatability::verification_is_enabled() {assert_eq!(handed_back.frame, callbacks.frame,"the fragment of {node:?} is built in another frame than the one its box is kept in");}- let geometry = fragment_tree::FragmentGeometry {+ fragment_tree::FragmentGeometry {cells: handed_back.laid_out.cells,line_data: handed_back.line_data,- };- if let Some(record) = record- && checks_kept_boxes_against_records()- {- assert!(- geometry.is_identical_to(&geometry_of_record(record)),- "the parent's record of {node:?} changed after its run handed the box back"- );}- geometry}pub(crate) fn store_table_inline_layout(&self, table: Node, layout: table_formatting_context::TableInlineLayout) {diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rsindex 62145b6b8b5..82f4a641b86 100644--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs@@ -198,10 +198,6 @@ impl ComparedLayouts<'_> {reference_outputs.root_outcome.input_cells_left_by_run,"{checked} left a root record {reference} does not");- assert_eq!(- checked_outputs.root_outcome.own_metrics_sealed, reference_outputs.root_outcome.own_metrics_sealed,- "{checked} seals the root's metrics unlike {reference}"- );assert!(checked_outputs.root_outcome.line_data.as_deref() == reference_outputs.root_outcome.line_data.as_deref(),"{checked} left line data {reference} does not"diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex 54ec6c97369..f4363b6581e 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -104,10 +104,6 @@ impl<T: Copy> SealableCell<T> {pub(crate) fn set_ignoring_seal(&self, value: T) {self.value.set(value);}-- pub(crate) fn is_sealed(&self) -> bool {- self.sealed.get()- }}#[derive(Clone, Default, PartialEq)]@@ -371,10 +367,6 @@ impl UsedValues {formatting_context::store_derived_baselines(self, state.baselines);}- pub(crate) fn own_metrics_are_sealed(&self) -> bool {- self.content_inline_size.is_sealed()- }-pub(crate) fn seal_own_metrics(&self) {self.content_inline_size.seal();self.content_block_size.seal();