Skip to content

Truly independent web browser

LibWeb: Read a layout node's row once per style lookup

Aliaksandr Kalenikdbf2831adda4
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index ca885f65436..f7c39d140de 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -51,6 +51,23 @@ pub(crate) use main_thread_entries::MainThreadFfiEntry;
pub(crate) const SLOTS_PER_CHUNK: usize = 256;
+#[cold]
+#[inline(never)]
+#[track_caller]
+fn invalid_slot_access(id: NodeSlotId) -> ! {
+ panic!("invalid layout node arena slot ID {id:?}")
+}
+
+#[cold]
+#[inline(never)]
+#[track_caller]
+fn stale_slot_access(id: NodeSlotId, slot_generation: u8, access: &str) -> ! {
+ panic!(
+ "layout node arena {access} a stale or unused slot: {id:?} has generation {}, the slot {slot_generation}",
+ id.generation()
+ )
+}
+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct IntrinsicSizeCacheKey {
/// The block size inline sizes were measured at, or the inline size a block size was measured at.
@@ -1676,37 +1693,31 @@ impl LayoutNodeArena {
paintable_row_reset
}
+ // NB: The invalid ID names a slot past every chunk the arena can hold, or one whose generation never matches, so
+ // the chunk lookup and the generation check reject it too.
#[inline]
pub(crate) fn data(&self, id: NodeSlotId) -> &NodeData {
- assert!(!id.is_invalid(), "invalid layout node arena slot ID");
let index = id.slot_index() as usize;
- let chunk = self
- .chunks
- .get(index / SLOTS_PER_CHUNK)
- .expect("invalid layout node arena slot ID");
+ let Some(chunk) = self.chunks.get(index / SLOTS_PER_CHUNK) else {
+ invalid_slot_access(id)
+ };
let data = chunk.slot(index % SLOTS_PER_CHUNK);
- assert_eq!(
- data.slot_generation.get(),
- id.generation(),
- "layout node arena read a stale or unused slot"
- );
+ if data.slot_generation.get() != id.generation() {
+ stale_slot_access(id, data.slot_generation.get(), "read")
+ }
data
}
/// The node's shape, for writing. See [`ShapeWriter`].
pub(crate) fn write_shape(&mut self, id: NodeSlotId) -> ShapeWriter<'_> {
- assert!(!id.is_invalid(), "invalid layout node arena slot ID");
let index = id.slot_index() as usize;
- let shape = self
- .chunks
- .get_mut(index / SLOTS_PER_CHUNK)
- .expect("invalid layout node arena slot ID")
- .write_shape(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &mut self.shape_writes);
- assert_eq!(
- shape.slot_generation.get(),
- id.generation(),
- "layout node arena wrote a stale or unused slot"
- );
+ let Some(chunk) = self.chunks.get_mut(index / SLOTS_PER_CHUNK) else {
+ invalid_slot_access(id)
+ };
+ let shape = chunk.write_shape(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &mut self.shape_writes);
+ if shape.slot_generation.get() != id.generation() {
+ stale_slot_access(id, shape.slot_generation.get(), "wrote")
+ }
shape
}
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index ae927baa150..167087ed7f1 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -285,11 +285,19 @@ impl<'arena> LayoutPass<'arena> {
/// The style of `node`: that of its row, or the position option it is being laid out with.
#[inline]
pub(crate) fn computed_values_view_if_styled(&self, node: Node) -> Option<ComputedValuesView<'arena>> {
+ self.row_and_style(node).1
+ }
+
+ /// The row of `node` together with its style as [`Self::computed_values_view_if_styled`] answers it, from one
+ /// lookup of the row.
+ #[inline]
+ pub(crate) fn row_and_style(&self, node: Node) -> (&'arena NodeData, Option<ComputedValuesView<'arena>>) {
+ let data = self.node_data(node);
if let Some(style) = self.position_option_style(node) {
// SAFETY: A position option's style stays alive while a box is laid out with it.
- return Some(unsafe { style.view() });
+ return (data, Some(unsafe { style.view() }));
}
- self.arena.style_view(node)
+ (data, node_facts::node_style_view(data))
}
pub(crate) fn can_skip_is_anonymous_text_run(&self, node: Node) -> bool {
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index 8e4787f7fa3..2d72d174d31 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -817,14 +817,8 @@ pub(crate) struct NodeFacts<'pass> {
impl<'pass> NodeFacts<'pass> {
#[inline]
pub(crate) fn new(callbacks: &LayoutPass<'pass>, node: Node) -> Self {
- let data = callbacks.node_data(node);
- let style = callbacks.computed_values_view_if_styled(node);
- Self {
- callbacks: *callbacks,
- node,
- data,
- style,
- }
+ let (data, style) = callbacks.row_and_style(node);
+ Self::from_lookups(callbacks, node, data, style)
}
/// The facts of `node`, from the node data and style payloads that [`Self::new`] looked up for it.
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index efcbe98cb77..aead65c8aca 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -227,7 +227,6 @@ impl<'cells> SizedCells<'cells> {
pub(crate) struct SizingContext<'pass, 'container> {
state: &'pass RunState,
callbacks: LayoutPass<'pass>,
- style_cache: Cell<Option<(Node, ComputedValuesView<'pass>)>>,
child_run_shapes: Option<&'container dyn formatting_context::ChildRunShapes>,
}
@@ -283,7 +282,6 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
Self {
state,
callbacks,
- style_cache: Cell::new(None),
child_run_shapes: None,
}
}
@@ -308,16 +306,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
}
pub(super) fn style(&self, node: Node) -> StyleValues<'pass> {
- let kind = self.callbacks.node_data(node).kind.get();
- let payloads = match self.style_cache.get() {
- Some((cached_node, payloads)) if cached_node == node => payloads,
- _ => {
- let payloads = self.callbacks.style_view(node);
- self.style_cache.set(Some((node, payloads)));
- payloads
- }
- };
- let style = StyleValues::new(payloads).for_kind(kind).in_frame(self.callbacks.frame);
+ let style = StyleValues::for_node(&self.callbacks, node);
if !style.is_absolutely_positioned() {
return style;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/style_values.rs b/Libraries/LibWeb/Rust/src/layout/style_values.rs
index f856ea6982a..42637a269a2 100644
--- a/Libraries/LibWeb/Rust/src/layout/style_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/style_values.rs
@@ -225,8 +225,9 @@ impl<'a> StyleValues<'a> {
#[inline]
pub(crate) fn for_node(callbacks: &LayoutPass<'a>, node: Node) -> Self {
- StyleValues::new(callbacks.style_view(node))
- .for_kind(callbacks.node_data(node).kind.get())
+ let (data, style) = callbacks.row_and_style(node);
+ StyleValues::new(style.expect("styled node must publish its style container before layout"))
+ .for_kind(data.kind.get())
.in_frame(callbacks.frame)
}