Skip to content

Truly independent web browser

LibWeb: Memoize live absolute rects in the view that reads them

Aliaksandr Kalenika768a78f22c1
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_read.rs b/Libraries/LibWeb/Rust/src/painting/paint_read.rs
index 9cc63a08bbb..dcfea61f7b4 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_read.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_read.rs
@@ -356,9 +356,56 @@ impl AsRef<LayoutNodeArena> for LayoutNodeArena {
}
}
+/// Where a live read keeps the absolute rects it computed. A view of the rows keeps its own, each valid for the
+/// geometry epoch of the arena it was computed at; the bare arena keeps none, as a shared borrow of it writes nothing.
+pub(crate) trait LiveAbsoluteRects {
+ fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect>;
+ fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect);
+}
+
+impl LiveAbsoluteRects for LayoutNodeArena {
+ fn memoized_live_absolute_rect(&self, _: NodeSlotId) -> Option<CssPixelRect> {
+ None
+ }
+
+ fn memoize_live_absolute_rect(&self, _: NodeSlotId, _: CssPixelRect) {}
+}
+
+impl<Arena: Deref<Target = LayoutNodeArena>> LiveAbsoluteRects
+ for crate::painting::paintable_rows::PaintableRows<Arena>
+{
+ fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
+ self.memoized_absolute_rect_of_view(id)
+ }
+
+ fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
+ self.memoize_absolute_rect_of_view(id, rect);
+ }
+}
+
+impl<Reader: LiveAbsoluteRects + ?Sized> LiveAbsoluteRects for &Reader {
+ fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
+ (**self).memoized_live_absolute_rect(id)
+ }
+
+ fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
+ (**self).memoize_live_absolute_rect(id, rect);
+ }
+}
+
+impl<Reader: LiveAbsoluteRects + ?Sized> LiveAbsoluteRects for &mut Reader {
+ fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
+ (**self).memoized_live_absolute_rect(id)
+ }
+
+ fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
+ (**self).memoize_live_absolute_rect(id, rect);
+ }
+}
+
// The live arena, and every view that borrows it, answers each read with the arena's own method of
// the same name.
-impl<Live: AsRef<LayoutNodeArena>> GeometryRead for Live {
+impl<Live: AsRef<LayoutNodeArena> + LiveAbsoluteRects> GeometryRead for Live {
type Row<'a>
= crate::layout::LiveRow<'a>
where
@@ -385,15 +432,15 @@ impl<Live: AsRef<LayoutNodeArena>> GeometryRead for Live {
}
fn memoized_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
- self.as_ref().memoized_absolute_rect(id)
+ self.memoized_live_absolute_rect(id)
}
fn memoize_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
- self.as_ref().memoize_absolute_rect(id, rect);
+ self.memoize_live_absolute_rect(id, rect);
}
}
-impl<Live: AsRef<LayoutNodeArena>> PaintRead for Live {
+impl<Live: AsRef<LayoutNodeArena> + LiveAbsoluteRects> PaintRead for Live {
fn rendered_text(&self, id: NodeSlotId) -> Option<&RenderedText> {
self.as_ref().text_content(id).map(|content| &**content)
}
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
index e6eaf667e64..c215b533e63 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
@@ -106,12 +106,12 @@ impl<'a> PaintableCommit<'a> {
self.arena
}
- pub(crate) fn discard_absolute_rects_memoized_during_commit(&self) {
- self.arena().clear_absolute_rect_memo();
+ pub(crate) fn discard_absolute_rects_memoized_during_commit(&mut self) {
+ self.arena_mut().clear_absolute_rect_memo();
}
- pub(crate) fn begin_commit(&self) {
- self.arena().clear_absolute_rect_memo();
+ pub(crate) fn begin_commit(&mut self) {
+ self.arena_mut().clear_absolute_rect_memo();
}
pub(crate) fn prepare_node(
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_rows.rs b/Libraries/LibWeb/Rust/src/painting/paintable_rows.rs
index 5cad1893f80..13fff907502 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_rows.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_rows.rs
@@ -19,7 +19,7 @@ use crate::painting::visual_context::{
BoxVisualContextNodeHandles, EMPTY_BOX_VISUAL_CONTEXT_NODE_HANDLES, PaintableVisualContextRecord,
};
use smallvec::{SmallVec, smallvec};
-use std::cell::{Cell, RefCell};
+use std::cell::RefCell;
use std::ffi::c_void;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
@@ -327,8 +327,7 @@ pub(crate) struct PaintableRowStore {
pub(crate) stacking_context_entries: StackingContextEntryColumn,
pub(crate) stacking_context_roots_flagged_for_resort: Vec<NodeSlotId>,
line_roots_needing_fragment_ownership: Vec<NodeSlotId>,
- absolute_rect_memo: RefCell<Vec<Option<(NodeSlotId, u64, crate::css::css_pixels::CssPixelRect)>>>,
- absolute_rect_memo_epoch: Cell<u64>,
+ absolute_rect_memo_epoch: u64,
committed_fragment_links: CowColumn<CommittedFragmentLinkSlot, PAINTABLE_SLOTS_PER_CHUNK>,
image_map_areas: crate::painting::image_map_areas::ImageMapAreaColumn,
unique_node_ids: UniqueNodeIdColumn,
@@ -358,6 +357,8 @@ fn set_visual_context_node_handles(
pub(crate) struct PaintableRows<Arena> {
arena: Arena,
+ /// The absolute rects this view computed, each valid for the geometry epoch of the arena it was computed at.
+ absolute_rects: RefCell<crate::painting::record::recorder_state::AbsoluteRectMemo>,
}
pub(crate) type PaintableRowsRef<'a> = PaintableRows<&'a LayoutNodeArena>;
@@ -365,7 +366,10 @@ pub(crate) type PaintableRowsMut<'a> = PaintableRows<&'a mut LayoutNodeArena>;
impl Clone for PaintableRowsRef<'_> {
fn clone(&self) -> Self {
- Self { arena: self.arena }
+ Self {
+ arena: self.arena,
+ absolute_rects: RefCell::default(),
+ }
}
}
@@ -545,6 +549,25 @@ where
impl<Arena> PaintableRowsRead for PaintableRows<Arena> where Arena: Deref<Target = LayoutNodeArena> {}
+impl<Arena> PaintableRows<Arena>
+where
+ Arena: Deref<Target = LayoutNodeArena>,
+{
+ pub(crate) fn memoized_absolute_rect_of_view(
+ &self,
+ id: NodeSlotId,
+ ) -> Option<crate::css::css_pixels::CssPixelRect> {
+ self.absolute_rects
+ .borrow()
+ .get(id, self.arena.absolute_rect_memo_epoch())
+ }
+
+ pub(crate) fn memoize_absolute_rect_of_view(&self, id: NodeSlotId, rect: crate::css::css_pixels::CssPixelRect) {
+ let epoch = self.arena.absolute_rect_memo_epoch();
+ self.absolute_rects.borrow_mut().set(id, epoch, rect);
+ }
+}
+
impl<Arena> PaintableRowsWrite for PaintableRows<Arena>
where
Arena: DerefMut<Target = LayoutNodeArena>,
@@ -700,7 +723,10 @@ impl LayoutNodeArena {
}
pub(crate) fn paintable_rows(&self) -> PaintableRowsRef<'_> {
- PaintableRows { arena: self }
+ PaintableRows {
+ arena: self,
+ absolute_rects: RefCell::default(),
+ }
}
pub(crate) fn schedule_scrollable_overflow_recalculation(&mut self, node: NodeSlotId) {
@@ -766,7 +792,7 @@ impl LayoutNodeArena {
let paint_facts = self.publish_paint_facts();
let store = &mut self.paintable_rows;
PublishedRows {
- geometry_epoch: store.absolute_rect_memo_epoch.get(),
+ geometry_epoch: store.absolute_rect_memo_epoch,
nodes,
paint_facts,
rows: store.rows.publish(),
@@ -796,7 +822,10 @@ impl LayoutNodeArena {
}
pub(crate) fn paintable_rows_mut(&mut self) -> PaintableRowsMut<'_> {
- PaintableRows { arena: self }
+ PaintableRows {
+ arena: self,
+ absolute_rects: RefCell::default(),
+ }
}
/// The fragment link committed for `node`. A layout reaches boxes whose paintable row is not populated yet, and a
@@ -814,26 +843,14 @@ impl LayoutNodeArena {
PaintableRowReset { slot, kind }
}
- pub(crate) fn memoized_absolute_rect(&self, id: NodeSlotId) -> Option<crate::css::css_pixels::CssPixelRect> {
- let store = &self.paintable_rows;
- let (memoized_id, memoized_epoch, rect) = store
- .absolute_rect_memo
- .borrow()
- .get(id.slot_index() as usize)
- .copied()
- .flatten()?;
- (memoized_id == id && memoized_epoch == store.absolute_rect_memo_epoch.get()).then_some(rect)
+ /// What the absolute rects memoized for the rows are valid for, which moves on with every write that moves a row.
+ pub(crate) fn absolute_rect_memo_epoch(&self) -> u64 {
+ self.paintable_rows.absolute_rect_memo_epoch
}
- pub(crate) fn memoize_absolute_rect(&self, id: NodeSlotId, rect: crate::css::css_pixels::CssPixelRect) {
- let store = &self.paintable_rows;
- store.absolute_rect_memo.borrow_mut()[id.slot_index() as usize] =
- Some((id, store.absolute_rect_memo_epoch.get(), rect));
- }
-
- pub(crate) fn clear_absolute_rect_memo(&self) {
- let epoch = &self.paintable_rows.absolute_rect_memo_epoch;
- epoch.set(epoch.get().checked_add(1).expect("absolute rect memo epoch overflowed"));
+ pub(crate) fn clear_absolute_rect_memo(&mut self) {
+ let epoch = &mut self.paintable_rows.absolute_rect_memo_epoch;
+ *epoch = epoch.checked_add(1).expect("absolute rect memo epoch overflowed");
}
pub(crate) fn paintable_row_count(&self) -> usize {
@@ -847,17 +864,14 @@ impl LayoutNodeArena {
.map(crate::painting::scrollable_overflow::OverflowStyle::new);
let store = &mut self.paintable_rows;
let index = layout_node.slot_index() as usize;
- let mut absolute_rect_memo = store.absolute_rect_memo.borrow_mut();
- let visual_context_records = &mut store.visual_context_records;
store.rows.grow_to(index + 1);
- visual_context_records.grow_to(index + 1);
+ store.visual_context_records.grow_to(index + 1);
store.committed_side_data.grow_to(index + 1);
store.visual_context_node_handles.grow_to(index + 1);
store.stacking_context_entries.grow_to(index + 1);
while store.side_data.len() <= index {
store.side_data.push(PaintableSideData::default());
store.row_paint_states.push(RowPaintState::default());
- absolute_rect_memo.push(None);
}
store.population_writes += 1;
@@ -874,12 +888,10 @@ impl LayoutNodeArena {
};
store.committed_side_data.set(index, CommittedSideData::default());
store.row_paint_states[index].clear();
- absolute_rect_memo[index] = None;
- visual_context_records.set(index, None);
+ store.visual_context_records.set(index, None);
set_visual_context_node_handles(&mut store.visual_context_node_handles, index, None);
drop_table(&mut store.stacking_context_entries, index);
self.scrollable_overflow.rows_to_measure.push(layout_node);
- drop((absolute_rect_memo, visual_context_records));
self.notify_committed_box_changed(layout_node);
}