Skip to content

Truly independent web browser

LibWeb: Summarize a cascade state's written winners once per state

Andreas Kling6f89c7f1459d
diff --git a/Libraries/LibWeb/Rust/src/css/style/cascade.rs b/Libraries/LibWeb/Rust/src/css/style/cascade.rs
index d68514141d2..acedf7e1d6b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/cascade.rs
@@ -37,7 +37,7 @@ use super::intern_table::content_hash;
use smallvec::SmallVec;
use std::hash::Hash;
use std::hash::Hasher;
-use std::sync::atomic::{AtomicU8, Ordering};
+use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
use crate::css::cascaded_properties::CascadeOrigin;
@@ -1418,6 +1418,21 @@ pub(super) const STATE_READS_ELEMENT_RANDOM_BASE: u8 = 1 << 5;
pub(super) const STATE_READS_CONTAINER_UNITS: u8 = 1 << 6;
/// What a state's winners read has been decided.
const STATE_READS_DECIDED: u8 = 1 << 7;
+/// A state's summary has been decided.
+const STATE_SUMMARY_DECIDED: u64 = 1 << 63;
+/// A state's summary holds a winner written with a substitution.
+const STATE_SUMMARY_HAS_SUBSTITUTIONS: u64 = 1 << 32;
+
+/// What the engine asks of a state's winners' written values for each element it computes a record for, which depends
+/// on the winners alone.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub(super) struct StateSummary {
+ /// The style groups of each non-inherited property the state declares `inherit` for, and every group for a value
+ /// the engine cannot see the spelling of.
+ pub(super) explicitly_inherited_groups: u32,
+ /// Whether a longhand winner is written with a substitution.
+ pub(super) has_substitutions: bool,
+}
/// Interned per-node winning declarations.
///
@@ -1440,6 +1455,9 @@ pub struct WinnerGroups {
/// first time it is asked and `STATE_READS_DECIDED` from then on. A state's winners never
/// change, and neither does what they read.
state_reads: Vec<crate::fork::ForkCopied<AtomicU8>>,
+ /// Each state's [`StateSummary`], packed, decided the first time it is asked where every winner's written value was
+ /// found, and `STATE_SUMMARY_DECIDED` from then on.
+ state_summaries: Vec<crate::fork::ForkCopied<AtomicU64>>,
groups: InternTable<WinnerGroupID, Box<[SemanticPropertyWinner]>>,
provenance_groups: InternTable<WinnerProvenanceGroupID, Box<[WinnerProvenance]>>,
priorities: InternTable<CascadePriorityID, CascadePriority>,
@@ -1507,6 +1525,7 @@ impl Default for WinnerGroups {
state_pending_reference_counts: Vec::new(),
state_winning_rules: Vec::new(),
state_reads: Vec::new(),
+ state_summaries: Vec::new(),
groups: InternTable::default(),
provenance_groups: InternTable::default(),
priorities: InternTable::default(),
@@ -1558,6 +1577,11 @@ impl WinnerGroups {
.iter()
.map(|reads| crate::fork::ForkCopied::new(AtomicU8::new(reads.load(Ordering::Relaxed))))
.collect(),
+ state_summaries: self
+ .state_summaries
+ .iter()
+ .map(|summary| crate::fork::ForkCopied::new(AtomicU64::new(summary.load(Ordering::Relaxed))))
+ .collect(),
groups: self.groups.clone(),
provenance_groups: self.provenance_groups.clone(),
priorities: self.priorities.clone(),
@@ -1766,6 +1790,8 @@ impl WinnerGroups {
self.state_pending_reference_counts.push(0);
self.state_winning_rules.push(winning_rules);
self.state_reads.push(crate::fork::ForkCopied::new(AtomicU8::new(0)));
+ self.state_summaries
+ .push(crate::fork::ForkCopied::new(AtomicU64::new(0)));
id
}
@@ -2046,6 +2072,29 @@ impl WinnerGroups {
}
}
+ /// The summary of `state`'s winners, once decided.
+ pub(super) fn state_summary(&self, state: CascadeStateID) -> Option<StateSummary> {
+ let packed = self.state_summaries.get(state.0 as usize)?.load(Ordering::Relaxed);
+ (packed & STATE_SUMMARY_DECIDED != 0).then_some(StateSummary {
+ explicitly_inherited_groups: packed as u32,
+ has_substitutions: packed & STATE_SUMMARY_HAS_SUBSTITUTIONS != 0,
+ })
+ }
+
+ pub(super) fn note_state_summary(&self, state: CascadeStateID, summary: StateSummary) {
+ if let Some(slot) = self.state_summaries.get(state.0 as usize) {
+ let substitutions = if summary.has_substitutions {
+ STATE_SUMMARY_HAS_SUBSTITUTIONS
+ } else {
+ 0
+ };
+ slot.store(
+ u64::from(summary.explicitly_inherited_groups) | substitutions | STATE_SUMMARY_DECIDED,
+ Ordering::Relaxed,
+ );
+ }
+ }
+
pub(super) fn winner_count_in_state(&self, state: CascadeStateID) -> usize {
self.states[state]
.iter()
@@ -2586,6 +2635,7 @@ impl WinnerGroups {
self.state_pending_reference_counts = Vec::new();
self.state_winning_rules = Vec::new();
self.state_reads = Vec::new();
+ self.state_summaries = Vec::new();
self.groups = InternTable::default();
self.provenance_groups = InternTable::default();
self.priorities = InternTable::default();
@@ -2618,6 +2668,7 @@ impl WinnerGroups {
self.state_pending_reference_counts,
self.state_winning_rules,
self.state_reads,
+ self.state_summaries,
self.winner_rule_references,
];
cached [self.nested_residency.bytes()];
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 57e1bfba557..53a780b0a50 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -1450,9 +1450,10 @@ impl RetainedState {
};
let pseudo_styles = self.pseudo_style_mask_or_rematch(node);
// A record whose winners read beyond its environment is the element's alone.
+ let element_alone = self.state_reads_beyond_environment(node, state);
let cache_key = parent
.zip(parent_record)
- .filter(|_| !self.state_reads_beyond_environment(node, state))
+ .filter(|_| !element_alone)
.and_then(|(parent, parent_record)| self.cold_record_parent(node, parent, parent_record, state))
.map(|parent| ColdRecordKey {
monospace_recascaded_font_size: self
@@ -1510,7 +1511,6 @@ impl RetainedState {
// it: a record C++ computed for a per-element value, such as a `random()` draw, is that
// element's alone. A store with substituted values is the environment's as well as the
// state's, and admits nothing for the state alone.
- let element_alone = self.state_reads_beyond_environment(node, state);
let mut store = match scratch.stores.get(&(state, environment)).filter(|_| !element_alone) {
Some(store) => store.clone(),
None => {
@@ -1548,25 +1548,11 @@ impl RetainedState {
};
self.note_node_substitution(node, scratch, state, environment);
// A record whose winners read beyond its environment is the element's alone, as is one
- // declaring registered custom properties, which its own font decides.
- let cache_key = parent
- .zip(parent_record)
- .filter(|_| !element_alone && !has_registered_declarations)
- .and_then(|(parent, parent_record)| self.cold_record_parent(node, parent, parent_record, state))
- .map(|parent| ColdRecordKey {
- monospace_recascaded_font_size: self
- .monospace_cohort_key(computed::ComputedStyleTarget::new(node, u8::MAX), state),
- sibling_position: self.sibling_position_key(node, state),
- parent,
- previous_style_record: 0,
- generation: cascade_state.0,
- state,
- facts: cold_record_facts(facts),
- pseudo_styles,
- environment,
- font_environment_generation: inputs.font_environment_generation,
- root_font_inputs: RootFontInputs::from_document(&inputs),
- });
+ // declaring registered custom properties, which its own font decides. The key is the one
+ // above, with the environment the element's declarations resolved.
+ let cache_key = cache_key
+ .filter(|_| !has_registered_declarations)
+ .map(|key| ColdRecordKey { environment, ..key });
if let Some(delta) = self.assign_cached_cold_record(
node,
target,
@@ -2838,58 +2824,83 @@ impl RetainedState {
}
fn state_explicitly_inherits_non_inherited_property(&self, node: StyleNodeID, state: CascadeStateID) -> bool {
- self.state_explicitly_inherited_group_masks(node, state)
- .next()
- .is_some()
+ self.state_summary(node, state).explicitly_inherited_groups != 0
}
/// The non-inherited style groups a winner state reads straight from the parent: what C++
/// marks the parent with when it computes a record from the state.
fn state_explicitly_inherited_groups(&self, node: StyleNodeID, state: CascadeStateID) -> u32 {
- self.state_explicitly_inherited_group_masks(node, state)
- .fold(0, |groups, mask| groups | mask)
+ self.state_summary(node, state).explicitly_inherited_groups
}
- /// The style groups of each non-inherited property a winner state declares `inherit` for, and
- /// every group for a value the engine cannot see the spelling of.
- fn state_explicitly_inherited_group_masks(
- &self,
- node: StyleNodeID,
- state: CascadeStateID,
- ) -> impl Iterator<Item = u32> + '_ {
- let is_inherit_keyword = |value: Option<&crate::css::style_value::RetainedStyleValueData>| {
- value.is_none_or(|value| {
- matches!(value.data(), crate::css::style_value::StyleValueData::Keyword { keyword }
- if *keyword == crate::css::style_compute::keyword::INHERIT)
- })
- };
- self.winner_groups.winners_in_state(state).filter_map(move |winner| {
- if crate::css::property_metadata::property_is_inherited(winner.property) {
- return None;
+ /// What the engine asks of a state's winners' written values, decided once for each state where `node` finds
+ /// every winner's written value: the style groups of each non-inherited property the state declares `inherit` for,
+ /// and every group for a value the engine cannot see the spelling of, and whether any longhand winner was written
+ /// with a substitution.
+ fn state_summary(&self, node: StyleNodeID, state: CascadeStateID) -> cascade::StateSummary {
+ use crate::css::style_value::StyleValueData;
+ if let Some(summary) = self.winner_groups.state_summary(state) {
+ return summary;
+ }
+ let mut summary = cascade::StateSummary::default();
+ // NB: A winner whose written value `node` does not find, such as an element declaration hidden for a demand,
+ // says nothing of other nodes, so the summary is only kept when every value was found.
+ let mut every_value_found = true;
+ for winner in self.winner_groups.winners_in_state(state) {
+ let inherited = crate::css::property_metadata::property_is_inherited(winner.property);
+ let Some(winner) = self.winner_groups.resolved_winner(winner) else {
+ continue;
+ };
+ let longhand = winner.property >= crate::css::property_metadata::FIRST_LONGHAND_PROPERTY_ID;
+ if inherited && !longhand {
+ continue;
}
- let winner = self.winner_groups.resolved_winner(winner)?;
let group_mask = || match crate::css::computed_values::StyleGroupMasks::get().dependencies(winner.property)
{
0 => u32::MAX,
mask => mask,
};
- match winner.source {
- WinnerSource::Rule(rule) => is_inherit_keyword(self.program.written_winner_value(
- rule,
- winner.property,
- winner.important,
- winner.key.value,
- ))
- .then(group_mask),
- WinnerSource::Element(kind) => {
- is_inherit_keyword(self.facts.element_declarations(node, kind).and_then(|declarations| {
- declarations.winner_value(winner.property, winner.important, winner.key.value)
- }))
- .then(group_mask)
+ let value = match winner.source {
+ WinnerSource::Rule(rule) => {
+ self.program
+ .written_winner_value(rule, winner.property, winner.important, winner.key.value)
+ }
+ WinnerSource::Element(kind) => self.facts.element_declarations(node, kind).and_then(|declarations| {
+ declarations.winner_value(winner.property, winner.important, winner.key.value)
+ }),
+ WinnerSource::ExactCascade => {
+ if !inherited {
+ summary.explicitly_inherited_groups = u32::MAX;
+ }
+ continue;
+ }
+ };
+ let Some(value) = value else {
+ every_value_found = false;
+ if !inherited {
+ summary.explicitly_inherited_groups |= group_mask();
}
- WinnerSource::ExactCascade => Some(u32::MAX),
+ continue;
+ };
+ if !inherited
+ && matches!(value.data(), StyleValueData::Keyword { keyword }
+ if *keyword == crate::css::style_compute::keyword::INHERIT)
+ {
+ summary.explicitly_inherited_groups |= group_mask();
}
- })
+ if longhand
+ && matches!(
+ value.data(),
+ StyleValueData::Unresolved { .. } | StyleValueData::PendingSubstitution { .. }
+ )
+ {
+ summary.has_substitutions = true;
+ }
+ }
+ if every_value_found {
+ self.winner_groups.note_state_summary(state, summary);
+ }
+ summary
}
/// Keep a record C++ published for an element as a first record a later alike element can
@@ -3143,7 +3154,7 @@ impl RetainedState {
/// Whether any winner of a state was written with a substitution, so the record computed
/// from it reads the node's custom-property environment.
pub(super) fn state_has_substitutions(&self, node: StyleNodeID, state: CascadeStateID) -> bool {
- self.state_substitution_values(node, state).next().is_some()
+ self.state_summary(node, state).has_substitutions
}
/// What a state's winners read beyond the cascade, as `cascade::STATE_READS_*` bits, decided
@@ -3167,12 +3178,22 @@ impl RetainedState {
reads |= cascade::STATE_READS_CUSTOM_FUNCTION;
}
}
- for (_, value, _) in self
+ // NB: As with the state's summary, what a node that does not find a winner's written value reads says nothing
+ // of other nodes.
+ let mut every_value_found = true;
+ for winner in self
.winner_groups
.winners_in_state(state)
.filter_map(|winner| self.winner_groups.resolved_winner(winner))
- .filter_map(|winner| self.written_winner_value(node, &winner).ok().flatten())
{
+ let (_, value, _) = match self.written_winner_value(node, &winner) {
+ Ok(Some(written)) => written,
+ Ok(None) => {
+ every_value_found = false;
+ continue;
+ }
+ Err(_) => continue,
+ };
let dependencies = crate::css::style_compute::collect_external_value_dependencies(value.data());
if dependencies.uses_tree_counting_function {
reads |= cascade::STATE_READS_SIBLING_POSITION;
@@ -3184,7 +3205,9 @@ impl RetainedState {
reads |= cascade::STATE_READS_CONTAINER_UNITS;
}
}
- self.winner_groups.note_state_reads(state, reads);
+ if every_value_found {
+ self.winner_groups.note_state_reads(state, reads);
+ }
reads
}