Truly independent web browser
LibWeb: Drop cached intrinsic sizes through memo invalidation notes
Aliaksandr Kalenikc50b81d58a4b
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rsindex 5d59c40b78d..e99c44d7193 100644--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs@@ -1309,10 +1309,11 @@ mod tests {let store = FcRunCacheStore::default();let in_flow_block_cache = InFlowBlockCacheStore::default();let text_memos = crate::layout::rendered_text::TextMemos::default();+ let intrinsic_size_caches = crate::layout::layout_node_arena::IntrinsicSizeCaches::default();let node = Node::new(0, 1);let mut notes = MemoInvalidations::default();notes.note_structural_inline_change(node);- notes.apply_to(&store, &in_flow_block_cache, &text_memos);+ notes.apply_to(&store, &in_flow_block_cache, &text_memos, &intrinsic_size_caches);assert_eq!(store.structural_epoch_bumps_of(node), 0);store.store(node, entry(at(100), 1, Some(root_with_a_descendant()), 0));@@ -1320,7 +1321,7 @@ mod tests {notes.note_structural_inline_change(node);notes.note_fragment_cache_epoch_bumped(node);notes.note_structural_inline_change(node);- notes.apply_to(&store, &in_flow_block_cache, &text_memos);+ notes.apply_to(&store, &in_flow_block_cache, &text_memos, &intrinsic_size_caches);assert_eq!(store.structural_epoch_bumps_of(node), 2);let reused = Node::new(0, 2);@@ -1328,7 +1329,7 @@ mod tests {notes.note_structural_inline_change(node);notes.note_slot_freed(node.slot_index());notes.note_structural_inline_change(reused);- notes.apply_to(&store, &in_flow_block_cache, &text_memos);+ notes.apply_to(&store, &in_flow_block_cache, &text_memos, &intrinsic_size_caches);assert!(!store.slot_holds_entries(0));assert_eq!(store.structural_epoch_bumps_of(reused), 0);}diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 3e31d4a8246..78f78703442 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -3633,7 +3633,8 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo}/// Brings the arena up to date for a stage before the stage borrows it: derives the facts its runs read, marks the-/// flex and grid items of the nodes marked for layout, and drops the cached intrinsic sizes of released slots.+/// flex and grid items of the nodes marked for layout, and applies the arena's memo invalidations to the stage's+/// memos.fn prepare_layout_stage(state: &mut super::ArenaHandle, kind: LayoutStageKind, root: NodeSlotId) {let (arena, scratch) = state.arena_mut_and_layout_scratch();match kind {@@ -3657,24 +3658,11 @@ fn prepare_layout_stage(state: &mut super::ArenaHandle, kind: LayoutStageKind, rarena.derive_facts_in_subtree(root);}}- drop_intrinsic_size_cache_entries_of_released_slots(arena, scratch);scratch.apply_memo_invalidations(arena);scratch.layout_trace.take_over_from(&mut arena.layout_trace);arena.begin_active_layout_pass();}-/// Drops the intrinsic size cache entries of the slots the arena let go of, or whose cache epoch wrapped, since the-/// last stage, before any run of this one reads a cache.-fn drop_intrinsic_size_cache_entries_of_released_slots(- arena: &mut LayoutNodeArena,- scratch: &super::run_state::LayoutScratch,-) {- let drops = arena.take_intrinsic_size_cache_drops();- if !drops.is_empty() {- scratch.intrinsic_size_caches.drop_slots(drops);- }-}-fn finish_entry_pass(entry_run_state: &RunState,entry_fragments: &std::rc::Rc<fragment_tree::RunFragmentBuilder>,diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 0a5b8cd2381..81e223bf947 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -314,47 +314,29 @@ impl IntrinsicSizeMaps {#[derive(Default)]struct IntrinsicSizeCacheSlot {generation: u8,- epoch: u16,sizes: Option<Box<IntrinsicSizeMaps>>,}-/// What an intrinsic size cache entry was measured for: the row in a slot, and the row's-/// intrinsic cache epoch, which every change that can move the row's intrinsic sizes bumps.-#[derive(Clone, Copy)]-pub(crate) struct IntrinsicSizeCacheStamp {- index: u32,- generation: u8,- epoch: u16,-}-/// The intrinsic sizes layout measured, kept from one pass to the next. Only layout reads or-/// writes them, so they are the layout stage's scratch; the arena only notes the slots whose-/// entries must go, which the stage drops before its next run reads anything.+/// writes them, so they are the layout stage's scratch; the arena notes the boxes whose sizes+/// went stale in its memo invalidations, which the stage applies before its next run reads any.#[derive(Default)]pub(crate) struct IntrinsicSizeCaches {slots: RefCell<Vec<IntrinsicSizeCacheSlot>>,}impl IntrinsicSizeCaches {- /// 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();- for index in slots {- if let Some(slot) = caches.get_mut(index as usize) {- *slot = IntrinsicSizeCacheSlot::default();- }+ pub(crate) fn drop_entries_of_slot(&self, slot: u32) {+ if let Some(slot) = self.slots.borrow_mut().get_mut(slot as usize) {+ *slot = IntrinsicSizeCacheSlot::default();}}- /// Answers `answer` from the maps of the row `stamp` names, where they hold what was measured for it.- fn with_maps<R>(- &self,- stamp: IntrinsicSizeCacheStamp,- answer: impl FnOnce(&IntrinsicSizeMaps) -> Option<R>,- ) -> Option<R> {+ /// Answers `answer` from the maps of `node`, where they hold what was measured for it.+ fn with_maps<R>(&self, node: NodeSlotId, answer: impl FnOnce(&IntrinsicSizeMaps) -> Option<R>) -> Option<R> {let caches = self.slots.borrow();- let slot = caches.get(stamp.index as usize)?;- if slot.generation != stamp.generation || slot.epoch != stamp.epoch {+ let slot = caches.get(node.slot_index() as usize)?;+ if slot.generation != node.generation() {return None;}answer(slot.sizes.as_ref()?)@@ -362,11 +344,11 @@ impl IntrinsicSizeCaches {pub(crate) fn intrinsic_block_size_cache_get(&self,- stamp: IntrinsicSizeCacheStamp,+ node: NodeSlotId,kind: IntrinsicSizeCacheKind,key: IntrinsicSizeCacheKey,) -> Option<IntrinsicBlockSizeMeasurement> {- self.with_maps(stamp, |maps| {+ self.with_maps(node, |maps| {let map = maps.block_sizes(kind).expect("block size cache kind must use the block axis");@@ -374,22 +356,17 @@ impl IntrinsicSizeCaches {})}- fn with_maps_mut(&self, stamp: IntrinsicSizeCacheStamp, callback: impl FnOnce(&mut IntrinsicSizeMaps)) {- let IntrinsicSizeCacheStamp {- index,- generation,- epoch,- } = stamp;+ fn with_maps_mut(&self, node: NodeSlotId, callback: impl FnOnce(&mut IntrinsicSizeMaps)) {+ let index = node.slot_index() as usize;let mut caches = self.slots.borrow_mut();- if caches.len() <= index as usize {- caches.resize_with(index as usize + 1, IntrinsicSizeCacheSlot::default);+ if caches.len() <= index {+ caches.resize_with(index + 1, IntrinsicSizeCacheSlot::default);}- let slot = &mut caches[index as usize];- if slot.generation != generation || slot.epoch != epoch {+ let slot = &mut caches[index];+ if slot.generation != node.generation() {*slot = IntrinsicSizeCacheSlot {- generation,- epoch,- sizes: Some(Box::default()),+ generation: node.generation(),+ sizes: None,};}callback(slot.sizes.get_or_insert_with(Box::default));@@ -397,12 +374,12 @@ impl IntrinsicSizeCaches {pub(crate) fn intrinsic_block_size_cache_put(&self,- stamp: IntrinsicSizeCacheStamp,+ node: NodeSlotId,kind: IntrinsicSizeCacheKind,key: IntrinsicSizeCacheKey,value: IntrinsicBlockSizeMeasurement,) {- self.with_maps_mut(stamp, |maps| {+ self.with_maps_mut(node, |maps| {let map = maps.block_sizes_mut(kind).expect("block size cache kind must use the block axis");@@ -412,22 +389,22 @@ impl IntrinsicSizeCaches {pub(crate) fn intrinsic_inline_size_measurement_cache_get(&self,- stamp: IntrinsicSizeCacheStamp,+ node: NodeSlotId,key: IntrinsicSizeCacheKey,) -> Option<IntrinsicInlineSizeMeasurement> {- self.with_maps(stamp, |maps| intrinsic_cache_lookup(&maps.content_inline_sizes, key))+ self.with_maps(node, |maps| intrinsic_cache_lookup(&maps.content_inline_sizes, key))}pub(crate) fn intrinsic_inline_size_depends_on_block_size(&self,- stamp: IntrinsicSizeCacheStamp,+ node: NodeSlotId,compute: impl FnOnce() -> bool,) -> bool {- if let Some(value) = self.with_maps(stamp, |maps| maps.inline_size_depends_on_block_size) {+ if let Some(value) = self.with_maps(node, |maps| maps.inline_size_depends_on_block_size) {return value;}let value = compute();- self.with_maps_mut(stamp, |maps| {+ self.with_maps_mut(node, |maps| {maps.inline_size_depends_on_block_size = Some(value);});value@@ -435,11 +412,11 @@ impl IntrinsicSizeCaches {pub(crate) fn intrinsic_inline_size_measurement_cache_put(&self,- stamp: IntrinsicSizeCacheStamp,+ node: NodeSlotId,key: IntrinsicSizeCacheKey,value: IntrinsicInlineSizeMeasurement,) {- self.with_maps_mut(stamp, |maps| {+ self.with_maps_mut(node, |maps| {intrinsic_cache_store(&mut maps.content_inline_sizes, key, value);});}@@ -1245,8 +1222,6 @@ pub(crate) struct LayoutNodeArena {free_list: Vec<u32>,next_index: u32,live_count: u32,- /// The slots whose intrinsic size cache entries the layout stage drops before its next run.- intrinsic_size_cache_drops: Vec<u32>,default_scroll_shift_anchors: Vec<DefaultScrollShiftAnchorSlot>,any_default_scroll_shift_anchor_ever_stored: bool,text_slots: TextSlots,@@ -1366,7 +1341,6 @@ impl LayoutNodeArena {free_list: Vec::new(),next_index: 0,live_count: 0,- intrinsic_size_cache_drops: Vec::new(),default_scroll_shift_anchors: Vec::new(),any_default_scroll_shift_anchor_ever_stored: false,text_slots: TextSlots::default(),@@ -1412,19 +1386,6 @@ impl LayoutNodeArena {}}- /// Drops one node's cached intrinsic sizes outright, for the wrap of its epoch:- /// entries are stamped with the epoch they were measured under, so a stamp reused- /// after a full lap would match a pre-wrap entry.- fn drop_intrinsic_size_cache(&mut self, node: NodeSlotId) {- self.intrinsic_size_cache_drops.push(node.slot_index());- }-- /// The slots whose intrinsic size cache entries must go, for the layout stage to drop before- /// its next run reads any.- pub(crate) fn take_intrinsic_size_cache_drops(&mut self) -> Vec<u32> {- std::mem::take(&mut self.intrinsic_size_cache_drops)- }-pub(crate) fn layout_commit_count(&self) -> u64 {self.layout_commit_count}@@ -1433,26 +1394,12 @@ impl LayoutNodeArena {self.layout_commit_count += 1;}- pub(crate) fn has_intrinsic_size_cache_drops(&self) -> bool {- !self.intrinsic_size_cache_drops.is_empty()- }-- /// What an intrinsic size cache entry measured for the row `id` now is valid for.- pub(crate) fn intrinsic_size_cache_stamp(&self, id: NodeSlotId) -> IntrinsicSizeCacheStamp {- IntrinsicSizeCacheStamp {- index: id.slot_index(),- generation: id.generation(),- epoch: self.data(id).intrinsic_cache_epoch,- }- }-pub(crate) fn reset_cached_intrinsic_sizes(&mut self, node: NodeSlotId) {- let epoch = self.unpublished_facts_mut(node).intrinsic_cache_epoch;- *epoch = epoch.wrapping_add(1);- let bumped_epoch = *epoch;- if bumped_epoch == 0 {- self.drop_intrinsic_size_cache(node);- }+ debug_assert!(+ !self.layout_pass_is_running(),+ "a node's intrinsic sizes became stale while a layout pass ran"+ );+ self.memo_invalidations.note_intrinsic_sizes_dropped(node.slot_index());}/// What writes did to the boxes the layout stage's run memos hold entries of since the stage last took it.@@ -1750,7 +1697,6 @@ impl LayoutNodeArena {self.style_record_pins[index as usize] = ArenaStylePin::None;self.style_records_pinned_by_host[index as usize] = 0;- self.intrinsic_size_cache_drops.push(index);if let Some(slot) = self.default_scroll_shift_anchors.get_mut(index as usize) {*slot = DefaultScrollShiftAnchorSlot::default();}@@ -7482,10 +7428,12 @@ mod tests {}#[test]- fn intrinsic_size_cache_validates_epoch_and_generation() {+ fn intrinsic_size_cache_entries_go_with_noted_resets_and_slot_reuse() {let mut arena = LayoutNodeArena::new();- let caches = super::IntrinsicSizeCaches::default();+ let scratch = crate::layout::run_state::LayoutScratch::default();+ let caches = &scratch.intrinsic_size_caches;let first = arena.allocate_for_test();+ scratch.apply_memo_invalidations(&mut arena);let key = IntrinsicSizeCacheKey {measured_at_inline_size: Some(CssPixels::from_raw(64)),..Default::default()@@ -7505,79 +7453,59 @@ mod tests {};let dependency_computations = Cell::new(0);- caches.intrinsic_block_size_cache_put(- arena.intrinsic_size_cache_stamp(first.slot),- IntrinsicSizeCacheKind::MaxContentBlock,- key,- value,- );- caches.intrinsic_inline_size_measurement_cache_put(- arena.intrinsic_size_cache_stamp(first.slot),- key,- inline_measurement,- );+ caches.intrinsic_block_size_cache_put(first.slot, IntrinsicSizeCacheKind::MaxContentBlock, key, value);+ caches.intrinsic_inline_size_measurement_cache_put(first.slot, key, inline_measurement);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(first.slot),- IntrinsicSizeCacheKind::MaxContentBlock,- key- ),+ caches.intrinsic_block_size_cache_get(first.slot, IntrinsicSizeCacheKind::MaxContentBlock, key),Some(value));assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(arena.intrinsic_size_cache_stamp(first.slot), key),+ caches.intrinsic_inline_size_measurement_cache_get(first.slot, key),Some(inline_measurement));- assert!(caches.intrinsic_inline_size_depends_on_block_size(- arena.intrinsic_size_cache_stamp(first.slot),- || {- dependency_computations.set(dependency_computations.get() + 1);- true- }- ));- assert!(- caches.intrinsic_inline_size_depends_on_block_size(arena.intrinsic_size_cache_stamp(first.slot), || false)- );+ assert!(caches.intrinsic_inline_size_depends_on_block_size(first.slot, || {+ dependency_computations.set(dependency_computations.get() + 1);+ true+ }));+ assert!(caches.intrinsic_inline_size_depends_on_block_size(first.slot, || false));assert_eq!(dependency_computations.get(), 1);- *arena.unpublished_facts_mut(first.slot).intrinsic_cache_epoch += 1;+ arena.reset_cached_intrinsic_sizes(first.slot);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(first.slot),- IntrinsicSizeCacheKind::MaxContentBlock,- key- ),+ caches.intrinsic_inline_size_measurement_cache_get(first.slot, key),+ Some(inline_measurement),+ "a reset reaches the cache when the stage applies the arena's notes"+ );+ scratch.apply_memo_invalidations(&mut arena);+ assert_eq!(+ caches.intrinsic_block_size_cache_get(first.slot, IntrinsicSizeCacheKind::MaxContentBlock, key),None);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(arena.intrinsic_size_cache_stamp(first.slot), key),+ caches.intrinsic_inline_size_measurement_cache_get(first.slot, key),None);- assert!(!caches.intrinsic_inline_size_depends_on_block_size(- arena.intrinsic_size_cache_stamp(first.slot),- || {- dependency_computations.set(dependency_computations.get() + 1);- false- }- ));+ assert!(!caches.intrinsic_inline_size_depends_on_block_size(first.slot, || {+ dependency_computations.set(dependency_computations.get() + 1);+ false+ }));assert_eq!(dependency_computations.get(), 2);++ caches.intrinsic_inline_size_measurement_cache_put(first.slot, key, inline_measurement);arena.free_subtree(first.slot).destroy_shells_and_invoke_callbacks(&crate::stage::MainThread::for_test());-let second = arena.allocate_for_test();assert_eq!(second.slot.slot_index(), first.slot.slot_index());assert_ne!(second.slot, first.slot);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(second.slot),- IntrinsicSizeCacheKind::MaxContentBlock,- key- ),- None+ caches.intrinsic_inline_size_measurement_cache_get(second.slot, key),+ None,+ "a reused slot misses by its generation before the stage applies the freeing note");+ scratch.apply_memo_invalidations(&mut arena);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(arena.intrinsic_size_cache_stamp(second.slot), key),+ caches.intrinsic_inline_size_measurement_cache_get(second.slot, key),None);arena@@ -7608,34 +7536,17 @@ mod tests {depends_on_percentage_block_size: false,depends_on_percentage_inline_basis: false,};- caches.intrinsic_block_size_cache_put(- arena.intrinsic_size_cache_stamp(allocation.slot),- max_content,- key_with_basis(100),- independent,- );+ caches.intrinsic_block_size_cache_put(allocation.slot, max_content, key_with_basis(100), independent);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),- max_content,- key_with_basis(100)- ),+ caches.intrinsic_block_size_cache_get(allocation.slot, max_content, key_with_basis(100)),Some(independent));assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),- max_content,- key_with_basis(200)- ),+ caches.intrinsic_block_size_cache_get(allocation.slot, max_content, key_with_basis(200)),Some(independent));assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),- max_content,- key_without_basis- ),+ caches.intrinsic_block_size_cache_get(allocation.slot, max_content, key_without_basis),Some(independent));@@ -7644,48 +7555,22 @@ mod tests {depends_on_percentage_block_size: true,depends_on_percentage_inline_basis: false,};- caches.intrinsic_block_size_cache_put(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_without_basis,- dependent,- );+ caches.intrinsic_block_size_cache_put(dependent_allocation.slot, max_content, key_without_basis, dependent);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_without_basis- ),+ caches.intrinsic_block_size_cache_get(dependent_allocation.slot, max_content, key_without_basis),Some(dependent));assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_with_basis(100)- ),+ caches.intrinsic_block_size_cache_get(dependent_allocation.slot, max_content, key_with_basis(100)),None);- caches.intrinsic_block_size_cache_put(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_with_basis(100),- dependent,- );+ caches.intrinsic_block_size_cache_put(dependent_allocation.slot, max_content, key_with_basis(100), dependent);assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_with_basis(100)- ),+ caches.intrinsic_block_size_cache_get(dependent_allocation.slot, max_content, key_with_basis(100)),Some(dependent));assert_eq!(- caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(dependent_allocation.slot),- max_content,- key_with_basis(200)- ),+ caches.intrinsic_block_size_cache_get(dependent_allocation.slot, max_content, key_with_basis(200)),None);@@ -7700,14 +7585,14 @@ mod tests {depends_on_percentage_inline_basis: true,};caches.intrinsic_block_size_cache_put(- arena.intrinsic_size_cache_stamp(allocation.slot),+ allocation.slot,max_content,key_at_another_inline_size_with_inline_basis(300),observes_inline_basis,);assert_eq!(caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),+ allocation.slot,max_content,key_at_another_inline_size_with_inline_basis(300)),@@ -7715,7 +7600,7 @@ mod tests {);assert_eq!(caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),+ allocation.slot,max_content,IntrinsicSizeCacheKey {percentage_basis_block_size: Some(CssPixels::from_raw(200)),@@ -7726,7 +7611,7 @@ mod tests {);assert_eq!(caches.intrinsic_block_size_cache_get(- arena.intrinsic_size_cache_stamp(allocation.slot),+ allocation.slot,max_content,key_at_another_inline_size_with_inline_basis(400)),@@ -7784,9 +7669,8 @@ mod tests {let measured_key = inline_size_cache_key(Some(100), Some(300), Some(50));for observed_parts in every_combination_of_key_parts() {let allocation = arena.allocate_for_test();- let stamp = || arena.intrinsic_size_cache_stamp(allocation.slot);let measurement = inline_size_measurement(128, observed_parts);- caches.intrinsic_inline_size_measurement_cache_put(stamp(), measured_key, measurement);+ caches.intrinsic_inline_size_measurement_cache_put(allocation.slot, measured_key, measurement);for changed_parts in every_combination_of_key_parts() {let measured_or_changed =|changed: bool, measured: i32| Some(if changed { measured * 2 } else { measured });@@ -7800,7 +7684,7 @@ mod tests {|| (changed_parts.percentage_inline_basis && observed_parts.percentage_inline_basis)|| (changed_parts.block_size && observed_parts.block_size);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(stamp(), probe),+ caches.intrinsic_inline_size_measurement_cache_get(allocation.slot, probe),(!probe_changes_an_observed_part).then_some(measurement),"observed {observed_parts:?}, changed {changed_parts:?}");@@ -7822,17 +7706,16 @@ mod tests {let key_at_block_size = |block_size: Option<i32>| inline_size_cache_key(None, None, block_size);let independent = arena.allocate_for_test();- let independent_stamp = || arena.intrinsic_size_cache_stamp(independent.slot);let measured_at_definite_block_size = inline_size_measurement(128, IntrinsicMeasurementDependencies::default());caches.intrinsic_inline_size_measurement_cache_put(- independent_stamp(),+ independent.slot,key_at_block_size(Some(50)),measured_at_definite_block_size,);for probed_block_size in [Some(50), Some(80), None] {assert_eq!(caches.intrinsic_inline_size_measurement_cache_get(- independent_stamp(),+ independent.slot,key_at_block_size(probed_block_size)),Some(measured_at_definite_block_size)@@ -7840,43 +7723,40 @@ mod tests {}let dependent = arena.allocate_for_test();- let dependent_stamp = || arena.intrinsic_size_cache_stamp(dependent.slot);let dependent_measurement = inline_size_measurement(256, observes_block_size);caches.intrinsic_inline_size_measurement_cache_put(- dependent_stamp(),+ dependent.slot,key_at_block_size(Some(50)),dependent_measurement,);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(dependent_stamp(), key_at_block_size(Some(50))),+ caches.intrinsic_inline_size_measurement_cache_get(dependent.slot, key_at_block_size(Some(50))),Some(dependent_measurement));for probed_block_size in [Some(80), None] {assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(- dependent_stamp(),- key_at_block_size(probed_block_size)- ),+ caches+ .intrinsic_inline_size_measurement_cache_get(dependent.slot, key_at_block_size(probed_block_size)),None);}let measured_without_block_size = inline_size_measurement(512, observes_block_size);caches.intrinsic_inline_size_measurement_cache_put(- dependent_stamp(),+ dependent.slot,key_at_block_size(None),measured_without_block_size,);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(dependent_stamp(), key_at_block_size(None)),+ caches.intrinsic_inline_size_measurement_cache_get(dependent.slot, key_at_block_size(None)),Some(measured_without_block_size));assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(dependent_stamp(), key_at_block_size(Some(80))),+ caches.intrinsic_inline_size_measurement_cache_get(dependent.slot, key_at_block_size(Some(80))),None);assert_eq!(- caches.intrinsic_inline_size_measurement_cache_get(dependent_stamp(), key_at_block_size(Some(50))),+ caches.intrinsic_inline_size_measurement_cache_get(dependent.slot, key_at_block_size(Some(50))),Some(dependent_measurement));diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rsindex 89186b9f0ff..c4c25b44fdf 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs@@ -230,11 +230,6 @@ impl<'arena> LayoutPass<'arena> {self.intrinsic_size_caches}- /// What an intrinsic size cache entry measured for `node` now is valid for.- pub(crate) fn intrinsic_size_cache_stamp(&self, node: Node) -> super::layout_node_arena::IntrinsicSizeCacheStamp {- self.arena.intrinsic_size_cache_stamp(node)- }-#[inline]pub(crate) fn node_data(&self, node: Node) -> &'arena NodeData {self.arena().data(node)diff --git a/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rsindex 7f33804e2a0..dd5dabaa2b8 100644--- a/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs+++ b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs@@ -4,9 +4,9 @@* SPDX-License-Identifier: BSD-2-Clause*/-//! What the arena's writes leave the layout stage's run memos to drop or reconsider. The memos live in the stage's-//! scratch, which the arena cannot reach, so the arena notes what a write did to a box and the stage applies the notes-//! to its memos before a layout pass reads them and before it sweeps them.+//! What the arena's writes leave the layout stage's memos to drop or reconsider. The memos live in the stage's scratch,+//! which the arena cannot reach, so the arena notes what a write did to a box and the stage applies the notes to its+//! memos before a layout pass reads them and before it sweeps them.//!//! The notes of a box coalesce. Only a layout pass stores memo entries, and a stage applies the notes before each pass,//! so between two applications a box's entries change only through the notes: entries a note drops stay dropped, and@@ -14,6 +14,7 @@use super::fc_run_cache::FcRunCacheStore;use super::in_flow_block_cache::InFlowBlockCacheStore;+use super::layout_node_arena::IntrinsicSizeCaches;use super::node_data::NodeSlotId;use super::rendered_text::TextMemos;@@ -23,6 +24,7 @@ struct SlotNotes {text_memos_dropped: bool,run_cache_entries_dropped: bool,in_flow_block_entries_dropped: bool,+ intrinsic_sizes_dropped: bool,fragment_cache_epoch_bumped: bool,structural_inline_changes: u32,generation: u8,@@ -63,6 +65,12 @@ impl MemoInvalidations {let notes = self.notes_of(slot);notes.text_memos_dropped = true;notes.in_flow_block_entries_dropped = true;+ notes.intrinsic_sizes_dropped = true;+ }++ /// Something the intrinsic sizes of the box in `slot` were measured from changed.+ pub(crate) fn note_intrinsic_sizes_dropped(&mut self, slot: u32) {+ self.notes_of(slot).intrinsic_sizes_dropped = true;}/// The text row in `slot` got other rendered text, which the chunks and segmenter made of the old text no longer@@ -97,6 +105,7 @@ impl MemoInvalidations {run_cache: &FcRunCacheStore,in_flow_block_cache: &InFlowBlockCacheStore,text_memos: &TextMemos,+ intrinsic_size_caches: &IntrinsicSizeCaches,) {for slot in self.noted_slots {let notes = self.slots[slot as usize];@@ -117,6 +126,9 @@ impl MemoInvalidations {if notes.text_memos_dropped {text_memos.forget(slot);}+ if notes.intrinsic_sizes_dropped {+ intrinsic_size_caches.drop_entries_of_slot(slot);+ }}}}diff --git a/Libraries/LibWeb/Rust/src/layout/node_data.rs b/Libraries/LibWeb/Rust/src/layout/node_data.rsindex b5302758c8e..25359c03d34 100644--- a/Libraries/LibWeb/Rust/src/layout/node_data.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_data.rs@@ -276,10 +276,9 @@ pub(crate) struct NodeData {pub next_sibling: ShapeCell<NodeSlotId>,pub kind: ShapeCell<NodeKind>,pub generated_for: ShapeCell<u8>,- pub intrinsic_cache_epoch: u16,pub flags: ShapeCell<u32>,/// Advanced on every layout invalidation that reaches this node or its- /// subtree, with no propagation boundary: unlike the intrinsic epoch,+ /// subtree, with no propagation boundary: unlike intrinsic size resets,/// changes inside absolutely positioned and SVG descendants must reach/// every ancestor, because their fragments live in ancestor run trees./// Wide enough that wrapping between a cache store and the next probe@@ -304,7 +303,6 @@ impl Default for NodeData {next_sibling: ShapeCell::new(NodeSlotId::INVALID),kind: ShapeCell::new(NodeKind::Unset),generated_for: ShapeCell::new(0),- intrinsic_cache_epoch: 0,flags: ShapeCell::new(0),slot_generation: ShapeCell::new(0),compositor_animation_frame_kinds: ShapeCell::new(0),@@ -321,7 +319,6 @@ impl Default for NodeData {/// The facts of a node that no publication carries, for writing: writing them marks nothing for the paint side,/// unlike a write to the node's shape.pub(crate) struct UnpublishedNodeFactsMut<'a> {- pub(crate) intrinsic_cache_epoch: &'a mut u16,pub(crate) fragment_cache_epoch: &'a mut u32,pub(crate) table_column_span: &'a mut u16,pub(crate) table_row_span: &'a mut u16,@@ -331,7 +328,6 @@ pub(crate) struct UnpublishedNodeFactsMut<'a> {impl NodeData {pub(super) fn unpublished_facts_mut(&mut self) -> UnpublishedNodeFactsMut<'_> {UnpublishedNodeFactsMut {- intrinsic_cache_epoch: &mut self.intrinsic_cache_epoch,fragment_cache_epoch: &mut self.fragment_cache_epoch,table_column_span: &mut self.table_column_span,table_row_span: &mut self.table_row_span,@@ -434,9 +430,8 @@ mod tests {}#[test]- fn intrinsic_cache_epoch_uses_existing_node_data_padding() {+ fn node_data_keeps_its_size_and_field_offsets() {assert_eq!(std::mem::size_of::<NodeData>(), 48);- assert_eq!(std::mem::offset_of!(NodeData, intrinsic_cache_epoch), 22);assert_eq!(std::mem::offset_of!(NodeData, flags), 24);assert_eq!(std::mem::offset_of!(NodeData, fragment_cache_epoch), 28);assert_eq!(std::mem::offset_of!(NodeData, slot_generation), 32);diff --git a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rsindex 09670129e24..2bf800c75e0 100644--- a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs+++ b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs@@ -837,8 +837,12 @@ pub(crate) struct FfiPartialRelayoutHostFacts {mod tests {use super::FfiPartialRelayoutHostFacts;use crate::css::style::tree::StyleNodeID;- use crate::layout::layout_node_arena::{LayoutNodeArena, NodeAllocation};+ use crate::layout::CssPixels;+ use crate::layout::layout_node_arena::{+ FirstLineMaxContent, IntrinsicInlineSizeMeasurement, IntrinsicSizeCacheKey, LayoutNodeArena, NodeAllocation,+ };use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};+ use crate::layout::run_state::LayoutScratch;fn allocate_box_with_a_dummy_shell(arena: &mut LayoutNodeArena) -> NodeAllocation {let allocation = arena.allocate_for_test();@@ -905,23 +909,49 @@ mod tests {arena.data(allocation.slot).flags.get() & NodeFlag::NeedsLayoutUpdate as u32 != 0}- fn intrinsic_cache_epoch(arena: &LayoutNodeArena, allocation: &NodeAllocation) -> u16 {- arena.data(allocation.slot).intrinsic_cache_epoch+ fn store_intrinsic_sizes(scratch: &LayoutScratch, allocations: &[&NodeAllocation]) {+ for allocation in allocations {+ scratch+ .intrinsic_size_caches+ .intrinsic_inline_size_measurement_cache_put(+ allocation.slot,+ IntrinsicSizeCacheKey::default(),+ IntrinsicInlineSizeMeasurement {+ min_content_inline_size: CssPixels::from_raw(10),+ max_content_inline_size: CssPixels::from_raw(20),+ first_line_max_content: FirstLineMaxContent::default(),+ depends_on_percentage_block_size: false,+ depends_on_percentage_inline_basis: false,+ depends_on_block_size: false,+ },+ );+ }+ }++ fn intrinsic_sizes_are_kept(scratch: &LayoutScratch, allocation: &NodeAllocation) -> bool {+ scratch+ .intrinsic_size_caches+ .intrinsic_inline_size_measurement_cache_get(allocation.slot, IntrinsicSizeCacheKey::default())+ .is_some()}#[test]- fn marking_a_node_marks_clean_ancestors_and_bumps_their_intrinsic_epochs() {+ fn marking_a_node_marks_clean_ancestors_and_drops_their_intrinsic_sizes() {let mut arena = LayoutNodeArena::new();+ let scratch = LayoutScratch::default();let parent = allocate_box_with_a_dummy_shell(&mut arena);let child = allocate_box_with_a_dummy_shell(&mut arena);arena.insert_child(parent.slot, child.slot, NodeSlotId::INVALID);+ scratch.apply_memo_invalidations(&mut arena);+ store_intrinsic_sizes(&scratch, &[&parent, &child]);arena.set_needs_layout_update(child.slot, true);+ scratch.apply_memo_invalidations(&mut arena);assert!(node_is_dirty(&arena, &child));assert!(node_is_dirty(&arena, &parent));- assert_eq!(intrinsic_cache_epoch(&arena, &child), 1);- assert_eq!(intrinsic_cache_epoch(&arena, &parent), 1);+ assert!(!intrinsic_sizes_are_kept(&scratch, &child));+ assert!(!intrinsic_sizes_are_kept(&scratch, &parent));free_node(&mut arena, &parent);}@@ -945,15 +975,19 @@ mod tests {#[test]fn remarking_a_dirty_non_boundary_node_leaves_its_parent_clean() {let mut arena = LayoutNodeArena::new();+ let scratch = LayoutScratch::default();let parent = allocate_box_with_a_dummy_shell(&mut arena);let child = allocate_box_with_a_dummy_shell(&mut arena);arena.insert_child(parent.slot, child.slot, NodeSlotId::INVALID);arena.set_node_flag(child.slot, NodeFlag::NeedsLayoutUpdate, true);+ scratch.apply_memo_invalidations(&mut arena);+ store_intrinsic_sizes(&scratch, &[&parent]);arena.set_needs_layout_update(child.slot, true);+ scratch.apply_memo_invalidations(&mut arena);assert!(!node_is_dirty(&arena, &parent));- assert_eq!(intrinsic_cache_epoch(&arena, &parent), 0);+ assert!(intrinsic_sizes_are_kept(&scratch, &parent));free_node(&mut arena, &parent);}diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rsindex 80ea0aaba29..8806124f29d 100644--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs@@ -135,6 +135,7 @@ impl LayoutScratch {&self.fc_run_cache_store,&self.in_flow_block_cache_store,&self.text_memos,+ &self.intrinsic_size_caches,);}@@ -347,10 +348,6 @@ impl RunState {content_sizing: ContentSizing,run: impl for<'run> FnOnce(&'run Self, LayoutPass<'run>) -> R,) -> R {- debug_assert!(- !callbacks.arena().has_intrinsic_size_cache_drops(),- "the layout stage drops released intrinsic size cache entries before its runs read a cache"- );let state = Self {root,content_sizing,diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rsindex 2ea9259b91f..f762cddbdb2 100644--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs@@ -326,7 +326,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {pub(crate) fn intrinsic_inline_size_may_depend_on_block_size(&self, root: Node) -> bool {self.callbacks.intrinsic_size_caches()- .intrinsic_inline_size_depends_on_block_size(self.callbacks.intrinsic_size_cache_stamp(root), || {+ .intrinsic_inline_size_depends_on_block_size(root, || {let mut pending = vec![root];while let Some(node) = pending.pop() {let facts = self.facts(node);@@ -2530,11 +2530,10 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {key: IntrinsicSizeCacheKey,) -> Option<IntrinsicBlockSizeMeasurement> {let key = self.key_in_frame(key);- let measurement = self.callbacks.intrinsic_size_caches().intrinsic_block_size_cache_get(- self.callbacks.intrinsic_size_cache_stamp(node),- kind,- key,- )?;+ let measurement = self+ .callbacks+ .intrinsic_size_caches()+ .intrinsic_block_size_cache_get(node, kind, key)?;self.charge_measurement_dependency_to_measured_box_and_containing_block(node,measurement.depends_on_percentage_block_size,@@ -2550,12 +2549,9 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {value: IntrinsicBlockSizeMeasurement,) {let key = self.key_in_frame(key);- self.callbacks.intrinsic_size_caches().intrinsic_block_size_cache_put(- self.callbacks.intrinsic_size_cache_stamp(node),- kind,- key,- value,- );+ self.callbacks+ .intrinsic_size_caches()+ .intrinsic_block_size_cache_put(node, kind, key, value);}fn intrinsic_inline_measurement_cache_get(@@ -2572,7 +2568,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {let measurement = self.callbacks.intrinsic_size_caches()- .intrinsic_inline_size_measurement_cache_get(self.callbacks.intrinsic_size_cache_stamp(node), key)?;+ .intrinsic_inline_size_measurement_cache_get(node, key)?;self.charge_measurement_dependency_to_measured_box_and_containing_block(node,measurement.depends_on_percentage_block_size,@@ -2589,7 +2585,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {let key = self.key_in_frame(key);self.callbacks.intrinsic_size_caches()- .intrinsic_inline_size_measurement_cache_put(self.callbacks.intrinsic_size_cache_stamp(node), key, value);+ .intrinsic_inline_size_measurement_cache_put(node, key, value);}fn transferred_intrinsic_inline_sizing_space(&self, sized: SizedBox<'_>) -> AvailableSpace {