Skip to content

Truly independent web browser

LibWeb: Index the text memos of layout by text state

Aliaksandr Kalenikab47e9dda2e7
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 24a290acbe8..fb8f23b9232 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -365,15 +365,20 @@ struct TextSlots {
states: CowVec<TextNodeState, TEXT_STATES_PER_CHUNK>,
/// The places in `states` no slot uses.
vacant_states: Vec<u32>,
+ released_states: Vec<u32>,
published: CowColumn<PublishedTextSlot, SLOTS_PER_CHUNK>,
}
impl TextSlots {
- fn state(&self, id: NodeSlotId) -> Option<&TextNodeState> {
+ fn state_index(&self, id: NodeSlotId) -> Option<u32> {
self.slots
.get(id.slot_index() as usize)
.filter(|slot| slot.generation == id.generation() && slot.state != TextNodeSlot::NO_STATE)
- .map(|slot| &self.states[slot.state as usize])
+ .map(|slot| slot.state)
+ }
+
+ fn state(&self, id: NodeSlotId) -> Option<&TextNodeState> {
+ self.state_index(id).map(|state| &self.states[state as usize])
}
/// The state of `id`'s slot, for writing. A slot last used by another generation starts over.
@@ -413,6 +418,7 @@ impl TextSlots {
if state != TextNodeSlot::NO_STATE {
self.states[state as usize] = TextNodeState::default();
self.vacant_states.push(state);
+ self.released_states.push(state);
}
}
@@ -1374,6 +1380,9 @@ impl LayoutNodeArena {
/// 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(&mut self) -> super::memo_invalidations::MemoInvalidations {
+ for state in self.text_slots.released_states.drain(..) {
+ self.memo_invalidations.note_text_state_dropped(state);
+ }
std::mem::take(&mut self.memo_invalidations)
}
@@ -5419,7 +5428,11 @@ impl LayoutNodeArena {
if !parent.is_invalid() && super::node_facts::has_flag(self.data(parent), NodeFlag::Anonymous) {
self.derive_flex_or_grid_item_flags(parent);
}
- self.memo_invalidations.note_text_replaced(id.slot_index());
+ let text_state = self
+ .text_slots
+ .state_index(id)
+ .expect("the row has the text it was just given");
+ self.memo_invalidations.note_text_state_dropped(text_state);
self.searchable_text = None;
// Publication can happen through a C++ text read before the enrolled
// sync runs. Invalidate here so every publication invalidates layout,
@@ -5528,6 +5541,17 @@ impl LayoutNodeArena {
self.text_node_state(id)?.content.as_ref()
}
+ pub(crate) fn text_state_index(&self, id: NodeSlotId) -> Option<u32> {
+ if !self.slot_is_live(id) {
+ return None;
+ }
+ self.text_slots.state_index(id)
+ }
+
+ pub(crate) fn text_state_capacity(&self) -> usize {
+ self.text_slots.states.len()
+ }
+
pub(super) fn set_first_letter_slices(
&mut self,
first_letter: NodeSlotId,
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 03e40720c06..e58a149b133 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -251,9 +251,12 @@ impl<'arena> LayoutPass<'arena> {
key: &super::rendered_text::TextChunkCacheKey,
compute: impl FnOnce() -> super::text_chunker::ChunkedText,
) -> std::sync::Arc<super::rendered_text::CachedTextChunks> {
- self.scratch
- .text_memos()
- .text_chunks(node, self.text_content(node).rendered(), key, compute)
+ self.scratch.text_memos().text_chunks(
+ self.text_state_index(node),
+ self.text_content(node).rendered(),
+ key,
+ compute,
+ )
}
pub(crate) fn prepared_inline_content_memos(
@@ -281,9 +284,17 @@ impl<'arena> LayoutPass<'arena> {
node: Node,
read: impl FnOnce(&super::text_chunker::GraphemeSegmenter) -> R,
) -> R {
- self.scratch
- .text_memos()
- .with_grapheme_segmenter(node, self.text_content(node).rendered(), read)
+ self.scratch.text_memos().with_grapheme_segmenter(
+ self.text_state_index(node),
+ self.text_content(node).rendered(),
+ read,
+ )
+ }
+
+ fn text_state_index(&self, node: Node) -> u32 {
+ self.arena
+ .text_state_index(node)
+ .expect("a text row holds its text in a text state")
}
#[inline]
diff --git a/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs
index 17aa4ef5a7b..1d0457abc56 100644
--- a/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs
+++ b/Libraries/LibWeb/Rust/src/layout/memo_invalidations.rs
@@ -22,7 +22,6 @@ use super::rendered_text::TextMemos;
#[derive(Clone, Copy, Default)]
struct SlotNotes {
noted: bool,
- text_memos_dropped: bool,
prepared_inline_content_dropped: bool,
run_cache_entries_dropped: bool,
in_flow_block_entries_dropped: bool,
@@ -37,6 +36,7 @@ struct SlotNotes {
pub(crate) struct MemoInvalidations {
slots: Vec<SlotNotes>,
noted_slots: Vec<u32>,
+ dropped_text_states: Vec<u32>,
}
impl MemoInvalidations {
@@ -65,7 +65,6 @@ impl MemoInvalidations {
pub(crate) fn note_slot_freed(&mut self, slot: u32) {
self.note_run_cache_entries_dropped(slot);
let notes = self.notes_of(slot);
- notes.text_memos_dropped = true;
notes.prepared_inline_content_dropped = true;
notes.in_flow_block_entries_dropped = true;
notes.intrinsic_sizes_dropped = true;
@@ -76,10 +75,10 @@ impl MemoInvalidations {
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
- /// describe.
- pub(crate) fn note_text_replaced(&mut self, slot: u32) {
- self.notes_of(slot).text_memos_dropped = true;
+ /// The text state at `text_state` got other rendered text or was let go of, which the chunks and segmenter made of
+ /// the old text no longer describe.
+ pub(crate) fn note_text_state_dropped(&mut self, text_state: u32) {
+ self.dropped_text_states.push(text_state);
}
/// The fragment cache epoch of `node` moved on: its run cache entries need the sweep, and its in-flow block entry
@@ -112,6 +111,9 @@ impl MemoInvalidations {
prepared_inline_content: &mut PreparedInlineContentMemos,
intrinsic_size_caches: &mut IntrinsicSizeCaches,
) {
+ for text_state in self.dropped_text_states {
+ text_memos.forget(text_state);
+ }
for slot in self.noted_slots {
let notes = self.slots[slot as usize];
if notes.run_cache_entries_dropped {
@@ -128,9 +130,6 @@ impl MemoInvalidations {
if notes.in_flow_block_entries_dropped {
in_flow_block_cache.remove_entry(slot);
}
- if notes.text_memos_dropped {
- text_memos.forget(slot);
- }
if notes.prepared_inline_content_dropped {
prepared_inline_content.forget(slot);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index 2d0b2faf7c3..14d7fdb186f 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -216,7 +216,7 @@ impl TextContent {
/// What layout keeps beside the rendered text of the text rows it lays out: the chunks the last run that chunked a row
/// made, and the row's grapheme segmenter. Each keeps the rendered text it was made for, which tells whether the row's
-/// text is still that text.
+/// text is still that text. They are indexed by the row's text state in the arena, so boxes without text take no room.
#[derive(Default)]
pub(crate) struct TextMemos {
/// Written in place, while a fork of the arena reads them as they were when it was made.
@@ -250,12 +250,12 @@ impl TextMemos {
/// run that chunked the row's text did so for another key.
pub(crate) fn text_chunks(
&self,
- node: NodeSlotId,
+ text_state: u32,
rendered: &Arc<RenderedText>,
key: &TextChunkCacheKey,
compute: impl FnOnce() -> super::text_chunker::ChunkedText,
) -> Arc<CachedTextChunks> {
- let index = node.slot_index() as usize;
+ let index = text_state as usize;
let memoized = self.chunks.with_row(index, |memo| match memo {
Some(Some(memo)) if Arc::ptr_eq(&memo.rendered, rendered) && memo.chunks.key == *key => {
Some(memo.chunks.clone())
@@ -285,31 +285,31 @@ impl TextMemos {
/// Runs `read` with the grapheme segmenter of the text row `node`, whose rendered text is `rendered`.
pub(crate) fn with_grapheme_segmenter<R>(
&self,
- node: NodeSlotId,
+ text_state: u32,
rendered: &Arc<RenderedText>,
read: impl FnOnce(&super::text_chunker::GraphemeSegmenter) -> R,
) -> R {
- self.grapheme_segmenters
- .with_row_mut(node.slot_index() as usize, |memo| {
- if !memo.as_ref().is_some_and(|memo| Arc::ptr_eq(&memo.rendered, rendered)) {
- *memo = Some(GraphemeSegmenterOfText {
- segmenter: super::text_chunker::GraphemeSegmenter::new(&rendered.text),
- rendered: rendered.clone(),
- });
- }
- read(&memo.as_ref().expect("the memo was just made").segmenter)
- })
+ self.grapheme_segmenters.with_row_mut(text_state as usize, |memo| {
+ if !memo.as_ref().is_some_and(|memo| Arc::ptr_eq(&memo.rendered, rendered)) {
+ *memo = Some(GraphemeSegmenterOfText {
+ segmenter: super::text_chunker::GraphemeSegmenter::new(&rendered.text),
+ rendered: rendered.clone(),
+ });
+ }
+ read(&memo.as_ref().expect("the memo was just made").segmenter)
+ })
}
- pub(super) fn cover_slots(&mut self, slot_count: usize) {
- self.chunks.cover_slots(slot_count);
- self.grapheme_segmenters.cover_slots(slot_count);
+ pub(super) fn cover_text_states(&mut self, text_state_count: usize) {
+ self.chunks.cover_slots(text_state_count);
+ self.grapheme_segmenters.cover_slots(text_state_count);
}
/// Lets go of what the memos keep for the row in `slot`, which the arena freed.
- pub(crate) fn forget(&mut self, slot: u32) {
- self.chunks.reset_row_where(slot as usize, Option::is_some);
- if let Some(segmenter) = self.grapheme_segmenters.row_mut(slot as usize) {
+ pub(crate) fn forget(&mut self, text_state: u32) {
+ let index = text_state as usize;
+ self.chunks.reset_row_where(index, Option::is_some);
+ if let Some(segmenter) = self.grapheme_segmenters.row_mut(index) {
*segmenter = None;
}
}
@@ -524,18 +524,18 @@ mod tests {
splits_at_orientation_changes: false,
};
let mut memos = TextMemos::default();
- memos.cover_slots(1);
- let node = NodeSlotId::new(0, 1);
- let mut previous = memos.text_chunks(node, content.rendered(), &key, ChunkedText::default);
+ memos.cover_text_states(1);
+ let text_state = 0;
+ let mut previous = memos.text_chunks(text_state, content.rendered(), &key, ChunkedText::default);
for language in [
LineBreakLanguage::Other,
LineBreakLanguage::Chinese,
LineBreakLanguage::Japanese,
] {
key.line_break_language = language;
- let chunks = memos.text_chunks(node, content.rendered(), &key, ChunkedText::default);
+ let chunks = memos.text_chunks(text_state, content.rendered(), &key, ChunkedText::default);
assert!(!std::sync::Arc::ptr_eq(&previous, &chunks));
- let hit = memos.text_chunks(node, content.rendered(), &key, || {
+ let hit = memos.text_chunks(text_state, content.rendered(), &key, || {
panic!("unchanged language should reuse chunks")
});
assert!(std::sync::Arc::ptr_eq(&chunks, &hit));
@@ -558,9 +558,12 @@ mod tests {
arena: &LayoutNodeArena,
key: &TextChunkCacheKey,
compute: &dyn Fn() -> ChunkedText| {
- scratch
- .text_memos()
- .text_chunks(node, arena.text_content(node).unwrap().rendered(), key, compute)
+ scratch.text_memos().text_chunks(
+ arena.text_state_index(node).unwrap(),
+ arena.text_content(node).unwrap().rendered(),
+ key,
+ compute,
+ )
};
let frozen_font_list =
libgfx_rust::font::FrozenFontListRef::new(std::sync::Arc::new(libgfx_rust::font::FrozenFontList::empty()));
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index 46a30f85bf9..09082ca2837 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -187,7 +187,7 @@ impl LayoutScratch {
pub(super) fn apply_memo_invalidations(&mut self, arena: &mut LayoutNodeArena) {
let slot_count = arena.slot_capacity();
self.intrinsic_size_caches.cover_slots(slot_count);
- self.text_memos.cover_slots(slot_count);
+ self.text_memos.cover_text_states(arena.text_state_capacity());
self.fragment_builder_scratch.cover_slots(slot_count);
self.fc_run_cache_store.cover_slots(slot_count);
self.in_flow_block_cache_store.cover_slots(slot_count);