Truly independent web browser
LibWeb: Count no references to the style values every thread shares
Andreas Kling9819e1f8c8d3
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rsindex 470c1ed76aa..ea085d9a2cd 100644--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs@@ -3047,8 +3047,16 @@ fn initial_value_table() -> &'static InitialValueTable {TABLE.get_or_init(|| {let values: Box<[_]> = (FIRST_LONGHAND_PROPERTY_ID..=LAST_LONGHAND_PROPERTY_ID).map(|property| {- crate::css::parser::value_parser::parse_initial_value(property)- .map_or_else(RetainedStyleValueData::none, RetainedStyleValueData::from_arc)+ crate::css::parser::value_parser::parse_initial_value(property).map_or_else(+ RetainedStyleValueData::none,+ |value| match crate::css::style_value_arc::StyleValueArc::is_unique(&value) {+ // NB: The table holds the value for the rest of the process.+ true => RetainedStyleValueData::from_arc(+ crate::css::style_value_arc::StyleValueArc::into_immortal(value),+ ),+ false => RetainedStyleValueData::from_arc(value),+ },+ )}).collect();let dependencies = valuesdiff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rsindex 10d33fe6ec7..394f79450ac 100644--- a/Libraries/LibWeb/Rust/src/css/style_value.rs+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs@@ -483,7 +483,7 @@ pub(crate) fn shared_style_value(value: StyleValueData) -> StyleValueArc {_ => None,};if let Some(index) = index {- StyleValueArc::clone(VALUES[index].get_or_init(|| StyleValueArc::new(value)))+ StyleValueArc::clone(VALUES[index].get_or_init(|| StyleValueArc::immortal(value)))} else {StyleValueArc::new(value)}@@ -497,7 +497,7 @@ impl RetainedStyleValueData {"the shared values hold every keyword");let value =- VALUES[usize::from(keyword)].get_or_init(|| StyleValueArc::new(StyleValueData::Keyword { keyword }));+ VALUES[usize::from(keyword)].get_or_init(|| StyleValueArc::immortal(StyleValueData::Keyword { keyword }));// SAFETY: A handle is one pointer at the value, as an arc is, and the shared values keep// theirs for the life of the process.unsafe { &*std::ptr::from_ref(value).cast::<Self>() }diff --git a/Libraries/LibWeb/Rust/src/css/style_value_arc.rs b/Libraries/LibWeb/Rust/src/css/style_value_arc.rsindex af1e9c46014..d7ab93ee2b9 100644--- a/Libraries/LibWeb/Rust/src/css/style_value_arc.rs+++ b/Libraries/LibWeb/Rust/src/css/style_value_arc.rs@@ -37,6 +37,10 @@ unsafe impl Sync for StyleValueArc where StyleValueData: Send + Sync {}/// The most references a value may have, beyond which counting aborts, as std's `Arc` does.const MAXIMUM_REFERENCES: usize = isize::MAX as usize;+/// The count of a value that lives for the rest of the process, which no reference counts: threads+/// sharing it only read the line it is on.+const IMMORTAL_REFERENCES: usize = usize::MAX;+impl StyleValueArc {const DATA_OFFSET: usize = std::mem::offset_of!(StyleValueAllocation, data);@@ -52,6 +56,20 @@ impl StyleValueArc {}}+ /// A value that lives for the rest of the process, for the tables of values every thread shares.+ pub(crate) fn immortal(data: StyleValueData) -> Self {+ let value = Self::new(data);+ Self::references(value.pointer.as_ptr()).store(IMMORTAL_REFERENCES, Ordering::Relaxed);+ value+ }++ /// This value, which must be its only reference, as one that lives for the rest of the process.+ pub(crate) fn into_immortal(this: Self) -> Self {+ assert!(Self::is_unique(&this), "a value becomes immortal before it is shared");+ Self::references(this.pointer.as_ptr()).store(IMMORTAL_REFERENCES, Ordering::Relaxed);+ this+ }+fn allocation(pointer: *const StyleValueData) -> *mut StyleValueAllocation {// SAFETY: Every pointer handed out points at the data field of a live allocation.unsafe { pointer.cast::<u8>().sub(Self::DATA_OFFSET).cast_mut().cast() }@@ -91,7 +109,11 @@ impl StyleValueArc {/// # Safety/// `pointer` must point at a live value.pub(crate) unsafe fn increment_strong_count(pointer: *const StyleValueData) {- let previous = Self::references(pointer).fetch_add(1, Ordering::Relaxed);+ let references = Self::references(pointer);+ if references.load(Ordering::Relaxed) == IMMORTAL_REFERENCES {+ return;+ }+ let previous = references.fetch_add(1, Ordering::Relaxed);if previous > MAXIMUM_REFERENCES {std::process::abort();}@@ -114,7 +136,11 @@ impl StyleValueArc {/// The value, where this is its only reference.pub(crate) fn into_inner(this: Self) -> Option<StyleValueData> {let pointer = Self::into_raw(this);- if Self::references(pointer).fetch_sub(1, Ordering::Release) != 1 {+ let references = Self::references(pointer);+ if references.load(Ordering::Relaxed) == IMMORTAL_REFERENCES {+ return None;+ }+ if references.fetch_sub(1, Ordering::Release) != 1 {return None;}fence(Ordering::Acquire);@@ -144,7 +170,11 @@ impl Clone for StyleValueArc {impl Drop for StyleValueArc {fn drop(&mut self) {- if Self::references(self.pointer.as_ptr()).fetch_sub(1, Ordering::Release) != 1 {+ let references = Self::references(self.pointer.as_ptr());+ if references.load(Ordering::Relaxed) == IMMORTAL_REFERENCES {+ return;+ }+ if references.fetch_sub(1, Ordering::Release) != 1 {return;}fence(Ordering::Acquire);@@ -255,6 +285,17 @@ mod tests {assert_eq!(payload_of(&moved_out), payload);}+ #[test]+ fn counts_no_references_to_an_immortal_value() {+ let value = StyleValueArc::immortal(StyleValueData::Number { value: 1.5 });+ let copy = value.clone();+ assert_eq!(StyleValueArc::strong_count(&value), super::IMMORTAL_REFERENCES);+ drop(copy);+ assert_eq!(StyleValueArc::strong_count(&value), super::IMMORTAL_REFERENCES);+ assert!(!StyleValueArc::is_unique(&value));+ assert!(StyleValueArc::into_inner(value).is_none());+ }+#[test]fn counts_references_through_raw_pointers() {let value = StyleValueArc::new(StyleValueData::Number { value: 1.5 });