Skip to content

Truly independent web browser

LibWeb: Write the shape of layout nodes through a mutable arena

Aliaksandr Kalenik3ff875134fc9
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 0a2b786ce22..72c27031517 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1516,7 +1516,7 @@ impl LayoutNodeArena {
self.slot_is_live(slot),
"layout node arena bound a shell to a dead slot"
);
- let data = self.write_shape(slot);
+ let mut data = self.write_shape(slot);
data.set_kind(construction_facts.kind);
data.set_flags(super::node_facts::construction_flags(
construction_facts.kind,
@@ -1696,10 +1696,9 @@ impl LayoutNodeArena {
);
}
- fn unlink_children_of_node_being_freed(&self, id: NodeSlotId) {
- let data = self.data(id);
+ fn unlink_children_of_node_being_freed(&mut self, id: NodeSlotId) {
loop {
- let child = data.first_child.get();
+ let child = self.data(id).first_child.get();
if child.is_invalid() {
break;
}
@@ -1805,14 +1804,14 @@ impl LayoutNodeArena {
}
/// The node's shape, for writing. See [`ShapeWriter`].
- pub(crate) fn write_shape(&self, id: NodeSlotId) -> 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(index / SLOTS_PER_CHUNK)
+ .get_mut(index / SLOTS_PER_CHUNK)
.expect("invalid layout node arena slot ID")
- .write_shape(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &self.shape_writes);
+ .write_shape(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &mut self.shape_writes);
assert_eq!(
shape.slot_generation.get(),
id.generation(),
@@ -1827,7 +1826,7 @@ impl LayoutNodeArena {
&mut self,
) -> crate::cow_column::ColumnSnapshot<super::node_data::PaintNode, PUBLISHED_ROWS_PER_CHUNK> {
self.tree_shape.publish(
- &self.chunks,
+ &mut self.chunks,
&self.style_records,
&self.style_nodes,
|index| {
@@ -1835,7 +1834,7 @@ impl LayoutNodeArena {
.get(index)
.is_some_and(|&pin| pin == ArenaStylePin::Derived)
},
- &self.shape_writes,
+ &mut self.shape_writes,
)
}
@@ -1846,12 +1845,6 @@ impl LayoutNodeArena {
self.chunks[index / SLOTS_PER_CHUNK].unpublished_facts_mut(index % SLOTS_PER_CHUNK)
}
- /// The shape of the node whose data `data` is, for writing.
- fn write_shape_of(&self, data: &NodeData) -> ShapeWriter<'_> {
- let (index, metadata) = self.slot_for_data(data);
- self.write_shape(NodeSlotId::new(index, metadata.generation))
- }
-
pub(crate) fn set_node_generated_for(&mut self, id: NodeSlotId, generated_for: u8, generator: Option<StyleNodeID>) {
self.write_shape(id).set_generated_for(generated_for);
self.set_node_style_node(id, generator);
@@ -2852,7 +2845,7 @@ impl LayoutNodeArena {
) {
let previous_payloads = self.data(row).style.get();
self.replace_style_record(row.slot_index() as usize, style.record);
- let shape = self.write_shape(row);
+ let mut shape = self.write_shape(row);
shape.set_style(style.payloads);
shape.mark();
self.note_row_style(row);
@@ -3867,10 +3860,10 @@ impl LayoutNodeArena {
}
fn derive_containing_block_establishment_flags(&mut self, node: NodeSlotId) {
- let data = self.write_shape(node);
+ let data = self.data(node);
let previous_flags = data.flags.get();
let (absolute, fixed) = if data.kind.get() == NodeKind::InlineNode {
- let absolute = !super::node_facts::has_flag(&*data, NodeFlag::Anonymous)
+ let absolute = !super::node_facts::has_flag(data, NodeFlag::Anonymous)
&& self
.node_style_if_live(node)
.is_some_and(crate::painting::style_queries::inline_establishes_absolute_position_containing_block);
@@ -3887,7 +3880,7 @@ impl LayoutNodeArena {
if fixed {
flags |= NodeFlag::EstablishesFixedPositionContainingBlock as u32;
}
- data.set_flags(flags);
+ self.write_shape(node).set_flags(flags);
let previous = previous_flags & establishment_flags;
let current = flags & establishment_flags;
@@ -3941,7 +3934,7 @@ impl LayoutNodeArena {
}
pub(crate) fn forget_committed_out_of_flow_facts(&mut self, node: NodeSlotId) {
- let data = self.write_shape(node);
+ let mut data = self.write_shape(node);
data.set_flags(data.flags.get() & !(NodeFlag::AbsposDescendantEscapes as u32));
let contained = &mut self.out_of_flow_positioning_contained;
if !contained.is_empty() {
@@ -4062,16 +4055,16 @@ impl LayoutNodeArena {
/// recognized by its kind. The layout node made for the row answers for the paint facts the row
/// is built with, which are not published.
pub(crate) fn stamp_dom_row(&mut self, slot: NodeSlotId, kind: NodeKind, style_node: Option<StyleNodeID>) {
- let data = self.write_shape(slot);
assert_eq!(
- data.kind.get(),
+ self.data(slot).kind.get(),
NodeKind::Unset,
"stamped a row the build allocated onto a bound slot"
);
- data.set_kind(kind);
let construction_facts = style_node.map_or(0, |style_node| {
self.with_style_store(|engine| engine.element_construction_facts(style_node))
});
+ let mut data = self.write_shape(slot);
+ data.set_kind(kind);
data.set_flags(super::node_facts::construction_flags(kind, false, construction_facts));
let math_layout_kind =
super::math_formatting_context::MathLayoutKind::from_construction_facts(construction_facts);
@@ -4121,7 +4114,7 @@ impl LayoutNodeArena {
/// Gives a row being built the paint facts the DOM published under `style_node`, the node the
/// row is built for. The row is not committed yet, so this is a plain write rather than the
/// change a live row's facts go through.
- pub(crate) fn stamp_dom_paint_facts(&self, slot: NodeSlotId, style_node: StyleNodeID) {
+ pub(crate) fn stamp_dom_paint_facts(&mut self, slot: NodeSlotId, style_node: StyleNodeID) {
let facts = self.with_style_store(|engine| engine.node_dom_paint_facts(style_node));
self.write_shape(slot).set_dom_paint_facts(facts);
}
@@ -4144,7 +4137,7 @@ impl LayoutNodeArena {
generator: StyleNodeID,
generated_for: u8,
) {
- let data = self.write_shape(slot);
+ let mut data = self.write_shape(slot);
assert_eq!(
data.kind.get(),
NodeKind::Unset,
@@ -4165,7 +4158,7 @@ impl LayoutNodeArena {
/// Stamps a row the build allocated for a piece of generated text, which names no DOM node and
/// carries no style of its own.
pub(crate) fn stamp_generated_text_row(&mut self, slot: NodeSlotId, text: ak::Utf16String) {
- let data = self.write_shape(slot);
+ let mut data = self.write_shape(slot);
assert_eq!(
data.kind.get(),
NodeKind::Unset,
@@ -4208,7 +4201,7 @@ impl LayoutNodeArena {
}
pub(crate) fn stamp_anonymous_box(&mut self, slot: NodeSlotId, kind: NodeKind, derived: DerivedStyleRecord) {
- let data = self.write_shape(slot);
+ let mut data = self.write_shape(slot);
assert_eq!(
data.kind.get(),
NodeKind::Unset,
@@ -4219,7 +4212,7 @@ impl LayoutNodeArena {
data.set_flags(super::node_facts::construction_flags(kind, true, 0));
self.replace_style_record(slot.slot_index() as usize, derived.record);
self.style_record_pins[slot.slot_index() as usize] = ArenaStylePin::Derived;
- let data = self.write_shape(slot);
+ let mut data = self.write_shape(slot);
data.set_style(derived.payloads);
data.mark();
self.note_row_style(slot);
@@ -4314,7 +4307,7 @@ impl LayoutNodeArena {
assert!(derived.record != 0 && !derived.payloads.is_null());
let previous_style_record = self.replace_style_record(slot.slot_index() as usize, derived.record);
self.shape_writes.note_style();
- let shape = self.write_shape(slot);
+ let mut shape = self.write_shape(slot);
shape.set_style(derived.payloads);
shape.mark();
self.note_row_style(slot);
@@ -4471,14 +4464,15 @@ impl LayoutNodeArena {
self.data(id).dom_paint_facts.get() & fact as u8 != 0
}
- pub(crate) fn set_node_dom_paint_facts(&self, id: NodeSlotId, facts: u8) -> bool {
+ pub(crate) fn set_node_dom_paint_facts(&mut self, id: NodeSlotId, facts: u8) -> bool {
let mut any_changed = false;
- for row in self.rows_sharing_dom_node_with(id) {
- let data = self.write_shape(row);
- if data.dom_paint_facts.get() == facts {
+ let mut cursor = Some(id);
+ while let Some(row) = cursor {
+ cursor = self.next_row_sharing_dom_node_with(id, row);
+ if self.data(row).dom_paint_facts.get() == facts {
continue;
}
- data.set_dom_paint_facts(facts);
+ self.write_shape(row).set_dom_paint_facts(facts);
any_changed = true;
use crate::painting::record::damage::PaintDamage;
self.push_paint_damage_for_repaint(row, PaintDamage::ALL_HIT | PaintDamage::SCROLL_METADATA);
@@ -4608,12 +4602,12 @@ impl LayoutNodeArena {
}
pub(crate) fn set_node_needs_compositor_animation_frame(
- &self,
+ &mut self,
id: NodeSlotId,
kind: super::node_data::CompositorAnimationFrameKind,
value: bool,
) {
- let data = self.write_shape(id);
+ let mut data = self.write_shape(id);
let mut updated = data.compositor_animation_frame_kinds.get();
if value {
updated |= kind as u8;
@@ -4693,7 +4687,7 @@ impl LayoutNodeArena {
// Scanning the document's dirty list per boundary would make cleanup quadratic.
let mut cursor = Some(root);
while let Some(node) = cursor {
- let data = self.write_shape(node);
+ let mut data = self.write_shape(node);
data.set_flags(data.flags.get() & !flags_to_clear);
self.remove_layout_update_flag_node(node);
cursor = self.next_node_in_layout_subtree_in_pre_order(root, node);
@@ -4722,7 +4716,7 @@ impl LayoutNodeArena {
index += 1;
continue;
}
- let data = self.write_shape(node);
+ let mut data = self.write_shape(node);
data.set_flags(data.flags.get() & !flags_to_clear);
self.remove_layout_update_flag_node(node);
}
@@ -5229,7 +5223,7 @@ impl LayoutNodeArena {
}
pub(crate) fn set_committed_fragment_link(
- &self,
+ &mut self,
id: NodeSlotId,
link: super::fragment_tree::FragmentLink,
geometry_epoch: Option<u32>,
@@ -5237,7 +5231,7 @@ impl LayoutNodeArena {
self.paintable_rows
.set_committed_fragment_link(id.slot_index(), id.generation(), geometry_epoch, link);
- let data = self.write_shape(id);
+ let mut data = self.write_shape(id);
data.set_flags(data.flags.get() | NodeFlag::HasCommittedFragmentLink as u32);
}
@@ -5268,19 +5262,22 @@ impl LayoutNodeArena {
)
}
- pub(crate) fn take_committed_fragment_link(&self, data: &NodeData) -> Option<super::fragment_tree::FragmentLink> {
- let (index, metadata) = self.slot_for_data(data);
+ pub(crate) fn take_committed_fragment_link(
+ &mut self,
+ id: NodeSlotId,
+ ) -> Option<super::fragment_tree::FragmentLink> {
let link = self
.paintable_rows
- .take_committed_fragment_link(index, metadata.generation);
+ .take_committed_fragment_link(id.slot_index(), id.generation());
+ let flags = self.data(id).flags.get();
assert_eq!(
- data.flags.get() & NodeFlag::HasCommittedFragmentLink as u32 != 0,
+ flags & NodeFlag::HasCommittedFragmentLink as u32 != 0,
link.is_some(),
"committed fragment link presence flag disagrees with the arena side table"
);
- let data = self.write_shape_of(data);
- data.set_flags(data.flags.get() & !(NodeFlag::HasCommittedFragmentLink as u32));
+ self.write_shape(id)
+ .set_flags(flags & !(NodeFlag::HasCommittedFragmentLink as u32));
link
}
@@ -5289,7 +5286,7 @@ impl LayoutNodeArena {
// Once that subtree is cleared, a later layout must rebuild it instead.
self.memo_invalidations.note_run_cache_entries_dropped(id.slot_index());
self.note_subtrees_of_self_and_ancestors_committed_anew(id);
- drop(self.take_committed_fragment_link(self.data(id)));
+ drop(self.take_committed_fragment_link(id));
}
fn text_node_state_mut(&mut self, id: NodeSlotId) -> TextStateMut<'_> {
@@ -5676,8 +5673,8 @@ impl LayoutNodeArena {
pub(crate) fn insert_child(&mut self, parent: NodeSlotId, child: NodeSlotId, before: NodeSlotId) {
assert_ne!(parent, child, "a layout node cannot become its own child");
- let parent_data = self.write_shape(parent);
- let child_data = self.write_shape(child);
+ let parent_data = self.data(parent);
+ let child_data = self.data(child);
let child_parent = child_data.parent.get();
let child_previous_sibling = child_data.previous_sibling.get();
@@ -5722,16 +5719,17 @@ impl LayoutNodeArena {
before_data.previous_sibling.get()
};
+ let mut child_data = self.write_shape(child);
child_data.set_parent(parent);
child_data.set_previous_sibling(previous);
child_data.set_next_sibling(before);
if previous.is_invalid() {
- parent_data.set_first_child(child);
+ self.write_shape(parent).set_first_child(child);
} else {
self.write_shape(previous).set_next_sibling(child);
}
if before.is_invalid() {
- parent_data.set_last_child(child);
+ self.write_shape(parent).set_last_child(child);
} else {
self.write_shape(before).set_previous_sibling(child);
}
@@ -5753,43 +5751,40 @@ impl LayoutNodeArena {
self.reset_cached_intrinsic_sizes_of_self_and_ancestors(parent);
}
- fn unlink_child(&self, parent: NodeSlotId, child: NodeSlotId) {
- let parent_data = self.write_shape(parent);
- let child_data = self.write_shape(child);
-
+ fn unlink_child(&mut self, parent: NodeSlotId, child: NodeSlotId) {
+ let child_data = self.data(child);
let child_parent = child_data.parent.get();
assert_eq!(child_parent, parent, "removed layout node is not a child of the parent");
let previous = child_data.previous_sibling.get();
let next = child_data.next_sibling.get();
if previous.is_invalid() {
- let first_child = parent_data.first_child.get();
+ let first_child = self.data(parent).first_child.get();
assert_eq!(first_child, child, "layout node child list lost its first child");
- parent_data.set_first_child(next);
+ self.write_shape(parent).set_first_child(next);
} else {
- let previous_data = self.write_shape(previous);
- let previous_next_sibling = previous_data.next_sibling.get();
+ let previous_next_sibling = self.data(previous).next_sibling.get();
assert_eq!(
previous_next_sibling, child,
"layout node sibling chain is inconsistent"
);
- previous_data.set_next_sibling(next);
+ self.write_shape(previous).set_next_sibling(next);
}
if next.is_invalid() {
- let last_child = parent_data.last_child.get();
+ let last_child = self.data(parent).last_child.get();
assert_eq!(last_child, child, "layout node child list lost its last child");
- parent_data.set_last_child(previous);
+ self.write_shape(parent).set_last_child(previous);
} else {
- let next_data = self.write_shape(next);
- let next_previous_sibling = next_data.previous_sibling.get();
+ let next_previous_sibling = self.data(next).previous_sibling.get();
assert_eq!(
next_previous_sibling, child,
"layout node sibling chain is inconsistent"
);
- next_data.set_previous_sibling(previous);
+ self.write_shape(next).set_previous_sibling(previous);
}
+ let mut child_data = self.write_shape(child);
child_data.set_parent(NodeSlotId::INVALID);
child_data.set_previous_sibling(NodeSlotId::INVALID);
child_data.set_next_sibling(NodeSlotId::INVALID);
@@ -5978,7 +5973,7 @@ impl LayoutNodeArena {
.chunks
.get_mut(index / SLOTS_PER_CHUNK)
.expect("invalid layout node arena slot ID");
- chunk.slot_mut(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &self.shape_writes)
+ chunk.slot_mut(index / SLOTS_PER_CHUNK, index % SLOTS_PER_CHUNK, &mut self.shape_writes)
}
fn metadata(&self, index: u32) -> &SlotMetadata {
@@ -7049,32 +7044,32 @@ mod tests {
let mut arena = LayoutNodeArena::new();
let node = arena.allocate_for_test().slot;
let current = |arena: &LayoutNodeArena| arena.with_current_committed_fragment(node, |fragment| fragment.node);
- let commit_from_layout = |arena: &LayoutNodeArena| {
+ let commit_from_layout = |arena: &mut LayoutNodeArena| {
arena.set_committed_fragment_link(
node,
fragment_tree::FragmentLink::for_test(node),
Some(arena.data(node).fragment_cache_epoch),
);
};
- commit_from_layout(&arena);
+ commit_from_layout(&mut arena);
assert_eq!(current(&arena), Some(node));
arena.bump_fragment_cache_epoch_of_self_and_ancestors(node);
assert_eq!(current(&arena), None);
- commit_from_layout(&arena);
+ commit_from_layout(&mut arena);
assert_eq!(current(&arena), Some(node));
- let moved = arena.take_committed_fragment_link(arena.data(node)).unwrap();
+ let moved = arena.take_committed_fragment_link(node).unwrap();
arena.set_committed_fragment_link(node, moved, None);
assert_eq!(current(&arena), None);
arena.begin_active_layout_pass();
- commit_from_layout(&arena);
+ commit_from_layout(&mut arena);
assert!(arena.leave_active_layout_pass());
assert_eq!(current(&arena), Some(node));
*arena.unpublished_facts_mut(node).fragment_cache_epoch = 0;
- commit_from_layout(&arena);
+ commit_from_layout(&mut arena);
*arena.unpublished_facts_mut(node).fragment_cache_epoch = u32::MAX;
arena.bump_fragment_cache_epoch_of_self_and_ancestors(node);
assert_eq!(arena.data(node).fragment_cache_epoch, 0);
@@ -7444,7 +7439,7 @@ mod tests {
arena.set_committed_fragment_link(old.slot, link, None);
let moved = arena
- .take_committed_fragment_link(arena.data(old.slot))
+ .take_committed_fragment_link(old.slot)
.expect("old slot must retain its committed fragment");
assert!(std::sync::Arc::ptr_eq(&moved.fragment, &retained_fragment));
arena.set_committed_fragment_link(new.slot, moved, None);
diff --git a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
index 9f8f968e538..09670129e24 100644
--- a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
@@ -1008,7 +1008,7 @@ mod tests {
arena.write_shape(svg_root.slot).set_kind(NodeKind::SVGSVGBox);
arena.insert_child(parent.slot, svg_root.slot, NodeSlotId::INVALID);
arena.populate_paintable_row(svg_root.slot);
- let plan_with_committed_containing_block = |arena: &LayoutNodeArena, containing_block| {
+ let plan_with_committed_containing_block = |arena: &mut LayoutNodeArena, containing_block| {
let mut link = crate::layout::fragment_tree::FragmentLink::for_test(svg_root.slot);
link.containing_block = containing_block;
arena.set_committed_fragment_link(svg_root.slot, link, None);
@@ -1016,13 +1016,13 @@ mod tests {
};
assert_eq!(
- plan_with_committed_containing_block(&arena, parent.slot),
+ plan_with_committed_containing_block(&mut arena, parent.slot),
Some(vec![super::PlannedBoundary {
root: svg_root.slot,
containing_block: parent.slot,
}])
);
- assert_eq!(plan_with_committed_containing_block(&arena, earlier_tree_box), None);
+ assert_eq!(plan_with_committed_containing_block(&mut arena, earlier_tree_box), None);
free_node(&mut arena, &parent);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index 5856b117057..1adb5594e23 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -2720,7 +2720,7 @@ fn update_principal_node_after_entry(
let new_data = arena.data(layout_node);
if node_facts::kind_is_box(old_data.kind.get())
&& node_facts::kind_is_box(new_data.kind.get())
- && let Some(link) = arena.take_committed_fragment_link(old_data)
+ && let Some(link) = arena.take_committed_fragment_link(old_layout_node)
{
arena.set_committed_fragment_link(layout_node, link, None);
}
@@ -3793,7 +3793,7 @@ impl<'a> TreeBuilderHost<'a> {
.arena()
.document_style_node()
.expect("a build names the document it lays out");
- self.arena().stamp_dom_paint_facts(slot, document);
+ self.arena_mut().stamp_dom_paint_facts(slot, document);
let unique_node_id = self
.arena()
.with_style_store(|engine| engine.element_unique_node_id(document));
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_shape.rs b/Libraries/LibWeb/Rust/src/layout/tree_shape.rs
index 01b8c3c3511..10fa56da0ea 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_shape.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_shape.rs
@@ -12,7 +12,7 @@
//! node chunks, and brings the rows of every node written since it last published up to date when
//! it publishes again, so publishing costs the nodes written since, not the whole tree.
//!
-//! The fields a [`PaintNode`] copies are [`ShapeCell`]s. They read like a `Cell`, but only a
+//! The fields a [`PaintNode`] copies are [`ShapeCell`]s. They read like plain fields, but only a
//! [`ShapeWriter`] or `&mut` writes one, and only a [`Chunk`] hands out either, marking the node in
//! the chunk when a write changes it. A write the next publication would miss does not compile.
@@ -20,107 +20,103 @@ use super::layout_node_arena::SLOTS_PER_CHUNK;
use super::node_data::{NodeData, NodeFlag, NodeKind, NodeSlotId, PaintNode, StylePayloadsRef};
use crate::cow_column::{ColumnSnapshot, CowColumn};
use crate::css::style::tree::StyleNodeID;
-use std::cell::Cell;
use std::ops::Deref;
-/// A field of a node the paint side reads. It reads like a `Cell`, and is written through a
+/// A field of a node the paint side reads. It reads like a plain field, and is written through a
/// [`ShapeWriter`] or through `&mut`, both of which mark the node in its chunk.
#[repr(transparent)]
-pub(crate) struct ShapeCell<T>(Cell<T>);
-
-impl<T: Copy> Clone for ShapeCell<T> {
- fn clone(&self) -> Self {
- Self(Cell::new(self.0.get()))
- }
-}
+#[derive(Clone)]
+pub(crate) struct ShapeCell<T>(T);
impl<T: Copy> ShapeCell<T> {
pub(crate) const fn new(value: T) -> Self {
- Self(Cell::new(value))
+ Self(value)
}
#[inline]
pub(crate) fn get(&self) -> T {
- self.0.get()
+ self.0
}
#[inline]
pub(crate) fn get_mut(&mut self) -> &mut T {
- self.0.get_mut()
+ &mut self.0
}
#[inline]
- fn set(&self, value: T) {
- self.0.set(value);
+ fn set(&mut self, value: T) {
+ self.0 = value;
}
}
/// How far the arena's nodes have been written, which tells a reader of the rows published before that they moved on.
#[derive(Clone, Default)]
pub(crate) struct ShapeWrites {
- all: Cell<u64>,
- identity: Cell<u64>,
- style: Cell<u64>,
+ all: u64,
+ identity: u64,
+ style: u64,
/// One bit per chunk holding a node marked since the last publication, so that publishing visits those alone.
- written_chunks: Vec<Cell<u64>>,
+ written_chunks: Vec<u64>,
}
impl ShapeWrites {
/// Every write that changed a node's shape, or the style record or node kept beside it.
pub(crate) fn all(&self) -> u64 {
- self.all.get()
+ self.all
}
/// The writes among them that changed what a row is: its slot's generation, its kind, what it is generated for,
/// its [`NodeFlag::IDENTITY`] flags, and the node whose style it carries. Installing a style changes none of it.
pub(crate) fn identity(&self) -> u64 {
- self.identity.get()
+ self.identity
}
/// The writes among them that gave a row a style record, but for a row they made, which changes what the row is.
pub(crate) fn style(&self) -> u64 {
- self.style.get()
+ self.style
}
- fn note(&self) {
- self.all.set(self.all.get() + 1);
+ fn note(&mut self) {
+ self.all += 1;
}
/// Notes a write that gave a row a style record.
- pub(crate) fn note_style(&self) {
+ pub(crate) fn note_style(&mut self) {
self.note();
- self.style.set(self.style.get() + 1);
+ self.style += 1;
}
/// Notes a write that changed what a row is.
- pub(crate) fn note_identity(&self) {
+ pub(crate) fn note_identity(&mut self) {
self.note();
- self.identity.set(self.identity.get() + 1);
+ self.identity += 1;
}
/// Makes room for the chunk at `chunk_index`, whose nodes are all marked as it is made.
pub(crate) fn add_chunk(&mut self, chunk_index: usize) {
- self.written_chunks.resize_with(chunk_index / 64 + 1, Cell::default);
+ self.written_chunks.resize(chunk_index / 64 + 1, 0);
self.note_chunk_written(chunk_index);
}
- fn note_chunk_written(&self, chunk_index: usize) {
- let word = &self.written_chunks[chunk_index / 64];
- word.set(word.get() | 1 << (chunk_index % 64));
+ fn note_chunk_written(&mut self, chunk_index: usize) {
+ self.written_chunks[chunk_index / 64] |= 1 << (chunk_index % 64);
}
/// The chunks holding a node marked since the last publication, which it forgets.
- fn take_written_chunks(&self) -> impl Iterator<Item = usize> + '_ {
- self.written_chunks.iter().enumerate().flat_map(|(word_index, word)| {
- let mut chunks = word.replace(0);
- std::iter::from_fn(move || {
- (chunks != 0).then(|| {
- let chunk = chunks.trailing_zeros() as usize;
- chunks &= chunks - 1;
- word_index * 64 + chunk
+ fn take_written_chunks(&mut self) -> impl Iterator<Item = usize> + '_ {
+ self.written_chunks
+ .iter_mut()
+ .enumerate()
+ .flat_map(|(word_index, word)| {
+ let mut chunks = std::mem::take(word);
+ std::iter::from_fn(move || {
+ (chunks != 0).then(|| {
+ let chunk = chunks.trailing_zeros() as usize;
+ chunks &= chunks - 1;
+ word_index * 64 + chunk
+ })
})
})
- })
}
}
@@ -129,11 +125,11 @@ impl ShapeWrites {
/// node's [`NodeData`], so the fields the paint side does not read are written through it as
/// before.
pub(crate) struct ShapeWriter<'a> {
- data: &'a NodeData,
- written_rows: &'a Cell<u64>,
+ data: &'a mut NodeData,
+ written_rows: &'a mut u64,
row_bit: u64,
chunk_index: usize,
- writes: &'a ShapeWrites,
+ writes: &'a mut ShapeWrites,
}
impl Deref for ShapeWriter<'_> {
@@ -145,87 +141,87 @@ impl Deref for ShapeWriter<'_> {
}
impl ShapeWriter<'_> {
- /// Writes a field, marking the node only for a value the field does not already hold.
+ /// Writes the field `field` selects, marking the node only for a value the field does not already hold.
#[inline]
- fn write<T: Copy + PartialEq>(&self, field: &ShapeCell<T>, value: T) {
- if field.get() != value {
+ fn write<T: Copy + PartialEq>(&mut self, field: fn(&mut NodeData) -> &mut ShapeCell<T>, value: T) {
+ if field(self.data).get() != value {
self.mark();
- field.set(value);
+ field(self.data).set(value);
}
}
/// Like [`Self::write`], for a field that says what the row is.
#[inline]
- fn write_identity<T: Copy + PartialEq>(&self, field: &ShapeCell<T>, value: T) {
- if field.get() != value {
+ fn write_identity<T: Copy + PartialEq>(&mut self, field: fn(&mut NodeData) -> &mut ShapeCell<T>, value: T) {
+ if field(self.data).get() != value {
self.mark_identity();
- field.set(value);
+ field(self.data).set(value);
}
}
/// Marks the node for the next publication, for a write to what the arena keeps beside it.
- pub(crate) fn mark(&self) {
+ pub(crate) fn mark(&mut self) {
self.mark_row();
self.writes.note();
}
/// Like [`Self::mark`], for a write that changes what the row is.
- pub(crate) fn mark_identity(&self) {
+ pub(crate) fn mark_identity(&mut self) {
self.mark_row();
self.writes.note_identity();
}
- fn mark_row(&self) {
- self.written_rows.set(self.written_rows.get() | self.row_bit);
+ fn mark_row(&mut self) {
+ *self.written_rows |= self.row_bit;
self.writes.note_chunk_written(self.chunk_index);
}
- pub(crate) fn set_parent(&self, parent: NodeSlotId) {
- self.write(&self.data.parent, parent);
+ pub(crate) fn set_parent(&mut self, parent: NodeSlotId) {
+ self.write(|data| &mut data.parent, parent);
}
- pub(crate) fn set_first_child(&self, child: NodeSlotId) {
- self.write(&self.data.first_child, child);
+ pub(crate) fn set_first_child(&mut self, child: NodeSlotId) {
+ self.write(|data| &mut data.first_child, child);
}
- pub(crate) fn set_last_child(&self, child: NodeSlotId) {
- self.write(&self.data.last_child, child);
+ pub(crate) fn set_last_child(&mut self, child: NodeSlotId) {
+ self.write(|data| &mut data.last_child, child);
}
- pub(crate) fn set_previous_sibling(&self, sibling: NodeSlotId) {
- self.write(&self.data.previous_sibling, sibling);
+ pub(crate) fn set_previous_sibling(&mut self, sibling: NodeSlotId) {
+ self.write(|data| &mut data.previous_sibling, sibling);
}
- pub(crate) fn set_next_sibling(&self, sibling: NodeSlotId) {
- self.write(&self.data.next_sibling, sibling);
+ pub(crate) fn set_next_sibling(&mut self, sibling: NodeSlotId) {
+ self.write(|data| &mut data.next_sibling, sibling);
}
- pub(crate) fn set_kind(&self, kind: NodeKind) {
- self.write_identity(&self.data.kind, kind);
+ pub(crate) fn set_kind(&mut self, kind: NodeKind) {
+ self.write_identity(|data| &mut data.kind, kind);
}
- pub(crate) fn set_generated_for(&self, generated_for: u8) {
- self.write_identity(&self.data.generated_for, generated_for);
+ pub(crate) fn set_generated_for(&mut self, generated_for: u8) {
+ self.write_identity(|data| &mut data.generated_for, generated_for);
}
- pub(crate) fn set_flags(&self, flags: u32) {
+ pub(crate) fn set_flags(&mut self, flags: u32) {
if (self.data.flags.get() ^ flags) & NodeFlag::IDENTITY != 0 {
- self.write_identity(&self.data.flags, flags);
+ self.write_identity(|data| &mut data.flags, flags);
} else {
- self.write(&self.data.flags, flags);
+ self.write(|data| &mut data.flags, flags);
}
}
- pub(crate) fn set_dom_paint_facts(&self, facts: u8) {
- self.write(&self.data.dom_paint_facts, facts);
+ pub(crate) fn set_dom_paint_facts(&mut self, facts: u8) {
+ self.write(|data| &mut data.dom_paint_facts, facts);
}
- pub(crate) fn set_compositor_animation_frame_kinds(&self, kinds: u8) {
- self.write(&self.data.compositor_animation_frame_kinds, kinds);
+ pub(crate) fn set_compositor_animation_frame_kinds(&mut self, kinds: u8) {
+ self.write(|data| &mut data.compositor_animation_frame_kinds, kinds);
}
- pub(crate) fn set_style(&self, style: StylePayloadsRef) {
- self.write(&self.data.style, style);
+ pub(crate) fn set_style(&mut self, style: StylePayloadsRef) {
+ self.write(|data| &mut data.style, style);
}
}
@@ -238,7 +234,7 @@ impl ShapeWriter<'_> {
pub(crate) struct Chunk {
slots: [NodeData; SLOTS_PER_CHUNK],
/// One bit per node whose shape may have been written since the paint side last looked.
- written_rows: [Cell<u64>; SLOTS_PER_CHUNK / 64],
+ written_rows: [u64; SLOTS_PER_CHUNK / 64],
}
impl Chunk {
@@ -252,7 +248,7 @@ impl Chunk {
for offset in 0..SLOTS_PER_CHUNK {
(&raw mut (*slots)[offset]).write(NodeData::default());
}
- (&raw mut (*chunk.as_mut_ptr()).written_rows).write(std::array::from_fn(|_| Cell::new(u64::MAX)));
+ (&raw mut (*chunk.as_mut_ptr()).written_rows).write([u64::MAX; SLOTS_PER_CHUNK / 64]);
chunk.assume_init()
}
}
@@ -275,22 +271,22 @@ impl Chunk {
/// The node at `offset` of this chunk, the arena's chunk at `chunk_index`, marked for the next publication.
#[inline]
- pub(crate) fn slot_mut(&mut self, chunk_index: usize, offset: usize, writes: &ShapeWrites) -> &mut NodeData {
- *self.written_rows[offset / 64].get_mut() |= 1 << (offset % 64);
+ pub(crate) fn slot_mut(&mut self, chunk_index: usize, offset: usize, writes: &mut ShapeWrites) -> &mut NodeData {
+ self.written_rows[offset / 64] |= 1 << (offset % 64);
writes.note_chunk_written(chunk_index);
&mut self.slots[offset]
}
#[inline]
pub(crate) fn write_shape<'a>(
- &'a self,
+ &'a mut self,
chunk_index: usize,
offset: usize,
- writes: &'a ShapeWrites,
+ writes: &'a mut ShapeWrites,
) -> ShapeWriter<'a> {
ShapeWriter {
- data: &self.slots[offset],
- written_rows: &self.written_rows[offset / 64],
+ data: &mut self.slots[offset],
+ written_rows: &mut self.written_rows[offset / 64],
row_bit: 1 << (offset % 64),
chunk_index,
writes,
@@ -300,15 +296,13 @@ impl Chunk {
/// Whether the node at `offset` may have been written since the marks were last taken.
#[cfg(test)]
pub(crate) fn is_marked(&self, offset: usize) -> bool {
- self.written_rows[offset / 64].get() & (1 << (offset % 64)) != 0
+ self.written_rows[offset / 64] & (1 << (offset % 64)) != 0
}
/// Clears every mark.
#[cfg(test)]
- pub(crate) fn clear_marks(&self) {
- for word in &self.written_rows {
- word.set(0);
- }
+ pub(crate) fn clear_marks(&mut self) {
+ self.written_rows = [0; SLOTS_PER_CHUNK / 64];
}
}
@@ -329,17 +323,17 @@ impl TreeShape {
/// publication shares is copied only for a change.
pub(crate) fn publish(
&mut self,
- chunks: &[Box<Chunk>],
+ chunks: &mut [Box<Chunk>],
style_records: &[u64],
style_nodes: &[Option<StyleNodeID>],
style_is_derived: impl Fn(usize) -> bool,
- writes: &ShapeWrites,
+ writes: &mut ShapeWrites,
) -> ColumnSnapshot<PaintNode, PUBLISHED_ROWS_PER_CHUNK> {
self.nodes.grow_to(chunks.len() * SLOTS_PER_CHUNK);
for chunk_index in writes.take_written_chunks() {
- let chunk = &chunks[chunk_index];
- for (word_index, word) in chunk.written_rows.iter().enumerate() {
- let mut written = word.replace(0);
+ let chunk = &mut chunks[chunk_index];
+ for word_index in 0..chunk.written_rows.len() {
+ let mut written = std::mem::take(&mut chunk.written_rows[word_index]);
while written != 0 {
let offset = word_index * 64 + written.trailing_zeros() as usize;
written &= written - 1;
@@ -358,6 +352,14 @@ impl TreeShape {
}
}
+const _: () = {
+ const fn assert_sync<T: Sync>() {}
+ assert_sync::<NodeData>();
+ assert_sync::<Chunk>();
+ assert_sync::<ShapeWrites>();
+ assert_sync::<TreeShape>();
+};
+
#[cfg(test)]
mod tests {
use super::*;
@@ -370,39 +372,40 @@ mod tests {
assert!(chunk.is_marked(0), "a new chunk's nodes are all marked");
chunk.clear_marks();
- let shape = chunk.write_shape(0, 3, &writes);
+ let mut shape = chunk.write_shape(0, 3, &mut writes);
shape.set_kind(NodeKind::Unset);
shape.set_parent(NodeSlotId::INVALID);
shape.set_flags(0);
assert!(!chunk.is_marked(3));
assert_eq!(writes.all(), 0);
- chunk.write_shape(0, 3, &writes).set_kind(NodeKind::BlockContainer);
+ chunk.write_shape(0, 3, &mut writes).set_kind(NodeKind::BlockContainer);
assert!(chunk.is_marked(3));
assert!(!chunk.is_marked(2) && !chunk.is_marked(4));
assert_eq!(writes.all(), 1);
- chunk.write_shape(0, 70, &writes).set_dom_paint_facts(1);
+ chunk.write_shape(0, 70, &mut writes).set_dom_paint_facts(1);
assert!(chunk.is_marked(70));
assert!(!chunk.is_marked(71));
- *chunk.slot_mut(0, 100, &writes).slot_generation.get_mut() = 1;
+ *chunk.slot_mut(0, 100, &mut writes).slot_generation.get_mut() = 1;
assert!(chunk.is_marked(100));
assert_eq!(chunk.slot(3).kind.get(), NodeKind::BlockContainer);
}
#[test]
fn only_a_write_of_what_a_row_is_counts_as_an_identity_write() {
- let chunk = Chunk::new();
+ let mut chunk = Chunk::new();
let mut writes = ShapeWrites::default();
writes.add_chunk(0);
- let shape = chunk.write_shape(0, 3, &writes);
+ let mut shape = chunk.write_shape(0, 3, &mut writes);
shape.set_parent(NodeSlotId::new(1, 1));
shape.set_style(StylePayloadsRef::new(std::ptr::NonNull::dangling().as_ptr()));
shape.set_flags(NodeFlag::HasStyle as u32);
shape.mark();
assert_eq!((writes.all(), writes.identity()), (4, 0));
+ let mut shape = chunk.write_shape(0, 3, &mut writes);
shape.set_flags(NodeFlag::HasStyle as u32 | NodeFlag::IsBody as u32);
shape.set_kind(NodeKind::BlockContainer);
shape.set_generated_for(1);
@@ -412,15 +415,15 @@ mod tests {
#[test]
fn a_publication_visits_only_the_chunks_holding_a_marked_node() {
- let chunk = Chunk::new();
+ let mut chunk = Chunk::new();
let mut writes = ShapeWrites::default();
for chunk_index in [0, 1, 70] {
writes.add_chunk(chunk_index);
}
assert_eq!(writes.take_written_chunks().collect::<Vec<_>>(), [0, 1, 70]);
assert_eq!(writes.take_written_chunks().count(), 0);
- chunk.write_shape(70, 7, &writes).set_kind(NodeKind::BlockContainer);
- chunk.write_shape(1, 8, &writes).set_kind(NodeKind::BlockContainer);
+ chunk.write_shape(70, 7, &mut writes).set_kind(NodeKind::BlockContainer);
+ chunk.write_shape(1, 8, &mut writes).set_kind(NodeKind::BlockContainer);
assert_eq!(writes.take_written_chunks().collect::<Vec<_>>(), [1, 70]);
}
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
index ea9995813f4..fbfb97785b7 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
@@ -371,12 +371,10 @@ impl<'a> PaintableCommit<'a> {
data.content_size = new_content_size;
data.offset = new_offset;
}
- self.arena().set_committed_fragment_link(
- node,
- link.clone(),
- // A layout pass bumps no epoch, so its geometry is laid out against the epoch the node has now.
- Some(self.arena().data(node).fragment_cache_epoch),
- );
+ // A layout pass bumps no epoch, so its geometry is laid out against the epoch the node has now.
+ let epoch_of_laid_out_geometry = self.arena().data(node).fragment_cache_epoch;
+ self.arena_mut()
+ .set_committed_fragment_link(node, link.clone(), Some(epoch_of_laid_out_geometry));
ReplacedCommittedFragmentLink {
content_size_change,
line_root_changes: LineRootChanges {
diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rs
index 80fc6c92110..52db21dbb7e 100644
--- a/Libraries/LibWeb/Rust/src/render_state.rs
+++ b/Libraries/LibWeb/Rust/src/render_state.rs
@@ -1177,7 +1177,7 @@ mod tests {
host.fresh_rows(ScriptForcedRead::for_test());
host.with_arena_untold_for_test(|arena| {
- let shape = arena.write_shape(row);
+ let mut shape = arena.write_shape(row);
shape.set_style(StylePayloadsRef::new(NonNull::dangling().as_ptr()));
shape.mark();
});