Skip to content

Truly independent web browser

LibWeb: Re-wrap a stored run's root cells in the cache on hits

Aliaksandr Kalenik9abe009a11d0
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 657335af060..fe87c24a268 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -6233,7 +6233,7 @@ impl<'pass> BlockFormattingContext<'pass> {
outcome,
consumed_block_size: outputs.result.automatic_content_block_size,
baselines: outputs.result.baselines,
- root_line_data: outputs.root_outcome.line_data,
+ root_line_data: outputs.line_data,
contents: fragmentainer.take_contents(),
};
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 5ebd56d7469..46ed0a4a234 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -42,6 +42,35 @@ pub(crate) struct RunObservations {
/// so that it stands for runs at any of them, such as the intrinsic block padding its table hands it once its rows
/// are sized.
pub(super) table_cell_block_paddings_unobserved: bool,
+ /// Whether the root's parent owns the root's input cells, which the run then leaves as they were handed to it but
+ /// for the content block size the root adopts.
+ pub(super) parent_owns_root_cells: bool,
+}
+
+/// The cells of the root of the run that produced `outputs`, as the root's parent keeps them where `probe`, which the
+/// outputs answer, describes the run: the probe's own cells where the parent owns them, and else those the run left,
+/// either with the content block size the root adopts at the probe and the inline margins the probe resolved where the
+/// run never observed them.
+pub(crate) fn cells_at_probe(
+ outputs: &formatting_context::RunOutputs,
+ probe: &FcRunCacheKey,
+) -> used_values::RootInputCells {
+ let mut cells = if outputs.observed.parent_owns_root_cells {
+ probe.root_cells
+ } else {
+ outputs.cells
+ };
+ if let Some(block_size) = formatting_context::content_block_size_adopted_after_run(
+ &probe.input,
+ outputs.result.automatic_content_block_size,
+ ) {
+ cells.set_content_block_size(block_size);
+ }
+ if outputs.observed.never_observed.inline_space_around_root {
+ cells.margin_left = probe.root_cells.margin_left;
+ cells.margin_right = probe.root_cells.margin_right;
+ }
+ cells
}
/// What a run did besides its result that decides which of its inputs it observed.
@@ -92,6 +121,7 @@ pub(crate) fn observations_of(
};
RunObservations {
never_observed,
+ parent_owns_root_cells: facts.parent_owns_root_input_cells,
table_cell_block_paddings_unobserved: facts.parent_owns_root_input_cells
&& NodeFacts::new(&run.pass, run.box_).is_table_cell()
&& !run_observes_root_block_padding(
@@ -453,63 +483,29 @@ impl FcRunCacheEntry {
if !root.payloads_past_run().pending_abspos.is_empty() || !arena.paintable_row_is_populated(box_) {
return false;
}
- let (content_inline_size, content_block_size) = self.root_content_size_as_its_parent_holds_it();
+ let cells = self.outputs.cells;
arena
.with_current_committed_fragment(box_, |committed| {
if std::sync::Arc::ptr_eq(committed, &root.fragment) {
return true;
}
let committed_content_size = self.key.parent_frame.size_from_physical(committed.sides.content_size);
- committed_content_size.width == content_inline_size
- && (committed_content_size.height == content_block_size
+ committed_content_size.width == cells.content_inline_size
+ && (committed_content_size.height == cells.content_block_size
|| self.outputs.observed.never_observed.own_block_size
== formatting_context::OwnBlockSizeObservation::Unobserved)
- && same_allocation(
- committed.line_data.as_ref(),
- self.outputs.root_outcome.line_data.as_ref(),
- )
+ && same_allocation(committed.line_data.as_ref(), self.outputs.line_data.as_ref())
&& committed.holds_same_contents_as(&root.fragment)
})
.unwrap_or(false)
}
- fn root_content_size_as_its_parent_holds_it(&self) -> (CssPixels, CssPixels) {
- match &self.outputs.root_outcome.input_cells_left_by_run {
- Some(cells) => (cells.content_inline_size, cells.content_block_size),
- None => (
- self.key.root_cells.content_inline_size,
- self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(
- &self.key.input,
- self.key.root_cells,
- ),
- ),
- }
- }
-
- /// The content block size a run with `input` and the parent-owned root input cells `root_cells` leaves its root
- /// with, where its contents are laid out as the stored run's were.
- fn content_block_size_run_leaves_root_whose_cells_its_parent_owns(
- &self,
- input: &LayoutInput,
- root_cells: used_values::RootInputCells,
- ) -> CssPixels {
- formatting_context::content_block_size_run_leaves_root_whose_cells_its_parent_owns(
- input,
- root_cells,
- self.outputs.result.automatic_content_block_size,
- )
- }
-
/// Whether the absolutely positioned boxes the stored run laid out against its root would be laid out against the
/// same root block size by the probed run, where they are all that observed the root's own block size.
fn probe_leaves_root_with_block_size_its_abspos_were_laid_out_against(&self, probe: &FcRunCacheKey) -> bool {
self.outputs.observed.never_observed.own_block_size
!= formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot
- || self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(&probe.input, probe.root_cells)
- == self.content_block_size_run_leaves_root_whose_cells_its_parent_owns(
- &self.key.input,
- self.key.root_cells,
- )
+ || cells_at_probe(&self.outputs, probe).content_block_size == self.outputs.cells.content_block_size
}
fn last_used_at_layout_commit(&self) -> u64 {
@@ -1069,7 +1065,7 @@ impl FcRunCacheAttempt {
else {
return None;
};
- entry.outputs.root_outcome.line_data.clone()
+ entry.outputs.line_data.clone()
}
/// Stores `outputs` where the probe asked for it, and hands them back to the run's parent: as the stored entry's, so
@@ -1081,6 +1077,13 @@ impl FcRunCacheAttempt {
key: &FcRunCacheKey,
outputs: formatting_context::RunOutputs,
) -> formatting_context::RunOutputsHandle {
+ if !matches!(self, Self::Bypass) && (cfg!(debug_assertions) || run_repeatability::verification_is_enabled()) {
+ assert_eq!(
+ cells_at_probe(&outputs, key),
+ outputs.cells,
+ "the stored run of {box_:?} would hand a probe of its own key other root cells than it left"
+ );
+ }
let store = || {
pass.fc_run_cache_store()
.expect("a run that probed a store concludes where it can store")
@@ -1243,10 +1246,8 @@ mod tests {
outputs: formatting_context::RunOutputs {
result: formatting_context::ChildLayoutResult::default(),
root,
- root_outcome: formatting_context::RunRootOutcome {
- input_cells_left_by_run: Some(used_values::RootInputCells::default()),
- line_data: None,
- },
+ cells: used_values::RootInputCells::default(),
+ line_data: None,
// NB: As a run whose result depends on no percentage block size proves.
observed: RunObservations {
never_observed: RunInputsTheStoredRunNeverObserved {
@@ -1254,6 +1255,7 @@ mod tests {
..RunInputsTheStoredRunNeverObserved::default()
},
table_cell_block_paddings_unobserved: false,
+ parent_owns_root_cells: false,
},
host_findings: None,
},
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index f19a8277616..5b2e63f0ac7 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1062,39 +1062,14 @@ pub(crate) enum OwnBlockSizeObservation {
ObservedOnlyByAbsposContainedByRoot,
}
-#[derive(Clone)]
-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) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
-}
-
-impl RunRootOutcome {
- /// What the parent keeps of the root once its run returned `result`, given the input cells it handed the run.
- fn laid_out_child_box(
- &self,
- root_cells: used_values::RootInputCells,
- result: &ChildLayoutResult,
- content_block_size_adopted_after_run: Option<CssPixels>,
- ) -> run_state::LaidOutChildBox {
- let mut cells = self.input_cells_left_by_run.unwrap_or(root_cells);
- if let Some(block_size) = content_block_size_adopted_after_run {
- cells.content_block_size = clamp_to_max_dimension_value(block_size.max(CssPixels::default()));
- }
- run_state::LaidOutChildBox {
- cells,
- baselines: result.baselines,
- orthogonal_flow_baselines: result.orthogonal_flow_baselines,
- line_data: self.line_data.clone(),
- }
- }
-}
-
pub(crate) struct RunOutputs {
pub(crate) result: ChildLayoutResult,
/// The fragment of the root, where the run builds fragments and is not a fragmentainer's.
pub(crate) root: Option<fragment_tree::RunRootFragment>,
- pub(crate) root_outcome: RunRootOutcome,
+ /// The root's cells as its parent keeps them once the run is done, in the parent's frame: where the parent owns
+ /// them, those it handed the run with the content block size the root adopted, and else those the run left.
+ pub(crate) cells: used_values::RootInputCells,
+ pub(crate) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
/// Which of its inputs the run observed, which the run cache reuses its outputs by.
pub(crate) observed: fc_run_cache::RunObservations,
/// Boxed, as most runs find out nothing for the host.
@@ -1118,28 +1093,39 @@ pub(super) enum RunOutputsHandle {
struct RunOutputParts {
result: ChildLayoutResult,
root: Option<fragment_tree::RunRootFragment>,
- root_outcome: RunRootOutcome,
- observed: fc_run_cache::RunObservations,
+ cells: used_values::RootInputCells,
+ line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
host_findings: Option<Box<super::run_state::HostFindings>>,
}
impl RunOutputsHandle {
- fn into_parts(self, takes_root: bool) -> RunOutputParts {
+ /// The parts of the outputs the parent of a run probed with `probe` takes in, with the root's fragment where
+ /// `takes_root`.
+ fn into_parts(self, takes_root: bool, probe: &fc_run_cache::FcRunCacheKey) -> RunOutputParts {
match self {
Self::Owned(outputs) => RunOutputParts {
result: outputs.result,
root: outputs.root.filter(|_| takes_root),
- root_outcome: outputs.root_outcome,
- observed: outputs.observed,
+ cells: outputs.cells,
+ line_data: outputs.line_data,
host_findings: outputs.host_findings,
},
- Self::Stored(entry) | Self::Reused(entry) => RunOutputParts {
- result: entry.outputs.result,
- root: entry.outputs.root.as_ref().filter(|_| takes_root).cloned(),
- root_outcome: entry.outputs.root_outcome.clone(),
- observed: entry.outputs.observed,
- host_findings: entry.outputs.host_findings.clone(),
- },
+ Self::Stored(entry) => Self::parts_of_stored(&entry.outputs, takes_root, entry.outputs.cells),
+ Self::Reused(entry) => Self::parts_of_stored(
+ &entry.outputs,
+ takes_root,
+ fc_run_cache::cells_at_probe(&entry.outputs, probe),
+ ),
+ }
+ }
+
+ fn parts_of_stored(outputs: &RunOutputs, takes_root: bool, cells: used_values::RootInputCells) -> RunOutputParts {
+ RunOutputParts {
+ result: outputs.result,
+ root: outputs.root.as_ref().filter(|_| takes_root).cloned(),
+ cells,
+ line_data: outputs.line_data.clone(),
+ host_findings: outputs.host_findings.clone(),
}
}
}
@@ -1437,28 +1423,6 @@ impl<'pass> FormattingContextRun<'pass> {
fragments.update_fragment_payload_of(node, |pending| update(pending.svg_mut()));
}
}
-
- pub(crate) fn outputs(
- &self,
- result: ChildLayoutResult,
- root: Option<fragment_tree::RunRootFragment>,
- root_cells_left_by_run: used_values::RootInputCells,
- root_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
- observed: fc_run_cache::RunObservations,
- parent_owns_root_input_cells: bool,
- ) -> RunOutputs {
- let root_outcome = RunRootOutcome {
- input_cells_left_by_run: (!parent_owns_root_input_cells).then_some(root_cells_left_by_run),
- line_data: root_line_data,
- };
- RunOutputs {
- result,
- root,
- root_outcome,
- observed,
- host_findings: self.state.take_host_findings(),
- }
- }
}
pub(crate) fn formatting_context_type_created_by_node_data(
@@ -2385,17 +2349,6 @@ fn layout_after_probe(
cache_attempt.conclude(&pass, box_, key, outputs)
}
-fn keep_inline_margins_resolved_now_where_unobserved(
- laid_out_box: &mut run_state::LaidOutChildBox,
- root_cells: used_values::RootInputCells,
- inputs_never_observed: fc_run_cache::RunInputsTheStoredRunNeverObserved,
-) {
- if inputs_never_observed.inline_space_around_root {
- laid_out_box.cells.margin_left = root_cells.margin_left;
- laid_out_box.cells.margin_right = root_cells.margin_right;
- }
-}
-
pub(crate) fn explicit_intrinsic_inner_inline_size(facts: &NodeFacts<'_>) -> CssPixels {
let style = facts.style();
if style.contain_intrinsic_width_has_length() {
@@ -2792,14 +2745,26 @@ fn lay_out_run_root_and_contents<'pass>(
parent_owns_root_input_cells,
},
);
- let outputs = run.outputs(
+ let cells = if parent_owns_root_input_cells {
+ used_values::RootInputCells {
+ content_block_size: content_block_size_run_leaves_root_whose_cells_its_parent_owns(
+ input,
+ root_cells,
+ result.automatic_content_block_size,
+ ),
+ ..root_cells
+ }
+ } else {
+ root_cells_left_by_run
+ };
+ let outputs = RunOutputs {
result,
root,
- root_cells_left_by_run,
- root_line_data,
+ cells,
+ line_data: root_line_data,
observed,
- parent_owns_root_input_cells,
- );
+ host_findings: state.take_host_findings(),
+ };
(outputs, root_cells_left_by_run)
}
@@ -3510,7 +3475,10 @@ pub(crate) fn lay_out_measurement_root_for_its_result(
pub(crate) fn lay_out_shape_for_its_result(pass: LayoutPass<'_>, shape: ChildRunShape) -> ChildLayoutResult {
let RunOutputParts {
result, host_findings, ..
- } = layout(pass, &RunRequest::of_shape(shape), RunSideInputs::default()).into_parts(false);
+ } = {
+ let request = RunRequest::of_shape(shape);
+ layout(pass, &request, RunSideInputs::default()).into_parts(false, &request.key)
+ };
pass.receive_host_findings(host_findings);
result
}
@@ -3552,23 +3520,21 @@ pub(super) fn content_block_size_run_leaves_root_whose_cells_its_parent_owns(
fn child_layout_from_outputs(pass: &LayoutPass<'_>, request: &RunRequest, outputs: RunOutputsHandle) -> ChildLayout {
let child = request.node;
- let input = &request.key.input;
- let root_cells = request.key.root_cells;
let outputs_are_stored_ones = matches!(outputs, RunOutputsHandle::Reused(_));
let RunOutputParts {
result,
root,
- root_outcome,
- observed,
+ cells,
+ line_data,
host_findings,
- } = outputs.into_parts(request.caller.collects_fragments());
+ } = outputs.into_parts(request.caller.collects_fragments(), &request.key);
pass.receive_host_findings(host_findings);
- let content_block_size_adopted_after_run =
- content_block_size_adopted_after_run(input, result.automatic_content_block_size);
- let mut laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);
- if outputs_are_stored_ones {
- keep_inline_margins_resolved_now_where_unobserved(&mut laid_out_box, root_cells, observed.never_observed);
- }
+ let laid_out_box = run_state::LaidOutChildBox {
+ cells,
+ baselines: result.baselines,
+ orthogonal_flow_baselines: result.orthogonal_flow_baselines,
+ line_data,
+ };
let fragment_arrival = root.map(|root| {
debug_assert!(
root.fragment.node == child,
diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
index d8b151ce87c..d5c2d9f5b98 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
@@ -241,13 +241,17 @@ impl ComparedLayouts<'_> {
checked_outputs.result, reference_outputs.result,
"{checked} returned a result {reference} does not"
);
+ // NB: Where the parent owns the root's cells, they are those of the layout's own input.
+ let cells_left_by_run = |outputs: &formatting_context::RunOutputs| {
+ (!outputs.observed.parent_owns_root_cells).then_some(outputs.cells)
+ };
assert_eq!(
- checked_outputs.root_outcome.input_cells_left_by_run,
- reference_outputs.root_outcome.input_cells_left_by_run,
+ cells_left_by_run(checked_outputs),
+ cells_left_by_run(reference_outputs),
"{checked} left root cells {reference} does not"
);
assert!(
- checked_outputs.root_outcome.line_data.as_deref() == reference_outputs.root_outcome.line_data.as_deref(),
+ checked_outputs.line_data.as_deref() == reference_outputs.line_data.as_deref(),
"{checked} left line data {reference} does not"
);
assert_eq!(