Truly independent web browser
LibWeb: Memoize intrinsic sizes within discarded layout runs
Aliaksandr Kalenik3957cafabe9e
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 42ebfc400d0..2072238a34f 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -409,15 +409,14 @@ pub(crate) struct IntrinsicSizeCacheStamp {#[derive(Default)]pub(crate) struct IntrinsicSizeCaches {slots: RefCell<Vec<IntrinsicSizeCacheSlot>>,- suspended: Cell<bool>,}impl IntrinsicSizeCaches {- /// Runs `measure` with the caches answering nothing and keeping nothing.- pub(crate) fn suspended_while<R>(&self, measure: impl FnOnce() -> R) -> R {- let was_suspended = self.suspended.replace(true);+ /// 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.suspended.set(was_suspended);+ self.slots.replace(set_aside);result}@@ -437,9 +436,6 @@ impl IntrinsicSizeCaches {stamp: IntrinsicSizeCacheStamp,answer: impl FnOnce(&IntrinsicSizeMaps) -> Option<R>,) -> Option<R> {- if self.suspended.get() {- return None;- }let caches = self.slots.borrow();let slot = caches.get(stamp.index as usize)?;if slot.generation != stamp.generation || slot.epoch != stamp.epoch {@@ -463,9 +459,6 @@ impl IntrinsicSizeCaches {}fn with_maps_mut(&self, stamp: IntrinsicSizeCacheStamp, callback: impl FnOnce(&mut IntrinsicSizeMaps)) {- if self.suspended.get() {- return;- }let IntrinsicSizeCacheStamp {index,generation,diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex 691a5ffb978..f4b1bbc399f 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -32,7 +32,8 @@ pub(crate) struct LayoutScratch {/// outside any run.host_findings_of_runs_in_progress: RefCell<Vec<HostFindings>>,host_findings_outside_runs: RefCell<HostFindings>,- /// How many runs whose results are discarded are in progress, which neither read nor keep memoized layout.+ /// 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>,}@@ -204,18 +205,18 @@ impl LayoutScratch {self.inline_item_stashes.borrow().get(&block_container).cloned()}- /// Lays out `run`, whose result is discarded, without reading or keeping memoized layout, so that it computes- /// everything afresh and leaves nothing for the runs after it.+ /// 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.suspended_while(run);+ 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 and keeps no memoized layout: the run- /// cache, the in-flow block cache, inline item stashes, intrinsic sizes and memoized measurements.+ /// 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, memoized measurements and intrinsic sizes.pub(crate) fn reads_no_memoized_layout(&self) -> bool {self.discarded_runs_in_progress.get() > 0}diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rsindex 57896e8cb31..810825a26b2 100644--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs@@ -4,10 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/-//! With LIBWEB_VERIFY_RUN_REPEATABILITY=cold, every layout run that executes is laid out twice. The first layout-//! reads and keeps no memoized layout and its result is discarded; the second is the run itself. Both have to agree on-//! everything the run hands its parent, which shows that a run depends on nothing but its input and the state of the-//! pass, and that what it reads from memoized layout is what it would have computed.+//! With LIBWEB_VERIFY_RUN_REPEATABILITY=cold, every layout run that executes is laid out twice. The first layout reads+//! no memoized layout from before it, keeps none after it, and its result is discarded; the second is the run itself.+//! Both have to agree on everything the run hands its parent, which shows that a run depends on nothing but its input+//! and the state of the pass, and that what it reads from memoized layout is what it would have computed.use super::*;