Skip to content

Truly independent web browser

LibWeb: Keep the layout run caches in the layout stage's scratch

Aliaksandr Kalenik421fd429f58a
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index f6021d3e30f..5b56d83b0d0 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3181,7 +3181,7 @@ impl<'pass> BlockFormattingContext<'pass> {
.arena()
.with_current_committed_fragment(node, |fragment| fragment.clone())
.and_then(|fragment| {
- let entry = callbacks.arena().in_flow_block_cache_store().matching(
+ let entry = callbacks.in_flow_block_cache_store().matching(
node.slot_index(),
validity,
&key,
@@ -3311,7 +3311,8 @@ impl<'pass> BlockFormattingContext<'pass> {
fragment_identity: placed.fragment.identity,
};
let committed_identity = arena.with_current_committed_fragment(node, |fragment| fragment.identity);
- arena
+ self.run
+ .callbacks
.in_flow_block_cache_store()
.store(node.slot_index(), entry, committed_identity);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index b95ec41746a..d4d8b3191cb 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -484,10 +484,11 @@ struct FcRunCacheSlot {
type EarlierEntriesOfBox = smallvec::SmallVec<[std::sync::Arc<FcRunCacheEntry>; EARLIER_ENTRIES_PER_BOX]>;
-/// Per-document store of completed run results, surviving across layout passes on the node arena: per box the latest
-/// entry, the one whose fragments the box committed where a later store moved it aside, and a few earlier ones.
+/// Per-document store of completed run results, surviving across layout passes in the layout stage's scratch: per box
+/// the latest entry, the one whose fragments the box committed where a later store moved it aside, and a few earlier
+/// ones.
#[derive(Clone, Default)]
-pub(crate) struct FcRunCacheArenaStore {
+pub(crate) struct FcRunCacheStore {
slots: RefCell<Vec<FcRunCacheSlot>>,
earlier_entries: RefCell<HashMap<u32, EarlierEntriesOfBox>>,
inline_layout_damage: RefCell<Vec<InlineLayoutDamage>>,
@@ -495,7 +496,7 @@ pub(crate) struct FcRunCacheArenaStore {
enrolled_for_sweep: RefCell<Vec<bool>>,
}
-impl FcRunCacheArenaStore {
+impl FcRunCacheStore {
pub(super) fn enqueue_for_sweep(&self, slot: u32) {
let mut enrolled = self.enrolled_for_sweep.borrow_mut();
if enrolled.len() <= slot as usize {
@@ -824,7 +825,7 @@ impl FcRunCacheAttempt {
) {
return Ok(Self::Bypass);
}
- let store = callbacks.arena().fc_run_cache_store();
+ let store = callbacks.fc_run_cache_store();
let validity = run_root_validity(callbacks, box_);
if !builds_fragments {
if let Some(entry) = store.first_entry_where(box_.slot_index(), |entry| {
@@ -887,7 +888,7 @@ impl FcRunCacheAttempt {
outputs.result.automatic_content_block_size,
)
.map(|block_size| clamp_to_max_dimension_value(block_size.max(CssPixels::default())));
- let store = callbacks.arena().fc_run_cache_store();
+ let store = callbacks.fc_run_cache_store();
let (validity, structurally_damaged_entry) = match self {
Self::Bypass => return,
Self::StoreResultOnly { validity } => {
@@ -1098,7 +1099,7 @@ mod tests {
.collect()
}
- fn widths_of_entries(store: &FcRunCacheArenaStore, slot: u32) -> Vec<AvailableSize> {
+ fn widths_of_entries(store: &FcRunCacheStore, slot: u32) -> Vec<AvailableSize> {
let mut widths = Vec::new();
store.first_entry_where(slot, |entry| {
widths.push(entry.key.input.available_space.inline_size);
@@ -1107,7 +1108,7 @@ mod tests {
widths
}
- fn width_of_committed_entry(store: &FcRunCacheArenaStore, slot: u32) -> Option<AvailableSize> {
+ fn width_of_committed_entry(store: &FcRunCacheStore, slot: u32) -> Option<AvailableSize> {
store.slots.borrow()[slot as usize]
.committed
.as_ref()
@@ -1120,7 +1121,7 @@ mod tests {
#[test]
fn sweep_checks_changed_entries_without_discarding_reusable_stale_data_early() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let entry = |epoch| entry(at(300), epoch, None, 0);
store.store(Node::new(0, 1), entry(1));
store.store(Node::new(1, 1), entry(1));
@@ -1157,7 +1158,7 @@ mod tests {
#[test]
fn a_store_keeps_the_entry_whose_fragments_the_box_committed_beside_newer_ones() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
store.sweep_pending_entries(|_, _| true, mark_entries_of_key(at(300)), 1);
@@ -1176,7 +1177,7 @@ mod tests {
#[test]
fn the_sweep_marks_the_entry_whose_fragments_the_box_committed_and_drops_its_descendant_fragments() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
store.store(box_, entry(at(320), 1, Some(root_with_a_descendant()), 0));
@@ -1212,7 +1213,7 @@ mod tests {
#[test]
fn an_earlier_entry_committed_in_place_of_the_marked_latest_one_stays_beside_later_stores() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
store.store(box_, entry(at(320), 1, Some(root_with_a_descendant()), 0));
@@ -1237,7 +1238,7 @@ mod tests {
#[test]
fn a_committed_entry_that_no_longer_describes_the_committed_subtree_goes() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
store.sweep_pending_entries(|_, _| true, mark_entries_of_key(at(300)), 1);
@@ -1249,7 +1250,7 @@ mod tests {
#[test]
fn entries_holding_fragments_the_box_never_committed_go_after_two_unused_layout_commits_beside_one_it_did() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(280), 1, Some(root_with_a_descendant()), 0));
store.sweep_pending_entries(|_, _| true, mark_entries_of_key(at(280)), 1);
@@ -1275,7 +1276,7 @@ mod tests {
#[test]
fn the_latest_entry_of_a_box_that_committed_none_of_its_entries_stays() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
for layout_commit_count in 1..5 {
@@ -1286,7 +1287,7 @@ mod tests {
#[test]
fn earlier_entries_make_room_oldest_first_and_results_of_runs_without_fragments_first() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(100), 1, Some(root_with_a_descendant()), 0));
store.store(box_, entry(at(200), 1, None, 0));
@@ -1301,9 +1302,39 @@ mod tests {
assert_eq!(widths_of_entries(&store, 0), widths(&[400, 600, 500, 300]));
}
+ #[test]
+ fn queued_memo_invalidations_count_structural_changes_only_while_the_box_holds_entries() {
+ use crate::layout::in_flow_block_cache::InFlowBlockCacheStore;
+ use crate::layout::memo_invalidations::MemoInvalidations;
+ let store = FcRunCacheStore::default();
+ let in_flow_block_cache = InFlowBlockCacheStore::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);
+ assert_eq!(store.structural_epoch_bumps_of(node), 0);
+
+ store.store(node, entry(at(100), 1, Some(root_with_a_descendant()), 0));
+ let mut notes = MemoInvalidations::default();
+ 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);
+ assert_eq!(store.structural_epoch_bumps_of(node), 2);
+
+ let reused = Node::new(0, 2);
+ let mut notes = MemoInvalidations::default();
+ 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);
+ assert!(!store.slot_holds_entries(0));
+ assert_eq!(store.structural_epoch_bumps_of(reused), 0);
+ }
+
#[test]
fn a_store_keeps_the_inline_damage_an_earlier_entry_saw() {
- let store = FcRunCacheArenaStore::default();
+ let store = FcRunCacheStore::default();
let box_ = Node::new(0, 1);
store.store(box_, entry(at(300), 1, Some(root_with_a_descendant()), 0));
store.note_inline_layout_damage(box_);
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index f9e92e33e75..f7a28aaf65a 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -3524,8 +3524,7 @@ impl LayoutStageJob {
}
notifications.prepend_layout_messages(messages);
let arena = state.arena();
- arena.end_layout_pass();
- state.layout_scratch().end_layout_pass();
+ state.layout_scratch().end_layout_pass(arena);
arena.reset_layout_update_flags_in_subtree(root);
arena.leave_active_layout_pass();
match kind {
@@ -3660,6 +3659,7 @@ fn prepare_layout_stage(state: &mut super::ArenaHandle, kind: LayoutStageKind, r
}
}
drop_intrinsic_size_cache_entries_of_released_slots(arena, state.layout_scratch());
+ state.layout_scratch().apply_memo_invalidations(arena);
arena.begin_active_layout_pass();
}
diff --git a/Libraries/LibWeb/Rust/src/layout/host_tables.rs b/Libraries/LibWeb/Rust/src/layout/host_tables.rs
index 13d0cdbf8fb..41dcc064d07 100644
--- a/Libraries/LibWeb/Rust/src/layout/host_tables.rs
+++ b/Libraries/LibWeb/Rust/src/layout/host_tables.rs
@@ -165,7 +165,7 @@ impl ArenaHandle {
pub(crate) fn fork(&self, engine: crate::css::style::StyleEngineHandle) -> Self {
Self {
arena: self.arena.fork(engine),
- layout_scratch: Default::default(),
+ layout_scratch: self.layout_scratch.fork(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
index eb5804432b1..7c686cd4c28 100644
--- a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
@@ -81,7 +81,7 @@ struct InFlowBlockSlot {
committed: Option<std::sync::Arc<InFlowBlockEntry>>,
}
-/// Per-document store of in-flow block layouts, surviving across layout passes on the node arena.
+/// Per-document store of in-flow block layouts, surviving across layout passes in the layout stage's scratch.
#[derive(Clone, Default)]
pub(crate) struct InFlowBlockCacheStore {
entries: RefCell<Vec<InFlowBlockSlot>>,
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index f4e250b0e06..1292acfca54 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1241,8 +1241,8 @@ pub(crate) struct LayoutNodeArena {
/// is the only one built for its node links to nothing. The chain lives on the rows rather than
/// under a key so that it survives the node's identity being retired and re-issued.
next_rows_built_for_same_node: Vec<Cell<NodeSlotId>>,
- fc_run_cache_store: super::fc_run_cache::FcRunCacheArenaStore,
- in_flow_block_cache_store: super::in_flow_block_cache::InFlowBlockCacheStore,
+ /// What writes since the layout stage last looked did to the boxes its run memos hold entries of.
+ memo_invalidations: RefCell<super::memo_invalidations::MemoInvalidations>,
pub(super) layout_trace: super::trace::LayoutTrace,
pub(crate) paintable_rows: crate::painting::paintable_rows::PaintableRowStore,
paint_state: RefCell<crate::painting::paint_state::PaintState>,
@@ -1357,8 +1357,7 @@ impl LayoutNodeArena {
layer_image_paint_facts: RefCell::default(),
svg_paint_resources: crate::painting::svg_paint_resources::SvgPaintResources::default(),
next_rows_built_for_same_node: Vec::new(),
- fc_run_cache_store: super::fc_run_cache::FcRunCacheArenaStore::default(),
- in_flow_block_cache_store: super::in_flow_block_cache::InFlowBlockCacheStore::default(),
+ memo_invalidations: RefCell::default(),
layout_trace: super::trace::LayoutTrace::default(),
paintable_rows: crate::painting::paintable_rows::PaintableRowStore::default(),
paint_state: RefCell::new(crate::painting::paint_state::PaintState::default()),
@@ -1437,24 +1436,17 @@ impl LayoutNodeArena {
}
}
- pub(crate) fn fc_run_cache_store(&self) -> &super::fc_run_cache::FcRunCacheArenaStore {
- &self.fc_run_cache_store
+ /// What writes did to the boxes the layout stage's run memos hold entries of since the stage last took it.
+ pub(crate) fn take_memo_invalidations(&self) -> super::memo_invalidations::MemoInvalidations {
+ self.memo_invalidations.take()
}
- pub(crate) fn in_flow_block_cache_store(&self) -> &super::in_flow_block_cache::InFlowBlockCacheStore {
- &self.in_flow_block_cache_store
- }
-
- pub(crate) fn end_layout_pass(&self) {
- self.sweep_stale_fc_run_cache_entries();
- }
-
- /// Drops entries whose slot or epoch no longer matches, marks the entry whose fragments each box
- /// committed and ages the others. Checks only boxes whose entries were invalidated, stored or used
- /// since the previous sweep, or can still age out. Stale entries survive until commit so inline
- /// layout can reuse their undamaged line prefixes.
- fn sweep_stale_fc_run_cache_entries(&self) {
- self.fc_run_cache_store.sweep_pending_entries(
+ /// Drops the entries of `store` whose slot or epoch no longer matches, marks the entry whose fragments each box
+ /// committed and ages the others. Checks only boxes whose entries were invalidated, stored or used since the
+ /// previous sweep, or can still age out. Stale entries survive until commit so inline layout can reuse their
+ /// undamaged line prefixes.
+ pub(crate) fn sweep_stale_fc_run_cache_entries(&self, store: &super::fc_run_cache::FcRunCacheStore) {
+ store.sweep_pending_entries(
|slot, validity| {
let Some(metadata) = self.slot_metadata.get(slot as usize) else {
return false;
@@ -1755,8 +1747,7 @@ impl LayoutNodeArena {
}
self.owned_image_natural_sizes.get_mut().remove(&id);
self.owned_image_providers.get_mut().remove(&id);
- self.fc_run_cache_store.remove_entry(index);
- self.in_flow_block_cache_store.remove_entry(index);
+ self.memo_invalidations.get_mut().note_slot_freed(index);
self.remove_layout_update_flag_node(id);
self.math_layout_kinds.get_mut().remove(&id);
if self.replaced_paint_facts.get_mut().contains_key(&id) {
@@ -5223,7 +5214,9 @@ impl LayoutNodeArena {
pub(crate) fn clear_committed_fragment_link(&self, id: NodeSlotId) {
// Cached runs may reuse the committed paintable subtree without replaying its fragments.
// Once that subtree is cleared, a later layout must rebuild it instead.
- self.fc_run_cache_store.remove_entry(id.slot_index());
+ self.memo_invalidations
+ .borrow_mut()
+ .note_run_cache_entries_dropped(id.slot_index());
self.note_subtrees_of_self_and_ancestors_committed_anew(id);
drop(self.take_committed_fragment_link(self.data(id)));
}
@@ -5590,7 +5583,7 @@ impl LayoutNodeArena {
/// The part of `invalidate_at_and_above` that applies to one node, for walks that also do other work on the way up.
pub(super) fn invalidate_one(&self, node: NodeSlotId, invalidation: AncestorInvalidation) {
if invalidation == AncestorInvalidation::StructuralChange {
- self.fc_run_cache_store.note_inline_layout_damage(node);
+ self.memo_invalidations.borrow_mut().note_structural_inline_change(node);
}
self.bump_fragment_cache_epoch(node);
if super::node_facts::kind_is_box(self.data(node).kind.get()) {
@@ -5609,9 +5602,9 @@ impl LayoutNodeArena {
!self.layout_pass_is_running(),
"a node's committed fragments became stale while a layout pass ran"
);
- self.fc_run_cache_store.note_invalidated_entry(node);
- // NB: An in-flow block entry is valid for one epoch only, so it never matches again.
- self.in_flow_block_cache_store.remove_entry(node.slot_index());
+ self.memo_invalidations
+ .borrow_mut()
+ .note_fragment_cache_epoch_bumped(node);
}
pub(crate) fn note_structural_change_at_and_above(&self, node: NodeSlotId) {
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 01437616252..94cda70202c 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -166,6 +166,16 @@ impl<'arena> LayoutPass<'arena> {
None
}
+ /// 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 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()
+ }
+
/// 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 {
diff --git a/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs
new file mode 100644
index 00000000000..054330494a7
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * 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.
+//!
+//! 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
+//! the notes after it see none.
+
+use super::fc_run_cache::FcRunCacheStore;
+use super::in_flow_block_cache::InFlowBlockCacheStore;
+use super::node_data::NodeSlotId;
+
+#[derive(Clone, Copy, Default)]
+struct SlotNotes {
+ noted: bool,
+ run_cache_entries_dropped: bool,
+ in_flow_block_entries_dropped: bool,
+ fragment_cache_epoch_bumped: bool,
+ structural_inline_changes: u32,
+ generation: u8,
+}
+
+/// The notes of each box the arena wrote since the stage last applied them, in the order the boxes were first noted.
+#[derive(Clone, Default)]
+pub(crate) struct MemoInvalidations {
+ slots: Vec<SlotNotes>,
+ noted_slots: Vec<u32>,
+}
+
+impl MemoInvalidations {
+ fn notes_of(&mut self, slot: u32) -> &mut SlotNotes {
+ let index = slot as usize;
+ if self.slots.len() <= index {
+ self.slots.resize_with(index + 1, SlotNotes::default);
+ }
+ let notes = &mut self.slots[index];
+ if !notes.noted {
+ notes.noted = true;
+ self.noted_slots.push(slot);
+ }
+ notes
+ }
+
+ /// The box in `slot` left its committed subtree or the arena, which drops its run cache entries.
+ pub(crate) fn note_run_cache_entries_dropped(&mut self, slot: u32) {
+ let notes = self.notes_of(slot);
+ notes.run_cache_entries_dropped = true;
+ notes.fragment_cache_epoch_bumped = false;
+ notes.structural_inline_changes = 0;
+ }
+
+ /// The box in `slot` left the arena, which drops all its memo entries.
+ pub(crate) fn note_slot_freed(&mut self, slot: u32) {
+ self.note_run_cache_entries_dropped(slot);
+ self.notes_of(slot).in_flow_block_entries_dropped = true;
+ }
+
+ /// The fragment cache epoch of `node` moved on: its run cache entries need the sweep, and its in-flow block entry,
+ /// valid for one epoch only, is dropped.
+ pub(crate) fn note_fragment_cache_epoch_bumped(&mut self, node: NodeSlotId) {
+ let notes = self.notes_of(node.slot_index());
+ notes.in_flow_block_entries_dropped = true;
+ if !notes.run_cache_entries_dropped {
+ notes.fragment_cache_epoch_bumped = true;
+ notes.generation = node.generation();
+ }
+ }
+
+ /// The child list of `node` changed, which run cache entries holding line data count.
+ pub(crate) fn note_structural_inline_change(&mut self, node: NodeSlotId) {
+ let notes = self.notes_of(node.slot_index());
+ if !notes.run_cache_entries_dropped {
+ notes.structural_inline_changes += 1;
+ notes.generation = node.generation();
+ }
+ }
+
+ /// Applies the notes to the memos, in the order the boxes were first noted.
+ pub(crate) fn apply_to(self, run_cache: &FcRunCacheStore, in_flow_block_cache: &InFlowBlockCacheStore) {
+ for slot in self.noted_slots {
+ let notes = self.slots[slot as usize];
+ if notes.run_cache_entries_dropped {
+ run_cache.remove_entry(slot);
+ } else if notes.structural_inline_changes > 0 || notes.fragment_cache_epoch_bumped {
+ let node = NodeSlotId::new(slot, notes.generation);
+ for _ in 0..notes.structural_inline_changes {
+ run_cache.note_inline_layout_damage(node);
+ }
+ if notes.fragment_cache_epoch_bumped {
+ run_cache.note_invalidated_entry(node);
+ }
+ }
+ if notes.in_flow_block_entries_dropped {
+ in_flow_block_cache.remove_entry(slot);
+ }
+ }
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index e7e6cfc15fe..cf8c90a76cd 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -37,6 +37,7 @@ pub(crate) mod line_box;
pub(crate) mod line_box_fragment;
pub(crate) mod line_builder;
pub(crate) mod math_formatting_context;
+mod memo_invalidations;
pub mod node_data;
pub(crate) mod node_facts;
pub(crate) mod tree_shape;
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index fd8cdfaf11c..5d54d9e82e1 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -17,6 +17,10 @@ pub(crate) struct LayoutScratch {
inline_item_stashes: RefCell<HashMap<NodeSlotId, super::inline_level_iterator::StashedInlineItems>>,
/// What the pass found out for the host outside any run, which its driver reports itself.
pub(super) host_findings_outside_runs: RefCell<HostFindings>,
+ /// The results of formatting context runs, kept for the runs after them in this pass and the passes after it.
+ fc_run_cache_store: super::fc_run_cache::FcRunCacheStore,
+ /// The layouts of in-flow blocks, kept for the passes after the one that laid them out.
+ in_flow_block_cache_store: super::in_flow_block_cache::InFlowBlockCacheStore,
}
/// What layout found out that the host hears of once the layout that found it is committed: what the
@@ -88,11 +92,38 @@ impl Default for LayoutScratch {
intrinsic_size_caches: Default::default(),
inline_item_stashes: RefCell::new(HashMap::default()),
host_findings_outside_runs: RefCell::new(HostFindings::default()),
+ fc_run_cache_store: Default::default(),
+ in_flow_block_cache_store: Default::default(),
}
}
}
impl LayoutScratch {
+ /// The scratch of a fork of the arena, which keeps the run memos the arena's entries were stored for and starts
+ /// the rest anew.
+ pub(super) fn fork(&self) -> Self {
+ Self {
+ fc_run_cache_store: self.fc_run_cache_store.clone(),
+ in_flow_block_cache_store: self.in_flow_block_cache_store.clone(),
+ ..Self::default()
+ }
+ }
+
+ pub(super) fn fc_run_cache_store(&self) -> &super::fc_run_cache::FcRunCacheStore {
+ &self.fc_run_cache_store
+ }
+
+ pub(super) fn in_flow_block_cache_store(&self) -> &super::in_flow_block_cache::InFlowBlockCacheStore {
+ &self.in_flow_block_cache_store
+ }
+
+ /// Brings the run memos up to date with what the arena's writes since the last call did to their boxes.
+ pub(super) fn apply_memo_invalidations(&self, arena: &LayoutNodeArena) {
+ arena
+ .take_memo_invalidations()
+ .apply_to(&self.fc_run_cache_store, &self.in_flow_block_cache_store);
+ }
+
pub(super) fn store_inline_item_stash(
&self,
block_container: NodeSlotId,
@@ -113,8 +144,10 @@ impl LayoutScratch {
std::mem::take(&mut self.host_findings_outside_runs.borrow_mut())
}
- /// Lets go of what the pass that just committed kept for itself.
- pub(crate) fn end_layout_pass(&self) {
+ /// Lets go of what the pass that just committed into `arena` kept for itself, and sweeps the run cache.
+ pub(crate) fn end_layout_pass(&self, arena: &LayoutNodeArena) {
+ self.apply_memo_invalidations(arena);
+ arena.sweep_stale_fc_run_cache_entries(&self.fc_run_cache_store);
self.inline_item_stashes.borrow_mut().clear();
}
}