Skip to content

Truly independent web browser

LibWeb: Keep what comparing two custom property stores answered

Andreas Klinge8e334b68c9c
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index e0c2398f251..3c2f509d1a3 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -15,7 +15,7 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::c_void;
use std::rc::Rc;
-use std::sync::Arc;
+use std::sync::{Arc, Weak};
use crate::css::thin_slice::ThinSlice;
use smallvec::SmallVec;
@@ -233,6 +233,23 @@ impl FromIterator<(usize, CustomPropertyEntry)> for CustomPropertyEntries {
}
}
+/// How many answers of [`CustomPropertyStore::hold_the_same_values`] a thread keeps.
+const COMPARED_STORE_SLOTS: usize = 64;
+
+/// What comparing two stores answered, which stands for as long as it is kept: a store is immutable,
+/// and the weak references keep another store from taking the place of either without keeping
+/// anything it holds.
+struct ComparedStores {
+ first: Weak<CustomPropertyStore>,
+ second: Weak<CustomPropertyStore>,
+ same: bool,
+}
+
+thread_local! {
+ static COMPARED_STORES: RefCell<[Option<ComparedStores>; COMPARED_STORE_SLOTS]> =
+ const { RefCell::new([const { None }; COMPARED_STORE_SLOTS]) };
+}
+
pub struct CustomPropertyStore {
/// The values the store holds itself: those it declares, each knowing where among its
/// declarations, and those it took in from the chain above it.
@@ -933,14 +950,7 @@ impl CustomPropertyStore {
/// element inherits read what the pass resolved.
pub(crate) fn animation_overlay_over(self: &Arc<Self>, base: Option<&Arc<Self>>) -> Arc<Self> {
let sampled_over = self.animation_overlay_base.as_ref();
- let moved = match (sampled_over, base) {
- (None, None) => false,
- (Some(sampled_over), Some(base)) => {
- !Arc::ptr_eq(sampled_over, base) && !sampled_over.holds_same_values_as(base)
- }
- _ => true,
- };
- if !moved {
+ if Self::absent_or_holding_the_same_values(sampled_over, base) {
return self.clone();
}
let animated: SmallVec<[(usize, CustomPropertyEntry); 8]> = self
@@ -1064,14 +1074,60 @@ impl CustomPropertyStore {
declared(self).eq(declared(other))
}
- /// Whether the store holds the same values as `other`, built over the same store and chained to
- /// the same parent.
- pub(crate) fn holds_same_values_as(&self, other: &Self) -> bool {
- let same_inheritance = match (&self.inheritance_parent, &other.inheritance_parent) {
+ /// Whether `first` and `second` hold the same values, as [`Self::holds_same_values_as`] tells.
+ /// Every element below a store asks this of the same two stores, each answer taking a walk of
+ /// their values, so a thread keeps the answers it found.
+ pub(crate) fn hold_the_same_values(first: &Arc<Self>, second: &Arc<Self>) -> bool {
+ if Arc::ptr_eq(first, second) {
+ return true;
+ }
+ // NB: The answer is the same whichever of the two is asked.
+ let (first, second) = if Arc::as_ptr(first) < Arc::as_ptr(second) {
+ (first, second)
+ } else {
+ (second, first)
+ };
+ let identities = (Arc::as_ptr(first) as u64) ^ (Arc::as_ptr(second) as u64).rotate_left(32);
+ let slot =
+ (identities.wrapping_mul(0x9e37_79b9_7f4a_7c15) >> (u64::BITS - COMPARED_STORE_SLOTS.ilog2())) as usize;
+ let known = COMPARED_STORES.with_borrow(|compared| {
+ compared[slot]
+ .as_ref()
+ .filter(|compared| {
+ compared.first.as_ptr() == Arc::as_ptr(first) && compared.second.as_ptr() == Arc::as_ptr(second)
+ })
+ .map(|compared| compared.same)
+ });
+ if let Some(same) = known {
+ return same;
+ }
+ let same = first.holds_same_values_as(second);
+ COMPARED_STORES.with_borrow_mut(|compared| {
+ compared[slot] = Some(ComparedStores {
+ first: Arc::downgrade(first),
+ second: Arc::downgrade(second),
+ same,
+ });
+ });
+ same
+ }
+
+ /// Whether `first` and `second` are both absent, or hold the same values.
+ pub(crate) fn absent_or_holding_the_same_values(first: Option<&Arc<Self>>, second: Option<&Arc<Self>>) -> bool {
+ match (first, second) {
(None, None) => true,
- (Some(first), Some(second)) => Arc::ptr_eq(first, second) || first.holds_same_values_as(second),
+ (Some(first), Some(second)) => Self::hold_the_same_values(first, second),
_ => false,
- };
+ }
+ }
+
+ /// Whether the store holds the same values as `other`, built over the same store and chained to
+ /// the same parent.
+ fn holds_same_values_as(&self, other: &Self) -> bool {
+ let same_inheritance = Self::absent_or_holding_the_same_values(
+ self.inheritance_parent.as_ref(),
+ other.inheritance_parent.as_ref(),
+ );
if !same_inheritance || !self.declares_the_same_names_as(other) {
return false;
}
@@ -3900,27 +3956,29 @@ mod tests {
}
}
+ fn store(
+ parent: Option<Arc<CustomPropertyStore>>,
+ names: impl IntoIterator<Item = u32>,
+ value: f64,
+ ) -> Arc<CustomPropertyStore> {
+ let entries = names
+ .into_iter()
+ .map(|index| {
+ let name = Utf16FlyString::from_utf16(&utf16(&format!("--name-{index}")));
+ let entry = CustomPropertyEntry {
+ name: name.clone(),
+ value: RetainedStyleValueData::from_owned(StyleValueData::Number { value }),
+ important: false,
+ declared: None,
+ };
+ (name.raw_identity(), entry)
+ })
+ .collect();
+ CustomPropertyStore::child(parent, entries)
+ }
+
#[test]
fn stores_built_apart_hold_the_same_values_where_every_name_reads_the_same() {
- fn store(
- parent: Option<Arc<CustomPropertyStore>>,
- names: std::ops::Range<u32>,
- value: f64,
- ) -> Arc<CustomPropertyStore> {
- let entries = names
- .map(|index| {
- let name = Utf16FlyString::from_utf16(&utf16(&format!("--name-{index}")));
- let entry = CustomPropertyEntry {
- name: name.clone(),
- value: RetainedStyleValueData::from_owned(StyleValueData::Number { value }),
- important: false,
- declared: None,
- };
- (name.raw_identity(), entry)
- })
- .collect();
- CustomPropertyStore::child(parent, entries)
- }
// NB: A parent of more names than a child takes in stays a store of the child's chain.
let child_over = |value| store(Some(store(None, 0..20, value)), 20..22, 2.0);
let (first, second) = (child_over(1.0), child_over(1.0));
@@ -3933,6 +3991,51 @@ mod tests {
assert!(!wider.holds_same_values_as(&first));
}
+ #[test]
+ fn stores_declaring_the_same_names_in_another_order_do_not_hold_the_same_values() {
+ let (first, second) = (store(None, [0, 1], 1.0), store(None, [1, 0], 1.0));
+ assert!(!first.holds_same_values_as(&second));
+ assert!(first.holds_same_values_as(&store(None, [0, 1], 1.0)));
+ }
+
+ #[test]
+ fn stores_compared_again_answer_as_they_did_whichever_is_asked() {
+ // NB: Turning every answer kept around shows that the next one is the one kept, and not a walk of the values.
+ fn turn_kept_answers() {
+ COMPARED_STORES.with_borrow_mut(|compared| {
+ for compared in compared.iter_mut().flatten() {
+ compared.same = !compared.same;
+ }
+ });
+ }
+ let (first, second, other) = (
+ store(None, 0..20, 1.0),
+ store(None, 0..20, 1.0),
+ store(None, 0..20, 2.0),
+ );
+ assert!(CustomPropertyStore::hold_the_same_values(&first, &first));
+
+ // NB: Two pairs can share a slot, so each pair is asked about again before the next is compared.
+ assert!(CustomPropertyStore::hold_the_same_values(&first, &second));
+ turn_kept_answers();
+ assert!(!CustomPropertyStore::hold_the_same_values(&second, &first));
+ assert!(first.holds_same_values_as(&second));
+
+ assert!(!CustomPropertyStore::hold_the_same_values(&first, &other));
+ turn_kept_answers();
+ assert!(CustomPropertyStore::hold_the_same_values(&other, &first));
+ assert!(!first.holds_same_values_as(&other));
+ }
+
+ #[test]
+ fn a_kept_answer_keeps_no_store_alive() {
+ let (first, second) = (store(None, 0..1, 1.0), store(None, 0..1, 1.0));
+ assert!(CustomPropertyStore::hold_the_same_values(&first, &second));
+ let weak = Arc::downgrade(&first);
+ drop(first);
+ assert!(weak.upgrade().is_none());
+ }
+
#[test]
fn custom_property_values_outlive_their_store() {
let name = Utf16FlyString::default();
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
index 408c6007dd0..a09b42b4697 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -779,11 +779,6 @@ impl RetainedState {
noted.filter(|noted| noted.raw() != 0)
}
- /// The environment the element's declarations resolved to last, beneath any animation overlay, where they resolve to
- /// its values again in `resolved`: an environment resolved anew, as one holding a registered value is, would move
- /// what the element's descendants inherit without moving any value they see. What an environment hands down was
- /// decided under the registrations it was resolved under, so only one handing down every value, then and now, is
- /// kept.
/// The store of the environment the element's declarations resolved to last, where `resolved` holds the same values
/// over the same stores, as [`Self::environment_resolving_to_the_same_values`] keeps it: a store computed again
/// gives way to it, so that the element's record holds the store its environment is named by.
@@ -798,11 +793,16 @@ impl RetainedState {
self.custom_property_environments.store(previous).cloned()
}
+ /// The environment the element's declarations resolved to last, beneath any animation overlay, where they resolve to
+ /// its values again in `resolved`: an environment resolved anew, as one holding a registered value is, would move
+ /// what the element's descendants inherit without moving any value they see. What an environment hands down was
+ /// decided under the registrations it was resolved under, so only one handing down every value, then and now, is
+ /// kept.
pub(super) fn environment_resolving_to_the_same_values(
&self,
node: StyleNodeID,
pseudo: Option<u8>,
- resolved: &CustomPropertyStore,
+ resolved: &Arc<CustomPropertyStore>,
registry: &crate::css::custom_properties::CustomPropertyRegistry,
) -> Option<u64> {
if pseudo.is_some() {
@@ -819,7 +819,9 @@ impl RetainedState {
return None;
}
let previous_store = self.custom_property_environments.store(previous)?;
- (resolved.hands_down_every_value(registry) && resolved.holds_same_values_as(previous_store)).then_some(previous)
+ (resolved.hands_down_every_value(registry)
+ && CustomPropertyStore::hold_the_same_values(resolved, previous_store))
+ .then_some(previous)
}
/// The custom properties in a dependency cycle through what an element's animations declare, as
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
index f7674bbd01b..5dd303c45ef 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
@@ -908,13 +908,7 @@ impl Record {
/// Whether `other` holds what this record does, its longhand table aside.
fn holds_the_same_besides_its_table_as(&self, other: &Record) -> bool {
- let same_store =
- |a: Option<&Arc<crate::css::custom_properties::CustomPropertyStore>>,
- b: Option<&Arc<crate::css::custom_properties::CustomPropertyStore>>| match (a, b) {
- (None, None) => true,
- (Some(a), Some(b)) => Arc::ptr_eq(a, b) || a.holds_same_values_as(b),
- _ => false,
- };
+ let same_store = crate::css::custom_properties::CustomPropertyStore::absent_or_holding_the_same_values;
self.payloads.current_color() == other.payloads.current_color()
&& self.rare().custom_property_reads == other.rare().custom_property_reads
&& self.rare().reads == other.rare().reads
@@ -1871,13 +1865,7 @@ impl<'a> Records<'a> {
{
return true;
}
- let same_store =
- |a: Option<&Arc<crate::css::custom_properties::CustomPropertyStore>>,
- b: Option<&Arc<crate::css::custom_properties::CustomPropertyStore>>| match (a, b) {
- (None, None) => true,
- (Some(a), Some(b)) => Arc::ptr_eq(a, b) || a.holds_same_values_as(b),
- _ => false,
- };
+ let same_store = crate::css::custom_properties::CustomPropertyStore::absent_or_holding_the_same_values;
sets.held_record(node).is_some_and(|held| {
same_store(held.custom_properties(), record.custom_properties())
&& same_store(
diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.py
index 0086552b2c1..50e3f70fce5 100755
--- a/Meta/Linters/check_rust_process_wide_state.py
+++ b/Meta/Linters/check_rust_process_wide_state.py
@@ -174,6 +174,7 @@ RENDER_STATE_ALLOWED = {
"css/style_compute/ffi_test_stubs.rs:NEXT",
],
),
+ f"{RENDER_STATE_CRATE}/src/css/custom_properties.rs:COMPARED_STORES": "per-thread memo of comparing immutable stores; a thread without one only walks their values again",
f"{RENDER_STATE_CRATE}/src/css/parser/stylesheet_cache.rs:PARSE_DEPENDENCIES": "per-thread record of the parse running on this thread; scoped to that parse",
f"{RENDER_STATE_CRATE}/src/css/style_value.rs:VALUES": "built-once keyword values, and a replay-only table of the same name",
f"{RENDER_STATE_CRATE}/src/css/parser/value_parser.rs:LISTS": "built-once immutable lists of initial background values",