Skip to content

Truly independent web browser

LibWeb: Mark a discarded layout run in its view of the pass

Aliaksandr Kalenikff5342b9de51
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 052e0ed83d4..f6021d3e30f 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3138,7 +3138,7 @@ impl<'pass> BlockFormattingContext<'pass> {
) -> bool {
let facts = self.facts(node);
let container_facts = self.facts(block_container);
- !self.run.callbacks.scratch().reads_no_memoized_layout()
+ !self.run.callbacks.reads_no_memoized_layout()
&& self.run.fragmentainer.is_none()
&& self.run.fragments.is_some()
&& !self.run.should_collect_devtools_layout_data
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 974c02f544e..5fc84a765d9 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -2012,7 +2012,7 @@ pub(super) fn run_formatting_context(
parent_fragments.is_none() || builds_fragments,
"the run of {box_:?} builds no fragments for its parent to commit"
);
- if callbacks.scratch().reads_no_memoized_layout() {
+ if callbacks.reads_no_memoized_layout() {
let outputs = execute_formatting_context_run(
builds_fragments,
root_cells,
@@ -2082,7 +2082,7 @@ pub(super) fn run_formatting_context(
.layout_trace
.run(box_, fc_type, || cache_attempt.trace_action());
let previous_line_data = cache_attempt.previous_line_data();
- let discarded_outputs = run_repeatability::lay_out_discarded_run(&callbacks, || {
+ let discarded_outputs = run_repeatability::lay_out_discarded_run(&callbacks, |callbacks| {
execute_formatting_context_run(
builds_fragments,
root_cells,
@@ -2122,7 +2122,7 @@ pub(super) fn run_formatting_context(
root_cells,
input,
&outputs,
- |other_root_cells, other_input| {
+ |callbacks, other_root_cells, other_input| {
execute_formatting_context_run(
builds_fragments,
other_root_cells,
@@ -3175,7 +3175,7 @@ pub(crate) fn lay_out_shape_for_its_result(callbacks: LayoutPass<'_>, shape: Chi
root_containing_block,
} = shape;
let builds_fragments = run_builds_fragments(&input, false);
- let lay_out = |root_cells, input, previous_line_data| {
+ let lay_out = |callbacks: LayoutPass<'_>, root_cells, input, previous_line_data| {
execute_formatting_context_run(
builds_fragments,
root_cells,
@@ -3195,8 +3195,8 @@ pub(crate) fn lay_out_shape_for_its_result(callbacks: LayoutPass<'_>, shape: Chi
callbacks.receive_host_findings(outputs.host_findings.clone());
outputs.result
};
- if callbacks.scratch().reads_no_memoized_layout() {
- return result_with_host_findings(&lay_out(root_cells, input, None));
+ if callbacks.reads_no_memoized_layout() {
+ return result_with_host_findings(&lay_out(callbacks, root_cells, input, None));
}
let runs_ahead_of_intrinsic_block_padding =
builds_fragments && input.sizing.table_cell_intrinsic_block_padding.is_some();
@@ -3228,8 +3228,9 @@ pub(crate) fn lay_out_shape_for_its_result(callbacks: LayoutPass<'_>, shape: Chi
};
let _trace = callbacks.arena().layout_trace.run(node, fc_type, || action);
let previous_line_data = cache_attempt.previous_line_data();
- let discarded_outputs = run_repeatability::lay_out_discarded_run(&callbacks, || lay_out(root_cells, input, None));
- let outputs = lay_out(root_cells, input, previous_line_data);
+ let discarded_outputs =
+ run_repeatability::lay_out_discarded_run(&callbacks, |callbacks| lay_out(callbacks, root_cells, input, None));
+ let outputs = lay_out(callbacks, root_cells, input, previous_line_data);
if let Some(discarded_outputs) = &discarded_outputs {
run_repeatability::verify_repeated_run(&callbacks, node, discarded_outputs, &outputs);
}
@@ -3240,7 +3241,7 @@ pub(crate) fn lay_out_shape_for_its_result(callbacks: LayoutPass<'_>, shape: Chi
root_cells,
input,
&outputs,
- |other_root_cells, other_input| lay_out(other_root_cells, other_input, None),
+ |callbacks, other_root_cells, other_input| lay_out(callbacks, other_root_cells, other_input, None),
);
}
cache_attempt.conclude(&callbacks, node, cache_key, &input, &outputs);
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index b2d1aa33fdb..63b62078841 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -1160,7 +1160,7 @@ impl InlineLevelIterator {
atomic_sizing: AtomicInlineSizing,
) -> Self {
let callbacks = context.callbacks;
- match callbacks.scratch().inline_item_stash(context.containing_block) {
+ match callbacks.inline_item_stash(context.containing_block) {
Some(stash) => Self::from_stash(context, stash),
None => InlineLevelIteratorGenerator::generate(context, atomic_sizing),
}
@@ -1204,7 +1204,7 @@ impl InlineLevelIterator {
glyphs.glyphs = libgfx_rust::text_layout::GlyphBuffer::Shared(std::mem::take(owned).into());
}
}
- context.callbacks.scratch().store_inline_item_stash(
+ context.callbacks.store_inline_item_stash(
context.containing_block,
StashedInlineItems {
items,
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 81c2255e556..c36686764d7 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -337,14 +337,6 @@ pub(crate) struct IntrinsicSizeCaches {
}
impl IntrinsicSizeCaches {
- /// Runs `measure` with caches of its own, which start empty and are dropped when it returns.
- pub(crate) fn set_aside_while<R>(&self, measure: impl FnOnce() -> R) -> R {
- let set_aside = self.slots.take();
- let result = measure();
- self.slots.replace(set_aside);
- result
- }
-
/// Drops the entries of `slots`, which were freed or whose rows' cache epoch wrapped.
pub(crate) fn drop_slots(&self, slots: Vec<u32>) {
let mut caches = self.slots.borrow_mut();
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index b298522e0b4..5953d91154b 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -31,6 +31,12 @@ pub(crate) struct LayoutPass<'arena> {
innermost_run: (Node, Node),
/// What the run this pass view was handed to found out for the host so far, or the pass outside any run.
host_findings: &'arena RefCell<super::run_state::HostFindings>,
+ /// The intrinsic sizes the runs this pass view reaches read and measure into: those passes measured, kept for the
+ /// passes after them, or inside a run whose result is discarded, that run's own.
+ intrinsic_size_caches: &'arena super::layout_node_arena::IntrinsicSizeCaches,
+ /// Whether this pass view is inside a run whose result is discarded, which reads no memoized layout from before it
+ /// and keeps none: the run cache, the in-flow block cache, inline item stashes and intrinsic sizes.
+ reads_no_memoized_layout: bool,
}
impl<'arena> LayoutPass<'arena> {
@@ -48,6 +54,8 @@ impl<'arena> LayoutPass<'arena> {
frame: super::geometry::Frame::PHYSICAL,
innermost_run: (NodeSlotId::INVALID, NodeSlotId::INVALID),
host_findings: &scratch.host_findings_outside_runs,
+ intrinsic_size_caches: &scratch.intrinsic_size_caches,
+ reads_no_memoized_layout: false,
}
}
@@ -101,6 +109,50 @@ impl<'arena> LayoutPass<'arena> {
self.scratch
}
+ /// Lays out `run`, whose result is discarded, with this pass as that run sees it: it reads no memoized layout from
+ /// before it and keeps none, so that it computes everything afresh and leaves nothing for the runs after it. It
+ /// measures intrinsic sizes into caches of its own, which start empty and are dropped when it returns.
+ pub(crate) fn lay_out_discarded_run<R>(self, run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> R) -> R {
+ let intrinsic_size_caches = super::layout_node_arena::IntrinsicSizeCaches::default();
+ let pass: LayoutPass<'_> = self;
+ run(LayoutPass {
+ intrinsic_size_caches: &intrinsic_size_caches,
+ reads_no_memoized_layout: true,
+ ..pass
+ })
+ }
+
+ /// Whether this pass view is inside a run whose result is discarded, which reads no memoized layout from before it
+ /// and keeps none.
+ pub(crate) fn reads_no_memoized_layout(&self) -> bool {
+ self.reads_no_memoized_layout
+ }
+
+ /// The inline items a block container generated earlier in the pass, which a run whose result is discarded does not
+ /// read.
+ pub(crate) fn inline_item_stash(
+ &self,
+ block_container: Node,
+ ) -> Option<super::inline_level_iterator::StashedInlineItems> {
+ if self.reads_no_memoized_layout {
+ return None;
+ }
+ self.scratch.inline_item_stash(block_container)
+ }
+
+ /// Keeps the inline items `block_container` generated for its later runs in the pass, unless this pass view is
+ /// inside a run whose result is discarded.
+ pub(crate) fn store_inline_item_stash(
+ &self,
+ block_container: Node,
+ stash: super::inline_level_iterator::StashedInlineItems,
+ ) {
+ if self.reads_no_memoized_layout {
+ return;
+ }
+ self.scratch.store_inline_item_stash(block_container, stash);
+ }
+
/// Notes what the run this pass view was handed to found out for the host, or the pass outside any run.
pub(crate) fn note_host_findings(&self, note: impl FnOnce(&mut super::run_state::HostFindings)) {
note(&mut self.host_findings.borrow_mut());
@@ -113,9 +165,10 @@ impl<'arena> LayoutPass<'arena> {
}
}
- /// The intrinsic sizes passes measured, kept for the passes after them.
+ /// The intrinsic sizes passes measured, kept for the passes after them, or inside a run whose result is discarded,
+ /// that run's own.
pub(crate) fn intrinsic_size_caches(&self) -> &'arena super::layout_node_arena::IntrinsicSizeCaches {
- &self.scratch.intrinsic_size_caches
+ self.intrinsic_size_caches
}
/// What an intrinsic size cache entry measured for `node` now is valid for.
diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
index 01c6a90de3f..129567e88df 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
@@ -38,18 +38,16 @@ pub(super) fn verification_is_enabled() -> bool {
/// progress around it already. Nothing it does is traced.
pub(super) fn lay_out_discarded_run(
callbacks: &LayoutPass<'_>,
- run: impl FnOnce() -> formatting_context::RunOutputs,
+ run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> formatting_context::RunOutputs,
) -> Option<formatting_context::RunOutputs> {
- if run_repeatability_verification() == RunRepeatabilityVerification::Off
- || callbacks.scratch().reads_no_memoized_layout()
- {
+ if run_repeatability_verification() == RunRepeatabilityVerification::Off || callbacks.reads_no_memoized_layout() {
return None;
}
Some(
callbacks
.arena()
.layout_trace
- .without_events(|| callbacks.scratch().lay_out_discarded_run(run)),
+ .without_events(|| callbacks.lay_out_discarded_run(run)),
)
}
@@ -131,9 +129,13 @@ pub(super) fn verify_run_ignores_its_unobserved_own_block_size_and_block_margins
root_cells: used_values::RootInputCells,
input: LayoutInput,
outputs: &formatting_context::RunOutputs,
- lay_out: impl Fn(used_values::RootInputCells, LayoutInput) -> formatting_context::RunOutputs,
+ lay_out: impl for<'discarded> Fn(
+ LayoutPass<'discarded>,
+ used_values::RootInputCells,
+ LayoutInput,
+ ) -> formatting_context::RunOutputs,
) {
- if !unobserved_own_block_size_verification_is_enabled() || callbacks.scratch().reads_no_memoized_layout() {
+ if !unobserved_own_block_size_verification_is_enabled() || callbacks.reads_no_memoized_layout() {
return;
}
let checked = format!("the run of {box_:?}, which never observed its own block size or block margins,");
@@ -163,9 +165,7 @@ pub(super) fn verify_run_ignores_its_unobserved_own_block_size_and_block_margins
}
for (reference_cells, reference_input, reference) in reference_inputs {
let reference_outputs = callbacks.arena().layout_trace.without_events(|| {
- callbacks
- .scratch()
- .lay_out_discarded_run(|| lay_out(reference_cells, reference_input))
+ callbacks.lay_out_discarded_run(|callbacks| lay_out(callbacks, reference_cells, reference_input))
});
ComparedLayouts {
checked: &checked,
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index d0e5dd10cd6..6f2e94ada28 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -22,9 +22,6 @@ pub(crate) struct LayoutScratch {
has_position_option_styles: Cell<bool>,
/// What the pass found out for the host outside any run, which its driver reports itself.
pub(super) host_findings_outside_runs: RefCell<HostFindings>,
- /// How many runs whose results are discarded are in progress, which read no memoized layout from before them and
- /// keep none.
- discarded_runs_in_progress: Cell<u32>,
}
/// What layout found out that the host hears of once the layout that found it is committed: what the
@@ -98,7 +95,6 @@ impl Default for LayoutScratch {
position_option_styles: RefCell::new(Vec::new()),
has_position_option_styles: Cell::new(false),
host_findings_outside_runs: RefCell::new(HostFindings::default()),
- discarded_runs_in_progress: Cell::new(0),
}
}
}
@@ -131,43 +127,21 @@ impl LayoutScratch {
.map(|(_, style)| *style)
}
- pub(crate) fn store_inline_item_stash(
+ pub(super) fn store_inline_item_stash(
&self,
block_container: NodeSlotId,
stash: super::inline_level_iterator::StashedInlineItems,
) {
- if self.reads_no_memoized_layout() {
- return;
- }
self.inline_item_stashes.borrow_mut().insert(block_container, stash);
}
- pub(crate) fn inline_item_stash(
+ pub(super) fn inline_item_stash(
&self,
block_container: NodeSlotId,
) -> Option<super::inline_level_iterator::StashedInlineItems> {
- if self.reads_no_memoized_layout() {
- return None;
- }
self.inline_item_stashes.borrow().get(&block_container).cloned()
}
- /// Lays out `run`, whose result is discarded, without reading memoized layout from before it or keeping any, so
- /// that it computes everything afresh and leaves nothing for the runs after it.
- pub(crate) fn lay_out_discarded_run<R>(&self, run: impl FnOnce() -> R) -> R {
- let in_progress = &self.discarded_runs_in_progress;
- in_progress.set(in_progress.get() + 1);
- let result = self.intrinsic_size_caches.set_aside_while(run);
- in_progress.set(in_progress.get() - 1);
- result
- }
-
- /// Whether a run whose result is discarded is in progress, which reads no memoized layout from before it and keeps
- /// none: the run cache, the in-flow block cache, inline item stashes and intrinsic sizes.
- pub(crate) fn reads_no_memoized_layout(&self) -> bool {
- self.discarded_runs_in_progress.get() > 0
- }
-
/// What the pass found out outside any run, which its driver reports itself.
pub(crate) fn take_host_findings_outside_runs(&self) -> HostFindings {
std::mem::take(&mut self.host_findings_outside_runs.borrow_mut())