Truly independent web browser
LibWeb: Keep discarded verification runs away from layout memos
Aliaksandr Kalenik1b267e9f7214
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex 7b6bf23101f..6258f326ba0 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -4285,8 +4285,7 @@ impl<'pass> BlockFormattingContext<'pass> {) -> bool {let facts = self.facts(node);let container_facts = self.facts(block_container);- !self.run.pass.reads_no_memoized_layout()- && !self.run.should_collect_devtools_layout_data+ !self.run.should_collect_devtools_layout_data&& containing_line_box_fragment.is_none()&& !self.is_line_clamp_container&& self.max_lines.get().is_none()@@ -4333,12 +4332,11 @@ impl<'pass> BlockFormattingContext<'pass> {std::sync::Arc<fragment_tree::Fragment>,)> {let pass = &self.run.pass;+ let store = pass.in_flow_block_cache_store()?;pass.arena().with_current_committed_fragment(node, |fragment| fragment.clone()).and_then(|fragment| {- let entry =- pass.in_flow_block_cache_store()- .matching(node.slot_index(), validity, key, fragment.identity)?;+ let entry = store.matching(node.slot_index(), validity, key, fragment.identity)?;Some((entry, fragment))})}@@ -4355,7 +4353,10 @@ impl<'pass> BlockFormattingContext<'pass> {continues_from_previous_fragment: bool,) -> InFlowReuse {if let Some(fragmentainer) = self.run.fragmentainer.as_deref() {- if continues_from_previous_fragment+ // NB: A discarded run lays out every column afresh, so that the verification checks the column box layouts+ // the run reuses.+ if self.run.pass.is_discarded_run()+ || continues_from_previous_fragment|| !self.in_flow_child_reads_only_its_key_and_floats_in_any_run(node,block_container,@@ -4388,7 +4389,9 @@ impl<'pass> BlockFormattingContext<'pass> {fragment: layout.fragment.clone(),});}- if !self.in_flow_child_may_be_reused(node, block_container, containing_line_box_fragment) {+ if self.run.pass.in_flow_block_cache_store().is_none()+ || !self.in_flow_child_may_be_reused(node, block_container, containing_line_box_fragment)+ {return InFlowReuse::Ineligible;}let pass = &self.run.pass;@@ -4842,11 +4845,10 @@ impl<'pass> BlockFormattingContext<'pass> {);return;}- let committed_identity = arena.with_current_committed_fragment(node, |fragment| fragment.identity);- self.run- .pass- .in_flow_block_cache_store()- .store(node.slot_index(), entry, committed_identity);+ if let Some(store) = self.run.pass.in_flow_block_cache_store() {+ let committed_identity = arena.with_current_committed_fragment(node, |fragment| fragment.identity);+ store.store(node.slot_index(), entry, committed_identity);+ }}/// Whether a float placed so far reaches below `block_offset` in `block_container`.@@ -5134,6 +5136,7 @@ impl<'pass> BlockFormattingContext<'pass> {== abspos_containing_block_info_contributions_received&& self.run.pass.host_findings_noted_so_far() == host_findings_noted_so_far&& let Some(placed) = reused.placed.as_ref().filter(|placed| !placed.exports_payloads)+ && let Some(store) = self.run.pass.in_flow_block_cache_store(){entry.fragment_identity = placed.fragment.identity;entry.floats_read_by_contents.margins_before_box = margins_before_box;@@ -5144,10 +5147,7 @@ impl<'pass> BlockFormattingContext<'pass> {.pass.arena().with_current_committed_fragment(node, |fragment| fragment.identity);- self.run- .pass- .in_flow_block_cache_store()- .store(node.slot_index(), entry, committed_identity);+ store.store(node.slot_index(), entry, committed_identity);}Some(LaidOutBlockLevelBox {content_offset: Some(reused.content_offset),diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex e6b1e81e5c3..5a04aa73b84 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -814,17 +814,18 @@ impl FcRunCacheAttempt {pass: &LayoutPass<'_>,key: &FcRunCacheKey,) -> Result<Self, FcRunCacheHit> {- if !can_describe_run(- box_,- runs_as_subgrid,- should_collect_devtools_layout_data,- measurement_root_keeps_its_result,- pass,- key,- ) {+ let Some(store) = pass.fc_run_cache_store().filter(|_| {+ can_describe_run(+ box_,+ runs_as_subgrid,+ should_collect_devtools_layout_data,+ measurement_root_keeps_its_result,+ pass,+ key,+ )+ }) else {return Ok(Self::Bypass);- }- let store = pass.fc_run_cache_store();+ };let validity = run_root_validity(pass, box_);if !builds_fragments {if let Some(entry) = store.first_entry_where_summary_allows(@@ -865,18 +866,20 @@ impl FcRunCacheAttempt {pass: &LayoutPass<'_>,key: &FcRunCacheKey,) -> bool {- if !can_describe_run(- box_,- false,- should_collect_devtools_layout_data,- measurement_root_keeps_its_result,- pass,- key,- ) {+ let Some(store) = pass.fc_run_cache_store().filter(|_| {+ can_describe_run(+ box_,+ false,+ should_collect_devtools_layout_data,+ measurement_root_keeps_its_result,+ pass,+ key,+ )+ }) else {return false;- }+ };let validity = run_root_validity(pass, box_);- pass.fc_run_cache_store()+ store.first_entry_where_summary_allows(box_.slot_index(),|summary| summary.may_answer_probe(validity, builds_fragments),@@ -909,12 +912,15 @@ impl FcRunCacheAttempt {key: &FcRunCacheKey,outputs: formatting_context::RunOutputs,) -> formatting_context::RunOutputsHandle {- let store = pass.fc_run_cache_store();+ let store = || {+ pass.fc_run_cache_store()+ .expect("a run that probed a store concludes where it can store")+ };let (validity, structurally_damaged_entry) = match self {Self::Bypass => return formatting_context::RunOutputsHandle::Owned(outputs),Self::StoreResultOnly { validity } => {debug_assert!(outputs.root.is_none(), "a run that builds no fragments returned a root");- return formatting_context::RunOutputsHandle::Stored(store.store(+ return formatting_context::RunOutputsHandle::Stored(store().store(box_,FcRunCacheEntry {key: key.clone(),@@ -951,7 +957,7 @@ impl FcRunCacheAttempt {structural_epoch_bumps_when_stored: 0,};formatting_context::RunOutputsHandle::Stored(- store.store(box_, entry, |entry| entry.holds_committed_fragments(pass.arena(), box_)),+ store().store(box_, entry, |entry| entry.holds_committed_fragments(pass.arena(), box_)),)}}diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex cd1936db15b..ccaf3d433af 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -2042,31 +2042,6 @@ pub(super) fn run_formatting_context(!parent_collects_fragments || builds_fragments,"the run of {box_:?} builds no fragments for its parent to commit");- if pass.reads_no_memoized_layout() {- let outputs = execute_formatting_context_run(- builds_fragments,- root_cells,- box_,- root_containing_block,- subgrid_parent,- fc_type,- should_collect_devtools_layout_data,- pass,- &input,- None,- table_inline_layout,- None,- );- return child_layout_from_outputs(- &pass,- parent_collects_fragments,- box_,- &input,- root_cells,- RunOutputsHandle::Owned(outputs),- false,- );- }let key = fc_run_cache::FcRunCacheKey::new(&pass, fc_type, input, root_cells);let attempt = fc_run_cache::FcRunCacheAttempt::probe(box_,@@ -3536,9 +3511,6 @@ pub(crate) fn lay_out_shape_for_its_result(pass: LayoutPass<'_>, shape: ChildRunpass.receive_host_findings(host_findings);result};- if pass.reads_no_memoized_layout() {- return result_with_host_findings(RunOutputsHandle::Owned(lay_out(pass, root_cells, input, None)));- }// NB: The only shapes of table cells are those laid out ahead of the intrinsic block padding their table hands them.let runs_ahead_of_intrinsic_block_padding = builds_fragments && NodeFacts::new(&pass, node).is_table_cell();let cache_attempt =diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rsindex 3825ae76ead..8d99dc248a6 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs@@ -34,12 +34,8 @@ 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 super::run_state::HostFindingsSink,- /// 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, prepared inline content and intrinsic sizes.- reads_no_memoized_layout: bool,+ /// The layout memos the runs this pass view reaches read and keep.+ memos: MemoScope<'arena>,/// The innermost of the position options the boxes this pass view lays out are laid out with.position_option_styles: Option<&'arena PositionOptionStyle<'arena>>,records_trace: bool,@@ -100,6 +96,17 @@ impl WorkerScratchOfThisThread {}}+/// Which layout memos a pass view reads and keeps: the run cache, the in-flow block cache, prepared inline content and+/// intrinsic sizes.+#[derive(Clone, Copy)]+enum MemoScope<'arena> {+ /// Those kept for the runs and passes after the ones that made them.+ Kept,+ /// None from before a run whose result is discarded, which keeps none after it either. It measures intrinsic sizes+ /// into caches of its own.+ Discarded(&'arena super::layout_node_arena::IntrinsicSizeCaches),+}+/// The style a box is laid out with in place of the style of its row: that of one of its position options. It links to/// the position option the box is laid out inside of, if any./// https://drafts.csswg.org/css-anchor-position-1/#apply-a-position-option@@ -127,8 +134,7 @@ impl<'arena> LayoutPass<'arena> {frame: super::geometry::Frame::PHYSICAL,innermost_run: (NodeSlotId::INVALID, NodeSlotId::INVALID),host_findings: host_findings_outside_runs,- intrinsic_size_caches: &scratch.intrinsic_size_caches,- reads_no_memoized_layout: false,+ memos: MemoScope::Kept,position_option_styles: None,records_trace: scratch.layout_trace.is_recording(),forks_runs: false,@@ -156,7 +162,7 @@ impl<'arena> LayoutPass<'arena> {}pub(super) fn forks_runs(&self) -> bool {- self.forks_runs && !self.reads_no_memoized_layout && !self.records_trace+ self.forks_runs && !self.is_discarded_run() && !self.records_trace}pub(super) fn noting_host_findings_in<'findings>(@@ -228,8 +234,7 @@ impl<'arena> LayoutPass<'arena> {intrinsic_size_caches.cover_slots(self.arena.slot_capacity());let pass: LayoutPass<'_> = self;run(LayoutPass {- intrinsic_size_caches: &intrinsic_size_caches,- reads_no_memoized_layout: true,+ memos: MemoScope::Discarded(&intrinsic_size_caches),..pass})}@@ -275,30 +280,38 @@ impl<'arena> LayoutPass<'arena> {/// Where this pass view records what its runs do while the document traces its layout: the document's layout trace,/// or nowhere inside a run whose result is discarded.pub(crate) fn layout_trace(&self) -> super::trace::LayoutTraceTarget<'arena> {- if self.reads_no_memoized_layout || !self.records_trace {+ if self.is_discarded_run() || !self.records_trace {return super::trace::LayoutTraceTarget::recording_nothing();}super::trace::LayoutTraceTarget::recording_into(&self.scratch.layout_trace)}- /// The results of formatting context runs, kept for the runs and passes after them.- pub(crate) fn fc_run_cache_store(&self) -> &'arena super::fc_run_cache::FcRunCacheStore {- self.scratch.fc_run_cache_store()+ /// The results of formatting context runs, kept for the runs and passes after them, unless this pass view is inside+ /// a run whose result is discarded.+ pub(crate) fn fc_run_cache_store(&self) -> Option<&'arena super::fc_run_cache::FcRunCacheStore> {+ self.kept_memos().then(|| self.scratch.fc_run_cache_store())}pub(crate) fn fragment_builder_scratch(&self) -> &'arena super::fragment_tree::FragmentBuilderScratch {self.scratch.fragment_builder_scratch()}- /// The layouts of in-flow blocks, kept for the passes after the one that laid them out.- pub(crate) fn in_flow_block_cache_store(&self) -> &'arena super::in_flow_block_cache::InFlowBlockCacheStore {- self.scratch.in_flow_block_cache_store()+ /// The layouts of in-flow blocks, kept for the passes after the one that laid them out, unless this pass view is+ /// inside a run whose result is discarded.+ pub(crate) fn in_flow_block_cache_store(+ &self,+ ) -> Option<&'arena super::in_flow_block_cache::InFlowBlockCacheStore> {+ self.kept_memos().then(|| self.scratch.in_flow_block_cache_store())}/// 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+ pub(crate) fn is_discarded_run(&self) -> bool {+ matches!(self.memos, MemoScope::Discarded(_))+ }++ fn kept_memos(&self) -> bool {+ matches!(self.memos, MemoScope::Kept)}/// Notes what the run this pass view was handed to found out for the host, or the pass outside any run.@@ -320,7 +333,10 @@ impl<'arena> LayoutPass<'arena> {/// 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.intrinsic_size_caches+ match self.memos {+ MemoScope::Kept => &self.scratch.intrinsic_size_caches,+ MemoScope::Discarded(intrinsic_size_caches) => intrinsic_size_caches,+ }}#[inline]@@ -351,10 +367,12 @@ impl<'arena> LayoutPass<'arena> {)}+ /// The inline content prepared for block containers, kept for the runs after the one that prepared it, unless this+ /// pass view is inside a run whose result is discarded.pub(crate) fn prepared_inline_content_memos(&self,- ) -> &'arena super::prepared_inline_content::PreparedInlineContentMemos {- self.scratch.prepared_inline_content_memos()+ ) -> Option<&'arena super::prepared_inline_content::PreparedInlineContentMemos> {+ self.kept_memos().then(|| self.scratch.prepared_inline_content_memos())}pub(crate) fn take_inline_content_preparation_buffers(diff --git a/Libraries/LibWeb/Rust/src/layout/prepared_inline_content.rs b/Libraries/LibWeb/Rust/src/layout/prepared_inline_content.rsindex 67ffa7b7345..1bd450c3098 100644--- a/Libraries/LibWeb/Rust/src/layout/prepared_inline_content.rs+++ b/Libraries/LibWeb/Rust/src/layout/prepared_inline_content.rs@@ -222,10 +222,12 @@ impl PreparedInlineContent {}pub(crate) fn for_block_container(pass: LayoutPass<'_>, block_container: Node) -> Arc<Self> {- if pass.reads_no_memoized_layout() || pass.lays_out_with_position_option() {+ let Some(memos) = pass+ .prepared_inline_content_memos()+ .filter(|_| !pass.lays_out_with_position_option())+ else {return Arc::new(Self::prepare(pass, block_container));- }- let memos = pass.prepared_inline_content_memos();+ };if let Some(kept) = memos.kept(block_container, run_root_validity(&pass, block_container)) {if cfg!(debug_assertions) || run_repeatability::verification_is_enabled() {kept.verify_against_fresh_preparation(pass, block_container);diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rsindex fad4729f9b2..6c41b3e75cc 100644--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs@@ -40,7 +40,7 @@ pub(super) fn lay_out_discarded_run(pass: &LayoutPass<'_>,run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> formatting_context::RunOutputs,) -> Option<formatting_context::RunOutputs> {- if run_repeatability_verification() == RunRepeatabilityVerification::Off || pass.reads_no_memoized_layout() {+ if run_repeatability_verification() == RunRepeatabilityVerification::Off || pass.is_discarded_run() {return None;}Some(pass.lay_out_discarded_run(run))@@ -186,7 +186,7 @@ pub(super) fn verify_run_ignores_its_unobserved_own_block_size_and_block_marginsLayoutInput,) -> formatting_context::RunOutputs,) {- if !unobserved_own_block_size_verification_is_enabled() || pass.reads_no_memoized_layout() {+ if !unobserved_own_block_size_verification_is_enabled() || pass.is_discarded_run() {return;}let checked = format!("the run of {box_:?}, which never observed its own block size or block margins,");