Skip to content

Truly independent web browser

LibWeb: Keep owner-only layout memos in lazily chunked columns

Aliaksandr Kalenik2a7628f29144
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 37fd99f0b74..5704e0079e0 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -498,26 +498,23 @@ struct FragmentBuilderBuffers {
#[derive(Default)]
pub(crate) struct FragmentBuilderScratch {
- placed_box_slots: Vec<PlacedBoxSlot>,
- builders_created: u64,
- spare_buffers: Vec<FragmentBuilderBuffers>,
+ placed_box_slots: super::pass_column::PassColumn<PlacedBoxSlot>,
+ builders_created: std::cell::Cell<u64>,
+ spare_buffers: std::cell::RefCell<Vec<FragmentBuilderBuffers>>,
}
impl FragmentBuilderScratch {
fn place(
- &mut self,
+ &self,
builder: u64,
node: crate::layout::node_data::NodeSlotId,
placement: run_state::BoxPlacement,
) -> bool {
- let index = node.slot_index() as usize;
- if self.placed_box_slots.len() <= index {
- self.placed_box_slots.resize(index + 1, PlacedBoxSlot::default());
- }
- let slot = &mut self.placed_box_slots[index];
- let was_placed_by_builder = slot.builder == builder;
- *slot = PlacedBoxSlot { builder, placement };
- was_placed_by_builder
+ self.placed_box_slots.with_row_mut(node.slot_index() as usize, |slot| {
+ let was_placed_by_builder = slot.builder == builder;
+ *slot = PlacedBoxSlot { builder, placement };
+ was_placed_by_builder
+ })
}
fn placement_by(
@@ -525,10 +522,27 @@ impl FragmentBuilderScratch {
builder: u64,
node: crate::layout::node_data::NodeSlotId,
) -> Option<run_state::BoxPlacement> {
- self.placed_box_slots
- .get(node.slot_index() as usize)
- .filter(|slot| slot.builder == builder)
- .map(|slot| slot.placement)
+ self.placed_box_slots.with_row(node.slot_index() as usize, |slot| {
+ slot.filter(|slot| slot.builder == builder).map(|slot| slot.placement)
+ })
+ }
+
+ pub(super) fn cover_slots(&mut self, slot_count: usize) {
+ self.placed_box_slots.cover_slots(slot_count);
+ }
+
+ fn next_builder_id(&self) -> u64 {
+ let builder = self.builders_created.get() + 1;
+ self.builders_created.set(builder);
+ builder
+ }
+
+ fn take_spare_buffers(&self) -> FragmentBuilderBuffers {
+ self.spare_buffers.borrow_mut().pop().unwrap_or_default()
+ }
+
+ fn return_spare_buffers(&self, buffers: FragmentBuilderBuffers) {
+ self.spare_buffers.borrow_mut().push(buffers);
}
}
@@ -1131,13 +1145,11 @@ impl RunFragmentBuilder {
is_entry_accumulator: bool,
callbacks: &LayoutPass<'_>,
) -> Self {
- let mut scratch = callbacks.fragment_builder_scratch().borrow_mut();
- scratch.builders_created += 1;
- let builder = scratch.builders_created;
+ let builder = callbacks.fragment_builder_scratch().next_builder_id();
let FragmentBuilderBuffers {
pending_fragments,
placed_boxes,
- } = scratch.spare_buffers.pop().unwrap_or_default();
+ } = callbacks.fragment_builder_scratch().take_spare_buffers();
Self {
root_node,
root_containing_block_slot,
@@ -1152,9 +1164,8 @@ impl RunFragmentBuilder {
}
fn with_placements<R>(&self, callbacks: &LayoutPass<'_>, read: impl FnOnce(&PlacementsOfBuilder<'_>) -> R) -> R {
- let scratch = callbacks.fragment_builder_scratch().borrow();
read(&PlacementsOfBuilder {
- scratch: &scratch,
+ scratch: callbacks.fragment_builder_scratch(),
builder: self.builder,
})
}
@@ -1174,7 +1185,6 @@ impl RunFragmentBuilder {
) {
let was_placed = callbacks
.fragment_builder_scratch()
- .borrow_mut()
.place(self.builder, node, placement);
debug_assert!(!was_placed, "a box is placed once");
let placed_boxes = &mut self.inner.borrow_mut().placed_boxes;
@@ -1588,17 +1598,18 @@ impl RunFragmentBuilder {
link.committed_offset = formatting_context::point_add(link.committed_offset, offset);
}
{
- let mut scratch = callbacks.fragment_builder_scratch().borrow_mut();
+ let scratch = callbacks.fragment_builder_scratch();
for &slot in &inner.placed_boxes.placed_slots {
- let placed = &mut scratch.placed_box_slots[slot as usize];
- debug_assert!(
- placed.builder == self.builder,
- "a run's placements stay with its builder"
- );
- if placed.placement.containing_block == root {
- placed.placement.content_offset_in_containing_block =
- formatting_context::point_add(placed.placement.content_offset_in_containing_block, offset);
- }
+ scratch.placed_box_slots.with_row_mut(slot as usize, |placed| {
+ debug_assert!(
+ placed.builder == self.builder,
+ "a run's placements stay with its builder"
+ );
+ if placed.placement.containing_block == root {
+ placed.placement.content_offset_in_containing_block =
+ formatting_context::point_add(placed.placement.content_offset_in_containing_block, offset);
+ }
+ });
}
}
for entry in inner
@@ -1982,9 +1993,7 @@ impl RunFragmentBuilder {
placed_boxes.cells_of_abspos_containing_blocks.clear();
callbacks
.fragment_builder_scratch()
- .borrow_mut()
- .spare_buffers
- .push(FragmentBuilderBuffers {
+ .return_spare_buffers(FragmentBuilderBuffers {
pending_fragments,
placed_boxes,
});
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 10321c8c0b8..24a290acbe8 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -34,7 +34,6 @@ use crate::layout::tree_mutation::HostCalls;
use crate::painting::paint_read::{GeometryRead, PaintRead};
use crate::render_state::DocumentHost;
use crate::stage::MainThread;
-use std::cell::RefCell;
use std::ffi::c_void;
use std::hash::Hash;
use std::ops::{Deref, DerefMut};
@@ -255,24 +254,29 @@ struct IntrinsicSizeCacheSlot {
/// 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>>,
+ slots: super::pass_column::PassColumn<IntrinsicSizeCacheSlot>,
}
impl IntrinsicSizeCaches {
+ pub(super) fn cover_slots(&mut self, slot_count: usize) {
+ self.slots.cover_slots(slot_count);
+ }
+
pub(crate) fn drop_entries_of_slot(&mut self, slot: u32) {
- if let Some(slot) = self.slots.get_mut().get_mut(slot as usize) {
+ if let Some(slot) = self.slots.row_mut(slot as usize) {
*slot = IntrinsicSizeCacheSlot::default();
}
}
/// 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(node.slot_index() as usize)?;
- if slot.generation != node.generation() {
- return None;
- }
- answer(slot.sizes.as_ref()?)
+ self.slots.with_row(node.slot_index() as usize, |slot| {
+ let slot = slot?;
+ if slot.generation != node.generation() {
+ return None;
+ }
+ answer(slot.sizes.as_ref()?)
+ })
}
pub(crate) fn max_content_block_size_cache_get(
@@ -284,19 +288,15 @@ impl IntrinsicSizeCaches {
}
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 {
- caches.resize_with(index + 1, IntrinsicSizeCacheSlot::default);
- }
- let slot = &mut caches[index];
- if slot.generation != node.generation() {
- *slot = IntrinsicSizeCacheSlot {
- generation: node.generation(),
- sizes: None,
- };
- }
- callback(slot.sizes.get_or_insert_with(Box::default));
+ self.slots.with_row_mut(node.slot_index() as usize, |slot| {
+ if slot.generation != node.generation() {
+ *slot = IntrinsicSizeCacheSlot {
+ generation: node.generation(),
+ sizes: None,
+ };
+ }
+ callback(slot.sizes.get_or_insert_with(Box::default));
+ });
}
pub(crate) fn max_content_block_size_cache_put(
@@ -6002,6 +6002,10 @@ impl LayoutNodeArena {
.filter(|&inline_box| self.slot_is_live(inline_box))
}
+ pub(crate) fn slot_capacity(&self) -> usize {
+ self.slot_metadata.len()
+ }
+
/// The live node in the slot at `index`, if there is one.
pub(crate) fn live_slot_at(&self, index: u32) -> Option<NodeSlotId> {
self.slot_metadata
@@ -7701,7 +7705,8 @@ mod tests {
#[test]
fn intrinsic_size_cache_answers_masked_probes_only_from_independent_measurements() {
let mut arena = LayoutNodeArena::new();
- let caches = super::IntrinsicSizeCaches::default();
+ let mut caches = super::IntrinsicSizeCaches::default();
+ caches.cover_slots(64);
let allocation = arena.allocate_for_test();
let dependent_allocation = arena.allocate_for_test();
let key_with_basis = |basis: i32| IntrinsicSizeCacheKey {
@@ -7838,7 +7843,8 @@ mod tests {
#[test]
fn intrinsic_inline_size_cache_answers_probes_through_every_combination_of_masked_key_parts() {
let mut arena = LayoutNodeArena::new();
- let caches = super::IntrinsicSizeCaches::default();
+ let mut caches = super::IntrinsicSizeCaches::default();
+ caches.cover_slots(64);
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();
@@ -7871,7 +7877,8 @@ mod tests {
#[test]
fn intrinsic_inline_size_cache_masks_the_block_size_of_measurements_that_never_observed_it() {
let mut arena = LayoutNodeArena::new();
- let caches = super::IntrinsicSizeCaches::default();
+ let mut caches = super::IntrinsicSizeCaches::default();
+ caches.cover_slots(64);
let observes_block_size = IntrinsicMeasurementDependencies {
block_size: true,
..Default::default()
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 4447fcc75ac..03e40720c06 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -133,7 +133,8 @@ impl<'arena> LayoutPass<'arena> {
/// before it and keeps none, so that it computes everything afresh and leaves nothing for the runs after it. It
/// measures intrinsic sizes into caches of its own, which start empty and are dropped when it returns.
pub(crate) fn lay_out_discarded_run<R>(self, run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> R) -> R {
- let intrinsic_size_caches = super::layout_node_arena::IntrinsicSizeCaches::default();
+ let mut intrinsic_size_caches = super::layout_node_arena::IntrinsicSizeCaches::default();
+ intrinsic_size_caches.cover_slots(self.arena.slot_capacity());
let pass: LayoutPass<'_> = self;
run(LayoutPass {
intrinsic_size_caches: &intrinsic_size_caches,
@@ -194,7 +195,7 @@ impl<'arena> LayoutPass<'arena> {
self.scratch.fc_run_cache_store()
}
- pub(crate) fn fragment_builder_scratch(&self) -> &'arena RefCell<super::fragment_tree::FragmentBuilderScratch> {
+ pub(crate) fn fragment_builder_scratch(&self) -> &'arena super::fragment_tree::FragmentBuilderScratch {
self.scratch.fragment_builder_scratch()
}
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index 81cd73db533..bdd542b434e 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -41,6 +41,7 @@ pub(crate) mod math_formatting_context;
mod memo_invalidations;
pub mod node_data;
pub(crate) mod node_facts;
+mod pass_column;
pub(crate) mod tree_shape;
pub use libcompositing_rust::node_slot_id;
mod partial_relayout;
diff --git a/Libraries/LibWeb/Rust/src/layout/pass_column.rs b/Libraries/LibWeb/Rust/src/layout/pass_column.rs
new file mode 100644
index 00000000000..885828bbe7a
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/layout/pass_column.rs
@@ -0,0 +1,225 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use std::cell::UnsafeCell;
+use std::sync::atomic::{AtomicPtr, Ordering};
+
+const ROWS_PER_CHUNK: usize = 128;
+
+#[repr(align(64))]
+struct PassChunk<T> {
+ rows: [UnsafeCell<T>; ROWS_PER_CHUNK],
+ #[cfg(debug_assertions)]
+ borrows: [std::sync::atomic::AtomicI32; ROWS_PER_CHUNK],
+}
+
+impl<T: Default> PassChunk<T> {
+ fn new() -> Box<Self> {
+ Box::new(Self {
+ rows: std::array::from_fn(|_| UnsafeCell::new(T::default())),
+ #[cfg(debug_assertions)]
+ borrows: std::array::from_fn(|_| std::sync::atomic::AtomicI32::new(0)),
+ })
+ }
+}
+
+struct OwnedChunk<T> {
+ pointer: AtomicPtr<PassChunk<T>>,
+ owns: std::marker::PhantomData<Box<PassChunk<T>>>,
+}
+
+impl<T> Default for OwnedChunk<T> {
+ fn default() -> Self {
+ Self {
+ pointer: AtomicPtr::default(),
+ owns: std::marker::PhantomData,
+ }
+ }
+}
+
+impl<T> Drop for OwnedChunk<T> {
+ fn drop(&mut self) {
+ let chunk = *self.pointer.get_mut();
+ if !chunk.is_null() {
+ // SAFETY: A non-null chunk pointer came from `Box::into_raw` in `make_chunk`, and only this slot owns it.
+ drop(unsafe { Box::from_raw(chunk) });
+ }
+ }
+}
+
+pub(crate) struct PassColumn<T> {
+ chunks: Vec<OwnedChunk<T>>,
+}
+
+impl<T> Default for PassColumn<T> {
+ fn default() -> Self {
+ Self { chunks: Vec::new() }
+ }
+}
+
+impl<T: Default> PassColumn<T> {
+ pub(crate) fn cover_slots(&mut self, slot_count: usize) {
+ let chunk_count = slot_count.div_ceil(ROWS_PER_CHUNK);
+ if self.chunks.len() < chunk_count {
+ self.chunks.resize_with(chunk_count, Default::default);
+ }
+ }
+
+ #[inline]
+ pub(crate) fn with_row<R>(&self, index: usize, read: impl FnOnce(Option<&T>) -> R) -> R {
+ self.table().with_row(index, read)
+ }
+
+ #[inline]
+ pub(crate) fn with_row_mut<R>(&self, index: usize, write: impl FnOnce(&mut T) -> R) -> R {
+ self.table().with_row_mut(index, write)
+ }
+
+ #[inline]
+ fn table(&self) -> ChunkTable<'_, T> {
+ ChunkTable { chunks: &self.chunks }
+ }
+
+ pub(crate) fn row_mut(&mut self, index: usize) -> Option<&mut T> {
+ Some(self.rows_of_chunk_mut(index / ROWS_PER_CHUNK)?[index % ROWS_PER_CHUNK].get_mut())
+ }
+
+ fn rows_of_chunk_mut(&mut self, chunk_index: usize) -> Option<&mut [UnsafeCell<T>; ROWS_PER_CHUNK]> {
+ let chunk = *self.chunks.get_mut(chunk_index)?.pointer.get_mut();
+ // SAFETY: `&mut self` rules out any other reference into the column's chunks.
+ Some(&mut unsafe { chunk.as_mut() }?.rows)
+ }
+}
+
+struct ChunkTable<'column, T> {
+ chunks: &'column [OwnedChunk<T>],
+}
+
+impl<T> Clone for ChunkTable<'_, T> {
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
+impl<T> Copy for ChunkTable<'_, T> {}
+
+impl<'column, T: Default> ChunkTable<'column, T> {
+ #[inline]
+ fn with_row<R>(self, index: usize, read: impl FnOnce(Option<&T>) -> R) -> R {
+ let Some(chunk) = self.chunk_for_reading(index / ROWS_PER_CHUNK) else {
+ return read(None);
+ };
+ let _borrow = RowBorrow::shared(chunk, index % ROWS_PER_CHUNK);
+ // SAFETY: Only the run laying out the row's box writes the row, and not while this read runs (`RowBorrow`
+ // checks that in debug builds).
+ read(Some(unsafe { &*chunk.rows[index % ROWS_PER_CHUNK].get() }))
+ }
+
+ #[inline]
+ fn with_row_mut<R>(self, index: usize, write: impl FnOnce(&mut T) -> R) -> R {
+ let chunk = self.chunk_for_writing(index / ROWS_PER_CHUNK);
+ let _borrow = RowBorrow::exclusive(chunk, index % ROWS_PER_CHUNK);
+ // SAFETY: Only the run laying out the row's box reaches the row while it writes it (`RowBorrow` checks that in
+ // debug builds).
+ write(unsafe { &mut *chunk.rows[index % ROWS_PER_CHUNK].get() })
+ }
+
+ #[inline]
+ fn chunk_for_reading(self, chunk_index: usize) -> Option<&'column PassChunk<T>> {
+ let chunk = self.chunks.get(chunk_index)?.pointer.load(Ordering::Acquire);
+ // SAFETY: A non-null chunk pointer stays valid until its column drops, which the borrow of the column rules
+ // out.
+ unsafe { chunk.as_ref() }
+ }
+
+ #[inline]
+ fn chunk_for_writing(self, chunk_index: usize) -> &'column PassChunk<T> {
+ let slot = &self
+ .chunks
+ .get(chunk_index)
+ .expect("a pass writes only rows of slots the arena held as the pass began")
+ .pointer;
+ let chunk = slot.load(Ordering::Acquire);
+ // SAFETY: As in `chunk_for_reading`.
+ unsafe { chunk.as_ref() }.unwrap_or_else(|| make_chunk(slot))
+ }
+}
+
+#[cold]
+#[inline(never)]
+fn make_chunk<T: Default>(slot: &AtomicPtr<PassChunk<T>>) -> &PassChunk<T> {
+ let made = Box::into_raw(PassChunk::new());
+ match slot.compare_exchange(std::ptr::null_mut(), made, Ordering::AcqRel, Ordering::Acquire) {
+ // SAFETY: The slot now owns `made`, which stays valid until its column drops.
+ Ok(_) => unsafe { &*made },
+ Err(made_by_another_run) => {
+ // SAFETY: `made` was never shared.
+ drop(unsafe { Box::from_raw(made) });
+ // SAFETY: As in `ChunkTable::chunk_for_reading`.
+ unsafe { &*made_by_another_run }
+ }
+ }
+}
+
+struct RowBorrow<'chunk> {
+ #[cfg(debug_assertions)]
+ state: &'chunk std::sync::atomic::AtomicI32,
+ #[cfg(not(debug_assertions))]
+ chunk: std::marker::PhantomData<&'chunk ()>,
+}
+
+impl<'chunk> RowBorrow<'chunk> {
+ #[inline]
+ fn shared<T>(chunk: &'chunk PassChunk<T>, row: usize) -> Self {
+ #[cfg(debug_assertions)]
+ {
+ let state = &chunk.borrows[row];
+ let previous = state.fetch_add(1, std::sync::atomic::Ordering::Acquire);
+ assert!(previous >= 0, "a pass column row is read while it is written");
+ Self { state }
+ }
+ #[cfg(not(debug_assertions))]
+ {
+ let _ = (chunk, row);
+ Self {
+ chunk: std::marker::PhantomData,
+ }
+ }
+ }
+
+ #[inline]
+ fn exclusive<T>(chunk: &'chunk PassChunk<T>, row: usize) -> Self {
+ #[cfg(debug_assertions)]
+ {
+ let state = &chunk.borrows[row];
+ let previous = state.swap(-1, std::sync::atomic::Ordering::Acquire);
+ assert!(
+ previous == 0,
+ "a pass column row is written while it is reached otherwise"
+ );
+ Self { state }
+ }
+ #[cfg(not(debug_assertions))]
+ {
+ let _ = (chunk, row);
+ Self {
+ chunk: std::marker::PhantomData,
+ }
+ }
+ }
+}
+
+#[cfg(debug_assertions)]
+impl Drop for RowBorrow<'_> {
+ fn drop(&mut self) {
+ let state = self.state.load(std::sync::atomic::Ordering::Relaxed);
+ if state < 0 {
+ self.state.store(0, std::sync::atomic::Ordering::Release);
+ } else {
+ self.state.fetch_sub(1, std::sync::atomic::Ordering::Release);
+ }
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index a5ecbaf3f55..c543615ff2d 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -222,7 +222,7 @@ impl TextContent {
pub(crate) struct TextMemos {
/// In chunks a fork of the arena shares until either side writes one, so forking does not copy every row's memo.
chunks: RefCell<crate::cow_column::CowVec<Option<ChunksOfText>, CHUNK_MEMOS_PER_CHUNK>>,
- grapheme_segmenters: RefCell<Vec<Option<GraphemeSegmenterOfText>>>,
+ grapheme_segmenters: super::pass_column::PassColumn<Option<GraphemeSegmenterOfText>>,
}
/// How many rows' chunk memos a chunk of [`TextMemos::chunks`] holds: a write after a fork copies this many.
@@ -246,7 +246,7 @@ impl TextMemos {
pub(crate) fn fork(&self) -> Self {
Self {
chunks: self.chunks.clone(),
- grapheme_segmenters: RefCell::default(),
+ grapheme_segmenters: Default::default(),
}
}
@@ -292,19 +292,20 @@ impl TextMemos {
rendered: &Arc<RenderedText>,
read: impl FnOnce(&super::text_chunker::GraphemeSegmenter) -> R,
) -> R {
- let index = node.slot_index() as usize;
- let mut segmenters = self.grapheme_segmenters.borrow_mut();
- if segmenters.len() <= index {
- segmenters.resize_with(index + 1, || None);
- }
- let memo = &mut segmenters[index];
- 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(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)
+ })
+ }
+
+ pub(super) fn cover_slots(&mut self, slot_count: usize) {
+ self.grapheme_segmenters.cover_slots(slot_count);
}
/// Lets go of what the memos keep for the row in `slot`, which the arena freed.
@@ -313,7 +314,7 @@ impl TextMemos {
if chunks.get(slot as usize).is_some_and(Option::is_some) {
chunks[slot as usize] = None;
}
- if let Some(segmenter) = self.grapheme_segmenters.get_mut().get_mut(slot as usize) {
+ if let Some(segmenter) = self.grapheme_segmenters.row_mut(slot as usize) {
*segmenter = None;
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index 4f2b9b43a74..6b87469f972 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -24,7 +24,7 @@ pub(crate) struct LayoutScratch {
text_memos: super::rendered_text::TextMemos,
inline_content_preparation_buffers: Cell<super::prepared_inline_content::InlineContentPreparationBuffers>,
prepared_inline_content_memos: super::prepared_inline_content::PreparedInlineContentMemos,
- fragment_builder_scratch: RefCell<fragment_tree::FragmentBuilderScratch>,
+ fragment_builder_scratch: fragment_tree::FragmentBuilderScratch,
}
/// What layout found out that the host hears of once the layout that found it is committed: what the
@@ -161,7 +161,7 @@ impl LayoutScratch {
&self.fc_run_cache_store
}
- pub(super) fn fragment_builder_scratch(&self) -> &RefCell<fragment_tree::FragmentBuilderScratch> {
+ pub(super) fn fragment_builder_scratch(&self) -> &fragment_tree::FragmentBuilderScratch {
&self.fragment_builder_scratch
}
@@ -185,6 +185,10 @@ impl LayoutScratch {
/// 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(&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.fragment_builder_scratch.cover_slots(slot_count);
arena.take_memo_invalidations().apply_to(
&mut self.fc_run_cache_store,
&mut self.in_flow_block_cache_store,