Truly independent web browser
LibWeb: Rank style reactions with dense columns instead of hash maps
Andreas Kling14b33f39cee5
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex 8b5fbc8f7f1..02068a67268 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -3215,7 +3215,7 @@ pub(super) fn sort_style_deltas_for_direct_application(engine: &StyleEngine, del|delta: &FfiStyleDelta| StyleNodeID::from_raw(delta.style_node).expect("a style delta must name an element");// Small batches cost less to compare directly. A large batch names its dependency order once instead of walking// both ancestor chains in every sort comparison.- if deltas.len() > 32 {+ if engine.tree.style_reaction_order_ranks_pay_off(deltas.len()) {let ranks = engine.tree.style_reaction_order_ranks(deltas.iter().map(node));deltas.sort_unstable_by_key(|delta| (ranks[&node(delta)], pseudo_rank(delta)));return;diff --git a/Libraries/LibWeb/Rust/src/css/style/flush.rs b/Libraries/LibWeb/Rust/src/css/style/flush.rsindex 18faab499ff..c4964f41dcd 100644--- a/Libraries/LibWeb/Rust/src/css/style/flush.rs+++ b/Libraries/LibWeb/Rust/src/css/style/flush.rs@@ -1983,13 +1983,19 @@ impl StyleEngine {// The rows settle in the order the host applies them, each after the rows of the batch// it inherits from, which it then finds settled rather than still to come.if published_nodes.len() > 1 {- let ranks = self.tree.style_reaction_order_ranks(published_nodes.iter().copied());let mut rows: Vec<_> = published_nodes.iter().copied().zip(previous_cascade_inputs.iter().copied()).collect();- rows.sort_unstable_by_key(|(node, _)| ranks[node]);+ if self.tree.style_reaction_order_ranks_pay_off(rows.len()) {+ let ranks = self.tree.style_reaction_order_ranks(published_nodes.iter().copied());+ rows.sort_unstable_by_key(|(node, _)| ranks[node]);+ } else {+ rows.sort_unstable_by(|(first, _), (second, _)| {+ self.tree.compare_style_reaction_order(*first, *second)+ });+ }for (index, (node, previous_cascade_input)) in rows.into_iter().enumerate() {published_nodes[index] = node;previous_cascade_inputs[index] = previous_cascade_input;diff --git a/Libraries/LibWeb/Rust/src/css/style/tree.rs b/Libraries/LibWeb/Rust/src/css/style/tree.rsindex 6c3d5c67fa2..fe347d1578a 100644--- a/Libraries/LibWeb/Rust/src/css/style/tree.rs+++ b/Libraries/LibWeb/Rust/src/css/style/tree.rs@@ -1785,17 +1785,31 @@ impl StyleNodeTree {/// Rank a batch in the same dependency order as `compare_style_reaction_order`./// Each ancestor is visited once, even for a deep chain of reacting descendants.- pub fn style_reaction_order_ranks(- &self,- nodes: impl IntoIterator<Item = StyleNodeID>,- ) -> HashMap<StyleNodeID, usize> {- let mut seen = HashSet::default();- let mut children: HashMap<StyleNodeID, Vec<(u8, StyleNodeID)>> = HashMap::default();+ /// Whether ranking a batch of `count` nodes with [`Self::style_reaction_order_ranks`] costs less than comparing+ /// them pairwise with [`Self::compare_style_reaction_order`]. The ranks are as long as the highest element slot+ /// they reach, so a small batch, or one that is small beside the tree, is compared directly instead.+ #[must_use]+ pub fn style_reaction_order_ranks_pay_off(&self, count: usize) -> bool {+ count > 32 && count.saturating_mul(64) >= self.parent.len()+ }++ pub fn style_reaction_order_ranks(&self, nodes: impl IntoIterator<Item = StyleNodeID>) -> StyleReactionOrderRanks {+ // NB: Each rank starts out unranked, becomes seen once the node's chain was walked, and is+ // assigned in the walk down from the roots.+ let mut ranks = Vec::new();+ let mark_seen = |ranks: &mut Vec<usize>, node: StyleNodeID| {+ let slot = self.element_index(node);+ if ranks.len() <= slot {+ ranks.resize(slot + 1, StyleReactionOrderRanks::UNRANKED);+ }+ std::mem::replace(&mut ranks[slot], StyleReactionOrderRanks::SEEN) != StyleReactionOrderRanks::SEEN+ };+ let mut edges: Vec<(StyleNodeID, u8, StyleNodeID)> = Vec::new();let mut roots = Vec::new();for mut node in nodes {- while seen.insert(node) {+ while mark_seen(&mut ranks, node) {if let Some((parent, branch)) = self.style_reaction_parent(node) {- children.entry(parent).or_default().push((branch, node));+ edges.push((parent, branch, node));node = parent;} else {roots.push(node);@@ -1803,20 +1817,35 @@ impl StyleNodeTree {}}}- // The existing order uses identity within each branch, not DOM sibling order.+ // NB: The existing order uses identity within each branch, not DOM sibling order. The edges of+ // each parent are a run, with its children in descending order so that the walk, which+ // takes the last pending node first, visits them in ascending order.roots.sort_unstable_by(|first, second| second.cmp(first));- for children in children.values_mut() {- children.sort_unstable_by(|first, second| second.cmp(first));+ edges.sort_unstable_by(|first, second| {+ first+ .0+ .cmp(&second.0)+ .then_with(|| (second.1, second.2).cmp(&(first.1, first.2)))+ });+ // NB: Where each parent's run of edges starts, and where it ends.+ let mut child_runs = vec![(0_u32, 0_u32); ranks.len()];+ for (index, &(parent, _, _)) in edges.iter().enumerate() {+ let run = &mut child_runs[self.element_index(parent)];+ if run.0 == run.1 {+ run.0 = index as u32;+ }+ run.1 = index as u32 + 1;}- let mut ranks = HashMap::default();+ let mut count = 0;let mut pending = roots;while let Some(node) = pending.pop() {- ranks.insert(node, ranks.len());- if let Some(children) = children.get(&node) {- pending.extend(children.iter().map(|&(_, child)| child));- }+ let slot = self.element_index(node);+ ranks[slot] = count;+ count += 1;+ let (start, end) = child_runs[slot];+ pending.extend(edges[start as usize..end as usize].iter().map(|&(_, _, child)| child));}- ranks+ StyleReactionOrderRanks { ranks }}fn style_reaction_parent(&self, node: StyleNodeID) -> Option<(StyleNodeID, u8)> {@@ -2075,6 +2104,32 @@ impl Iterator for Preorder<'_> {}}+/// Where each node of a batch, and each node on the chains above them, stands in the order style+/// reactions apply, indexed by the node.+pub struct StyleReactionOrderRanks {+ ranks: Vec<usize>,+}++impl StyleReactionOrderRanks {+ const UNRANKED: usize = usize::MAX;+ const SEEN: usize = usize::MAX - 1;++ #[cfg(test)]+ fn len(&self) -> usize {+ self.ranks.iter().filter(|&&rank| rank < Self::SEEN).count()+ }+}++impl std::ops::Index<&StyleNodeID> for StyleReactionOrderRanks {+ type Output = usize;++ fn index(&self, node: &StyleNodeID) -> &usize {+ let rank = &self.ranks[node.element_slot()];+ assert!(*rank < Self::SEEN, "a node outside the ranked batch has no rank");+ rank+ }+}+#[cfg(test)]mod tests {use super::*;