Skip to content

Truly independent web browser

LibWeb: Look up text chunk memos without counting the font list

Aliaksandr Kalenikbc38c16823fe
diff --git a/Libraries/LibWeb/Rust/src/css/style/pointer_key.rs b/Libraries/LibWeb/Rust/src/css/style/pointer_key.rs
index 1a9fcac7afa..540b898fcbf 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pointer_key.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pointer_key.rs
@@ -30,6 +30,12 @@ impl Counted for RetainedStyleValueData {
}
}
+impl Counted for libgfx_rust::font::FrozenFontListRef {
+ fn address(&self) -> usize {
+ self.as_raw().addr()
+ }
+}
+
/// What a map keyed by [`PointerKey`] is looked up by, which takes no count, so that a hit costs what one by the bare
/// address would. It is made only from a counted reference, and a key is the only way to keep one in a map.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index cf2be50a4a8..7df62b97365 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -151,7 +151,7 @@ pub(crate) fn enroll_text_after_language_change(arena: &mut LayoutNodeArena, roo
!enrolled.is_empty()
}
-#[derive(Clone)]
+#[derive(Clone, PartialEq)]
pub(crate) struct TextChunkCacheKey {
pub(crate) should_wrap_lines: bool,
pub(crate) should_respect_linebreaks: bool,
@@ -161,30 +161,14 @@ pub(crate) struct TextChunkCacheKey {
pub(crate) line_break: u8,
pub(crate) line_break_language: super::text_chunker::LineBreakLanguage,
pub(crate) font_variant_emoji: u8,
- pub(crate) frozen_font_list: std::sync::Arc<libgfx_rust::font::FrozenFontList>,
+ /// A frozen cascade only moves when a load fills one of its faces in. The chunks made against it hold it.
+ pub(crate) frozen_font_list: crate::css::style::pointer_key::PointerLookup,
/// How many times loads had filled faces of the frozen cascade in, each of which can move the
/// font of a chunk.
pub(crate) frozen_font_list_fill_generation: u32,
pub(crate) splits_at_orientation_changes: bool,
}
-impl PartialEq for TextChunkCacheKey {
- fn eq(&self, other: &Self) -> bool {
- self.should_wrap_lines == other.should_wrap_lines
- && self.should_respect_linebreaks == other.should_respect_linebreaks
- && self.unidirectional_ltr == other.unidirectional_ltr
- && self.white_space_collapse == other.white_space_collapse
- && self.word_break == other.word_break
- && self.line_break == other.line_break
- && self.line_break_language == other.line_break_language
- && self.font_variant_emoji == other.font_variant_emoji
- && self.splits_at_orientation_changes == other.splits_at_orientation_changes
- // A frozen cascade only moves when a load fills one of its faces in.
- && std::sync::Arc::ptr_eq(&self.frozen_font_list, &other.frozen_font_list)
- && self.frozen_font_list_fill_generation == other.frozen_font_list_fill_generation
- }
-}
-
pub(crate) struct CachedTextChunks {
key: TextChunkCacheKey,
chunks: super::text_chunker::ChunkedText,
@@ -528,6 +512,8 @@ mod tests {
fn text_chunk_cache_tracks_line_break_language_changes() {
use crate::layout::text_chunker::{ChunkedText, LineBreakLanguage};
let content = content("文・", 0, 2, Vec::new());
+ let frozen_font_list =
+ libgfx_rust::font::FrozenFontListRef::new(std::sync::Arc::new(libgfx_rust::font::FrozenFontList::empty()));
let mut key = TextChunkCacheKey {
should_wrap_lines: true,
should_respect_linebreaks: false,
@@ -537,7 +523,7 @@ mod tests {
line_break: crate::css::css_enums::line_break::LOOSE,
line_break_language: LineBreakLanguage::Japanese,
font_variant_emoji: 0,
- frozen_font_list: std::sync::Arc::new(libgfx_rust::font::FrozenFontList::empty()),
+ frozen_font_list: crate::css::style::pointer_key::PointerLookup::new(&frozen_font_list, ()),
frozen_font_list_fill_generation: 0,
splits_at_orientation_changes: false,
};
@@ -579,6 +565,8 @@ mod tests {
.text_memos()
.text_chunks(node, 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()));
let key = TextChunkCacheKey {
should_wrap_lines: true,
should_respect_linebreaks: false,
@@ -588,7 +576,7 @@ mod tests {
line_break: crate::css::css_enums::line_break::AUTO,
line_break_language: crate::layout::text_chunker::LineBreakLanguage::Other,
font_variant_emoji: 0,
- frozen_font_list: std::sync::Arc::new(libgfx_rust::font::FrozenFontList::empty()),
+ frozen_font_list: crate::css::style::pointer_key::PointerLookup::new(&frozen_font_list, ()),
frozen_font_list_fill_generation: 0,
splits_at_orientation_changes: false,
};
@@ -606,6 +594,7 @@ mod tests {
let chunked_hello = || ChunkedText {
chunks: vec![chunk],
fonts: Box::new([font.clone()]),
+ frozen_font_list_held_for_memo_key: None,
};
let original = text_chunks(&scratch, &arena, &key, &chunked_hello);
let hit = text_chunks(&scratch, &arena, &key, &|| panic!("matching chunks should be cached"));
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index 0effacebe15..649fd4ae0c2 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -336,6 +336,9 @@ impl TextChunk {
pub(crate) struct ChunkedText {
pub(crate) chunks: Vec<TextChunk>,
pub(crate) fonts: Box<[FontHandle]>,
+ /// The frozen cascade the chunks were made against, which a memo of the chunks looks up by its address.
+ #[expect(dead_code, reason = "held so that no other cascade takes the address")]
+ pub(crate) frozen_font_list_held_for_memo_key: Option<std::sync::Arc<FrozenFontList>>,
}
impl ChunkedText {
@@ -361,6 +364,7 @@ pub(crate) fn chunk_text(inputs: TextChunkInputs<'_>) -> ChunkedText {
ChunkedText {
chunks,
fonts: chunker.fonts.into_boxed_slice(),
+ frozen_font_list_held_for_memo_key: None,
}
}
@@ -1371,7 +1375,8 @@ pub(crate) fn text_chunks(
unidirectional_ltr: bool,
) -> std::sync::Arc<super::rendered_text::CachedTextChunks> {
let parent_style = StyleValues::for_node(callbacks, callbacks.parent(node));
- let frozen_font_list = parent_style.frozen_font_list_ref();
+ let frozen_font_list = parent_style.frozen_font_list();
+ let counted_frozen_font_list = &parent_style.font().frozen_font_list;
let key = super::rendered_text::TextChunkCacheKey {
should_wrap_lines,
should_respect_linebreaks,
@@ -1385,18 +1390,19 @@ pub(crate) fn text_chunks(
LineBreakLanguage::Other
},
font_variant_emoji: parent_style.font_variant_emoji(),
+ frozen_font_list: crate::css::style::pointer_key::PointerLookup::new(counted_frozen_font_list, ()),
frozen_font_list_fill_generation: frozen_font_list.fill_generation(),
- frozen_font_list,
splits_at_orientation_changes: matches!(
parent_style.writing_mode(),
writing_mode::VERTICAL_RL | writing_mode::VERTICAL_LR
) && parent_style.text_orientation() == text_orientation::MIXED,
};
let text = &callbacks.text_content(node).text;
- callbacks.text_chunks(node, &key, || {
- chunk_text(TextChunkInputs {
+ callbacks.text_chunks(node, &key, || ChunkedText {
+ frozen_font_list_held_for_memo_key: Some(parent_style.frozen_font_list_ref()),
+ ..chunk_text(TextChunkInputs {
text,
- frozen_font_list: &key.frozen_font_list,
+ frozen_font_list,
white_space_collapse: key.white_space_collapse,
word_break: key.word_break,
line_break: key.line_break,