Truly independent web browser
LibWeb: Take a custom-property environment by the answer it came from
Andreas Kling0bb125dc93b1
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rsindex a7ec00b7b28..f93b0dff78a 100644--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs@@ -387,6 +387,23 @@ fn substituted_tokens(value: &StyleValueData) -> &StyleValueData {/// Custom property names, sorted and without duplicates.pub(crate) type CustomPropertyNames = Arc<[Arc<[u16]>]>;+/// What an element's custom declarations resolve over, where its match answer decides them all.+#[derive(Clone, Copy, PartialEq, Eq, Hash)]+pub(super) struct EnvironmentByAnswerKey {+ answer: MatchAnswerID,+ tree_scope: TreeScopeID,+ parent_environment: u64,+ registration_generation: u64,+}++/// The environment such declarations resolved to, and what they read beyond it.+#[derive(Clone)]+pub(super) struct EnvironmentByAnswer {+ environment: u64,+ reads: u8,+ custom_property_reads: Option<Option<CustomPropertyNames>>,+}+pub(super) fn append_custom_property_reads(value: &StyleValueData, names: &mut Vec<Arc<[u16]>>) -> bool {let Some((references, all_references_visible)) = custom_property_references(substituted_tokens(value)) else {return true;@@ -1381,6 +1398,92 @@ impl RetainedState {self.engine_custom_property_environment_of(node, None, parent_environment, inputs, registered)}+ /// `engine_custom_property_environment` for an element whose cascade declares no registered custom property: the+ /// environment an element alike in its match answer, its tree and the environment it inherits resolved to.+ pub(super) fn engine_custom_property_environment_by_answer(+ &mut self,+ node: StyleNodeID,+ parent_environment: u64,+ inputs: &bridge::FfiDocumentStyleComputationInputs,+ ) -> Drive<u64> {+ let key = self.environment_by_answer_key(node, parent_environment, inputs);+ if let Some(key) = key+ && let Some(memoized) = self.custom_property_environments_by_answer.get(&key).cloned()+ {+ self.counters.bump(Counter::EngineCustomPropertyEnvironmentAnswerHits);+ match memoized.reads {+ 0 => self.custom_declaration_reads.remove(&node),+ reads => self.custom_declaration_reads.insert(node, reads),+ };+ match memoized.custom_property_reads {+ None => self.custom_declaration_custom_property_reads.remove(&node),+ Some(reads) => self.custom_declaration_custom_property_reads.insert(node, reads),+ };+ return Ok(memoized.environment);+ }+ self.last_custom_property_environment_memoizable = false;+ let environment = self.engine_custom_property_environment(node, parent_environment, inputs, None)?;+ if let Some(key) = key+ && self.last_custom_property_environment_memoizable+ {+ let memoized = EnvironmentByAnswer {+ environment,+ reads: self.custom_declaration_reads.get(&node).copied().unwrap_or(0),+ custom_property_reads: self.custom_declaration_custom_property_reads.get(&node).cloned(),+ };+ self.custom_property_environments_by_answer.insert(key, memoized);+ }+ Ok(environment)+ }++ /// What decides an element's custom declarations, where its published match answer does: no batch of matches+ /// stands in for the answer, the element's inline style declares no custom property, and no container condition+ /// gates a rule of the answer declaring one.+ fn environment_by_answer_key(+ &self,+ node: StyleNodeID,+ parent_environment: u64,+ inputs: &bridge::FfiDocumentStyleComputationInputs,+ ) -> Option<EnvironmentByAnswerKey> {+ if self.facts.element_custom_declarations_written(node).is_some() {+ return None;+ }+ let gated = |rule: RuleID| {+ self.program.rule_is_gated_by_container_query(rule) && !self.program.custom_declarations_of(rule).is_empty()+ };+ let answer_id = if let Some(matches) = self.batch_custom_property_matches.get(&node) {+ if matches.iter().any(|entry| entry.pseudo.is_none() && gated(entry.rule)) {+ return None;+ }+ *self.batch_custom_property_answers.get(&node)?+ } else {+ let (published, answer) = Self::published_answer_lookup(+ &self.published_match_answers,+ self.batch_matching_traversal.as_deref(),+ node,+ )?;+ let matches = published.matches_for(answer)?;+ if matches+ .iter()+ .any(|entry| entry.pseudo_element.is_none() && gated(entry.rule))+ {+ return None;+ }+ answer.cascade_input?+ };+ Some(EnvironmentByAnswerKey {+ answer: answer_id,+ tree_scope: self.tree.tree_scope(node),+ parent_environment,+ registration_generation: inputs.custom_property_registration_generation,+ })+ }++ /// Forget the environments taken by match answer, whose identities may be handed out again.+ pub(super) fn forget_custom_property_environments_by_answer(&mut self) {+ self.custom_property_environments_by_answer.clear();+ }+/// What `engine_custom_property_environment` says of the element, for one of its/// pseudo-elements over the element's own environment.pub(super) fn engine_custom_property_environment_of(@@ -1396,6 +1499,7 @@ impl RetainedState {self.custom_declaration_reads.remove(&node);self.custom_declaration_custom_property_reads.remove(&node);}+ self.last_custom_property_environment_memoizable = true;return Ok(self.custom_property_environments.inheritable(parent_environment));}// A node the engine drives has the match answer its winners came from, and a published@@ -1470,6 +1574,7 @@ impl RetainedState {) -> Drive<u64> {let inherited_environment = self.custom_property_environments.inheritable(parent_environment);if cascaded.is_empty() {+ self.last_custom_property_environment_memoizable = true;return Ok(inherited_environment);}let registry_ref = inputs.custom_property_registry();@@ -1511,6 +1616,7 @@ impl RetainedState {let reads_conditions =functions.is_some() || cascaded.iter().any(|(_, value)| value_reads_conditions(value.data()));let memoizes = !reads_attributes && !reads_conditions && registered.is_none();+ self.last_custom_property_environment_memoizable = memoizes;let key = Self::environment_inputs(parent_environment,inputs.custom_property_registration_generation,diff --git a/Libraries/LibWeb/Rust/src/css/style/flush.rs b/Libraries/LibWeb/Rust/src/css/style/flush.rsindex beb7f17ed74..d3e6e27f767 100644--- a/Libraries/LibWeb/Rust/src/css/style/flush.rs+++ b/Libraries/LibWeb/Rust/src/css/style/flush.rs@@ -293,6 +293,10 @@ impl RetainedState {&& let Some(matches) = self.batch_custom_property_matches_of(answers, answer){self.batch_custom_property_matches.insert(node, matches);+ match answer.cascade_input {+ Some(identity) => self.batch_custom_property_answers.insert(node, identity),+ None => self.batch_custom_property_answers.remove(&node),+ };}if let Some(matches) = self.batch_backing_pseudo_matches_of(node, answers, answer) {self.batch_backing_pseudo_matches.insert(node, matches);@@ -307,6 +311,7 @@ impl RetainedState {fn clear_batch_answer_facts(&mut self) {self.batch_answers_complete_but_for_custom_properties.clear();self.batch_custom_property_matches.clear();+ self.batch_custom_property_answers.clear();self.batch_backing_pseudo_matches.clear();}@@ -422,6 +427,7 @@ impl StyleEngine {// them before this transaction publishes anything: a later C++ computation can install// the same record, which must not then be mistaken for an unconsumed derivation.self.discard_engine_computed_records();+ self.retained.forget_custom_property_environments_by_answer();self.reclaim_computed_memory_if_needed();self.sync_tier3_benefit_observations();let tier3_evictions = self.retained.memory.finish_tier3_quota_period();diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rsindex f253650e30d..a81e4c014c1 100644--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs@@ -2019,6 +2019,8 @@ impl StyleEngine {custom_property_environments: Default::default(),nodes_with_substituted_records: HashSet::default(),custom_declaration_reads: HashMap::default(),+ custom_property_environments_by_answer: HashMap::default(),+ last_custom_property_environment_memoizable: false,custom_declaration_custom_property_reads: HashMap::default(),nodes_with_tree_counting_records: HashMap::default(),nodes_with_rolled_back_records: HashMap::default(),@@ -2054,6 +2056,7 @@ impl StyleEngine {engine_pseudo_record_cache: HashMap::default(),batch_answers_complete_but_for_custom_properties: HashMap::default(),batch_custom_property_matches: HashMap::default(),+ batch_custom_property_answers: HashMap::default(),batch_backing_pseudo_matches: HashMap::default(),engine_cold_record_cache: Default::default(),engine_cold_record_donors: Default::default(),@@ -3423,6 +3426,9 @@ impl RetainedState {custom_property_environments: _,nodes_with_substituted_records,custom_declaration_reads,+ // Keyed by match answer, and cleared with the answers whose identities it names.+ custom_property_environments_by_answer: _,+ last_custom_property_environment_memoizable: _,custom_declaration_custom_property_reads,nodes_with_tree_counting_records,nodes_with_rolled_back_records,@@ -3463,6 +3469,7 @@ impl RetainedState {// Filled and cleared within one transaction's record loop.batch_answers_complete_but_for_custom_properties: _,batch_custom_property_matches: _,+ batch_custom_property_answers: _,batch_backing_pseudo_matches: _,engine_cold_record_cache: _,engine_cold_record_donors: _,diff --git a/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs b/Libraries/LibWeb/Rust/src/css/style/instrumentation.rsindex 8935af31f39..da6736173a6 100644--- a/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs+++ b/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs@@ -163,6 +163,7 @@ define_counters! {EngineComputedRecordSubstitutionMemoHits => "engineComputedRecordSubstitutionMemoHits",EngineCustomPropertyEnvironmentsResolved => "engineCustomPropertyEnvironmentsResolved",EngineCustomPropertyEnvironmentMemoHits => "engineCustomPropertyEnvironmentMemoHits",+ EngineCustomPropertyEnvironmentAnswerHits => "engineCustomPropertyEnvironmentAnswerHits",EngineCustomPropertyEnvironmentBails => "engineCustomPropertyEnvironmentBails",EngineComputedRecordBailMonospaceQuirk => "engineComputedRecordBailMonospaceQuirk",EngineComputedRecordBailRecordParent => "engineComputedRecordBailRecordParent",diff --git a/Libraries/LibWeb/Rust/src/css/style/matching.rs b/Libraries/LibWeb/Rust/src/css/style/matching.rsindex c9ca7cb3255..ad858a7af1e 100644--- a/Libraries/LibWeb/Rust/src/css/style/matching.rs+++ b/Libraries/LibWeb/Rust/src/css/style/matching.rs@@ -488,6 +488,7 @@ impl RetainedState {.release(MemoryCategory::BatchScratch, traversal.dispatch_workspace_bytes);traversal.cascade_compaction_workspace_memory.release();traversal.unbatched_facts_memory.release();+ self.forget_custom_property_environments_by_answer();let released_cascade_payload_bytes = self.match_answers.sweep_unreferenced();self.retained_match_answers.release_swept_cascade_payloads(released_cascade_payload_bytes);@@ -741,6 +742,7 @@ impl RetainedState {caches.answers.release(&mut self.match_answers);}drop(caches);+ self.forget_custom_property_environments_by_answer();let released_cascade_payload_bytes = self.match_answers.sweep_unreferenced();self.retained_match_answers.release_swept_cascade_payloads(released_cascade_payload_bytes);@@ -4208,8 +4210,11 @@ impl RetainedState {if let Some(batch) = self.batch_answers_complete_but_for_custom_properties.get_mut(&node) {*batch = complete_but_for_custom_properties;}+ // The answer the batch named for these matches is not the one republished, so the matches no longer key an+ // environment by it.if let Some(matches) = custom_property_matches {self.batch_custom_property_matches.insert(node, matches);+ self.batch_custom_property_answers.remove(&node);}self.computed_group_sets.set_node_pseudo_style_mask(node, Some(pseudo_style_mask));diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rsindex 82253e6008a..88c31866542 100644--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs@@ -853,6 +853,13 @@ pub struct RetainedState {/// declare or read through `var()`. `None` when they cannot be listed.custom_declaration_custom_property_reads:HashMap<StyleNodeID, Option<custom_property_cascade::CustomPropertyNames>>,+ /// The custom-property environments elements resolved to over the environments they inherit, and what their+ /// declarations read, by the match answer the declarations cascaded from: an element alike in its answer, its+ /// tree and what it inherits resolves alike. Cleared wherever a match answer's identity may be handed out again.+ custom_property_environments_by_answer:+ HashMap<custom_property_cascade::EnvironmentByAnswerKey, custom_property_cascade::EnvironmentByAnswer>,+ /// Whether the environment resolved last stands for every element alike in its inputs.+ last_custom_property_environment_memoizable: bool,/// Which of a node's records read its place among its siblings, written or produced by a/// substitution: a bit for its own record and one per synthetic pseudo-element kind, which/// each record the engine derives, or admits from C++, sets or clears as it is installed. A@@ -966,6 +973,8 @@ pub struct RetainedState {/// Beside them, the matches in each answer that declare custom properties, read the same way:/// what the node's custom-property cascade runs over while its answer is not installed.batch_custom_property_matches: HashMap<StyleNodeID, Vec<BatchCustomPropertyMatch>>,+ /// The identity of the answer each node's `batch_custom_property_matches` were read from, where it has one.+ batch_custom_property_answers: HashMap<StyleNodeID, MatchAnswerID>,/// Beside them, each published host's matches for the element-backed pseudo-elements, which/// the elements backing them cascade from while the host's answer is not installed.batch_backing_pseudo_matches: HashMap<StyleNodeID, Vec<RuleMatch>>,diff --git a/Libraries/LibWeb/Rust/src/css/style/program_updates.rs b/Libraries/LibWeb/Rust/src/css/style/program_updates.rsindex cca60008795..8d60292e5d9 100644--- a/Libraries/LibWeb/Rust/src/css/style/program_updates.rs+++ b/Libraries/LibWeb/Rust/src/css/style/program_updates.rs@@ -1280,6 +1280,7 @@ impl StyleEngine {/// Answers the identities the end of the transaction released, for the host to mint again.pub(super) fn discard_style_transaction_outputs(&mut self) -> Vec<u32> {+ self.retained.forget_custom_property_environments_by_answer();if let Some(pass) = self.host.suspended_style_pass.take() {self.abandon_style_pass(pass);}diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rsindex c8c90a3811d..0dfb17ccd24 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs@@ -664,7 +664,7 @@ impl RetainedState {};// A function's container condition the engine cannot decide yet waits, as the row does.let environment = self- .engine_custom_property_environment(node, parent_environment, &inputs, None)+ .engine_custom_property_environment_by_answer(node, parent_environment, &inputs).inspect_err(|&unanswered| {if unanswered == Unanswered::Refused {self.counters.bump(Counter::EngineComputedRecordBailCustomProperties);@@ -1501,7 +1501,7 @@ impl RetainedState {let has_registered_declarations = self.declares_registered_custom_property(node, None, &inputs);// A function's container condition the engine cannot decide yet waits, as the row does.let mut environment = self- .engine_custom_property_environment(node, parent_environment, &inputs, None)+ .engine_custom_property_environment_by_answer(node, parent_environment, &inputs).inspect_err(|&unanswered| {if unanswered == Unanswered::Refused {self.counters.bump(Counter::EngineComputedRecordBailCustomProperties);@@ -4273,6 +4273,7 @@ impl StyleEngine {self.retained.custom_property_environments.retain_only(|identity| live.contains(&identity));+ self.retained.forget_custom_property_environments_by_answer();true}diff --git a/Tests/LibWeb/Text/expected/css/style-engine/custom-property-environment-resolves-once.txt b/Tests/LibWeb/Text/expected/css/style-engine/custom-property-environment-resolves-once.txtindex 92037181dbe..f48f4f26d09 100644--- a/Tests/LibWeb/Text/expected/css/style-engine/custom-property-environment-resolves-once.txt+++ b/Tests/LibWeb/Text/expected/css/style-engine/custom-property-environment-resolves-once.txt@@ -1,5 +1,6 @@environments resolved: 1-environment memo hits: 49+environment memo hits: 0+environment answer hits: 50padding: 1pxchanged padding: 9pxneighbour padding: 1pxdiff --git a/Tests/LibWeb/Text/input/css/style-engine/custom-property-environment-resolves-once.html b/Tests/LibWeb/Text/input/css/style-engine/custom-property-environment-resolves-once.htmlindex 76b784b280f..8757a9e762f 100644--- a/Tests/LibWeb/Text/input/css/style-engine/custom-property-environment-resolves-once.html+++ b/Tests/LibWeb/Text/input/css/style-engine/custom-property-environment-resolves-once.html@@ -20,6 +20,7 @@const counters = internals.styleEngineCounters();println(`environments resolved: ${(counters.engineCustomPropertyEnvironmentsResolved ?? 0) - (before.engineCustomPropertyEnvironmentsResolved ?? 0)}`);println(`environment memo hits: ${(counters.engineCustomPropertyEnvironmentMemoHits ?? 0) - (before.engineCustomPropertyEnvironmentMemoHits ?? 0)}`);+ println(`environment answer hits: ${(counters.engineCustomPropertyEnvironmentAnswerHits ?? 0) - (before.engineCustomPropertyEnvironmentAnswerHits ?? 0)}`);println(`padding: ${getComputedStyle(host.firstElementChild).paddingTop}`);// An element whose values differ resolves an environment of its own, and its neighbours