Truly independent web browser
LibWeb: Keep deferred element style inputs in an ordered map
Aliaksandr Kalenik78f64e16c190
diff --git a/Libraries/LibWeb/Rust/src/css/style/flush.rs b/Libraries/LibWeb/Rust/src/css/style/flush.rsindex 805e89483a0..9d49c2dfc4d 100644--- a/Libraries/LibWeb/Rust/src/css/style/flush.rs+++ b/Libraries/LibWeb/Rust/src/css/style/flush.rs@@ -473,8 +473,18 @@ impl StyleEngine {&& !self.host.program_staging.is_dirty()&& self.host.sheet_rule_replacement.is_none();self.host.deferred_element_style_inputs_moved |= !self.host.deferred_element_style_inputs.is_empty();- for input in std::mem::take(&mut self.host.deferred_element_style_inputs) {- self.record_input(input.key, input.old, input.new);+ for (node, input) in std::mem::take(&mut self.host.deferred_element_style_inputs) {+ self.record_input(+ InputKey::ElementStyleInput(node),+ InputValue::ElementStyleInput {+ reaction: 0,+ inherited_style_groups: 0,+ },+ InputValue::ElementStyleInput {+ reaction: input.reaction,+ inherited_style_groups: input.inherited_style_groups,+ },+ );}self.host.deferred_element_style_inputs_are_pending = false;let inputs_moved = std::mem::take(&mut self.retained.row_inputs_moved);diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rsindex 22d3fa50af0..0d78c1f040d 100644--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs@@ -2176,7 +2176,7 @@ impl StyleEngine {journal: NormalizationJournal::new(),deferred_geometry_journal: NormalizationJournal::new(),flushing_deferred_geometry_journal: false,- deferred_element_style_inputs: Vec::new(),+ deferred_element_style_inputs: Default::default(),deferred_element_style_inputs_moved: false,deferred_element_style_inputs_are_pending: false,environment_move_changed_names: Default::default(),@@ -2381,15 +2381,10 @@ impl StyleEngine {/// the merged inherited style groups in the next, or zero when nothing was folded./// The reaction the element `node` owes as an element style input the engine defers, if it owes one.pub(crate) fn deferred_element_style_reaction(&self, node: StyleNodeID) -> Option<u8> {- let index = self- .host+ self.host.deferred_element_style_inputs- .binary_search_by_key(&InputKey::ElementStyleInput(node), |pending| pending.key)- .ok()?;- match self.host.deferred_element_style_inputs[index].new {- InputValue::ElementStyleInput { reaction, .. } => Some(reaction),- _ => None,- }+ .get(&node)+ .map(|pending| pending.reaction)}pub fn absorb_element_style_input(@@ -2399,19 +2394,12 @@ impl StyleEngine {inherited_style_groups: u8,absorbs_any: bool,) -> u32 {- let Ok(index) = self- .host- .deferred_element_style_inputs- .binary_search_by_key(&InputKey::ElementStyleInput(node), |pending| pending.key)- else {- return 0;- };- let InputValue::ElementStyleInput {+ let Some(&DeferredElementStyleInput {reaction: pending_reaction,inherited_style_groups: pending_inherited_style_groups,- } = self.host.deferred_element_style_inputs[index].new+ }) = self.host.deferred_element_style_inputs.get(&node)else {- unreachable!();+ return 0;};// A descendant recompute asks nothing of the element its own reaction does not answer, and// the applied reaction carries what it asks of the descendants to the element's children.@@ -2421,7 +2409,7 @@ impl StyleEngine {{return 0;}- self.host.deferred_element_style_inputs.remove(index);+ self.host.deferred_element_style_inputs.remove(&node);self.host.deferred_element_style_inputs_moved = true;u32::from(reaction | pending_reaction)| (u32::from(inherited_style_groups | pending_inherited_style_groups) << 8)@@ -2438,28 +2426,14 @@ impl StyleEngine {self.host.deferred_element_style_inputs.iter()- .filter_map(|input| match (input.key, input.new) {- (- InputKey::ElementStyleInput(node),- InputValue::ElementStyleInput {- reaction,- inherited_style_groups,- },- ) => Some((node, reaction, inherited_style_groups)),- _ => None,- })+ .map(|(&node, pending)| (node, pending.reaction, pending.inherited_style_groups)).collect(),)}/// Drop the style input an element owes: C++ computed the element's style, which answers it.pub fn consume_element_style_input(&mut self, node: StyleNodeID) {- if let Ok(index) = self- .host- .deferred_element_style_inputs- .binary_search_by_key(&InputKey::ElementStyleInput(node), |pending| pending.key)- {- self.host.deferred_element_style_inputs.remove(index);+ if self.host.deferred_element_style_inputs.remove(&node).is_some() {self.host.deferred_element_style_inputs_moved = true;}}@@ -2467,26 +2441,15 @@ impl StyleEngine {/// Drop the style input an element owes but the descendant recompute it carries: a demand/// answers the element's own style, which reaches none of its descendants.pub(super) fn consume_element_style_input_but_descendants(&mut self, node: StyleNodeID) {- let Ok(index) = self- .host- .deferred_element_style_inputs- .binary_search_by_key(&InputKey::ElementStyleInput(node), |pending| pending.key)- else {+ let Some(pending) = self.host.deferred_element_style_inputs.get_mut(&node) else {return;};self.host.deferred_element_style_inputs_moved = true;- let InputValue::ElementStyleInput {- reaction,- inherited_style_groups,- } = &mut self.host.deferred_element_style_inputs[index].new- else {- unreachable!();- };- if *reaction & transaction::STYLE_REACTION_RECOMPUTE_DESCENDANT_STYLES != 0 {- *reaction = transaction::STYLE_REACTION_RECOMPUTE_DESCENDANT_STYLES;- *inherited_style_groups = 0;+ if pending.reaction & transaction::STYLE_REACTION_RECOMPUTE_DESCENDANT_STYLES != 0 {+ pending.reaction = transaction::STYLE_REACTION_RECOMPUTE_DESCENDANT_STYLES;+ pending.inherited_style_groups = 0;} else {- self.host.deferred_element_style_inputs.remove(index);+ self.host.deferred_element_style_inputs.remove(&node);}}@@ -3608,12 +3571,9 @@ impl RetainedState {}impl HostState {- /// Whether `node` still owes a deferred style input. The deferred inputs are kept sorted by- /// key, so this is a binary search.+ /// Whether `node` still owes a deferred style input.pub(super) fn owes_element_style_input(&self, node: StyleNodeID) -> bool {- self.deferred_element_style_inputs- .binary_search_by_key(&InputKey::ElementStyleInput(node), |pending| pending.key)- .is_ok()+ self.deferred_element_style_inputs.contains_key(&node)}}diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rsindex 336f04a3309..483d2a1d172 100644--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs@@ -1097,6 +1097,12 @@ pub struct RetainedState {diagnostic_plan_capture: Option<DiagnosticPlanCapture>,}+#[derive(Clone, Copy)]+struct DeferredElementStyleInput {+ reaction: u8,+ inherited_style_groups: u8,+}+/// Host-facing engine state: C++ ownership and journal intake./// Never reachable from an evaluation step.#[derive(Clone)]@@ -1113,7 +1119,7 @@ pub struct HostState {deferred_geometry_journal: NormalizationJournal,flushing_deferred_geometry_journal: bool,/// Exact element reactions retained across rootless flushes until a style root can consume them.- deferred_element_style_inputs: Vec<NormalizedInput>,+ deferred_element_style_inputs: std::collections::BTreeMap<StyleNodeID, DeferredElementStyleInput>,/// Whether the deferred element style inputs moved since the document's host last took them.deferred_element_style_inputs_moved: bool,/// Whether the deferred element style inputs are owed to the next transaction, as opposed todiff --git a/Libraries/LibWeb/Rust/src/css/style/ordering.rs b/Libraries/LibWeb/Rust/src/css/style/ordering.rsindex 2e8807a48cd..63b78eee11f 100644--- a/Libraries/LibWeb/Rust/src/css/style/ordering.rs+++ b/Libraries/LibWeb/Rust/src/css/style/ordering.rs@@ -2097,38 +2097,16 @@ impl StyleEngine {/// Merge one element style input into the deferred inputs, which are kept sorted by key.pub(crate) fn defer_element_style_input(&mut self, node: StyleNodeID, reaction: u8, inherited_style_groups: u8) {self.host.deferred_element_style_inputs_moved = true;- let key = InputKey::ElementStyleInput(node);- match self+ let pending = self.host.deferred_element_style_inputs- .binary_search_by_key(&key, |pending| pending.key)- {- Ok(index) => {- let InputValue::ElementStyleInput {- reaction: pending_reaction,- inherited_style_groups: pending_inherited_style_groups,- } = &mut self.host.deferred_element_style_inputs[index].new- else {- unreachable!();- };- *pending_reaction |= reaction;- *pending_inherited_style_groups |= inherited_style_groups;- }- Err(index) => self.host.deferred_element_style_inputs.insert(- index,- NormalizedInput {- key,- old: InputValue::ElementStyleInput {- reaction: 0,- inherited_style_groups: 0,- },- new: InputValue::ElementStyleInput {- reaction,- inherited_style_groups,- },- },- ),- }+ .entry(node)+ .or_insert(DeferredElementStyleInput {+ reaction: 0,+ inherited_style_groups: 0,+ });+ pending.reaction |= reaction;+ pending.inherited_style_groups |= inherited_style_groups;}/// Release a drained transaction's scratch charge.diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rsindex 486194080d1..f867049474b 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs@@ -204,8 +204,8 @@ impl RetainedState {let owing: HashSet<StyleNodeID> = host.journal.inputs()- .chain(host.deferred_element_style_inputs.iter().copied()).filter_map(|input| input.key.style_node())+ .chain(host.deferred_element_style_inputs.keys().copied()).collect();owing.is_empty()|| !std::iter::successors(self.tree.flat_tree_parent(node), |&parent| {