Truly independent web browser
LibWeb: Forget cached animation styles the style engine releases
Tim Ledbetter3dc91fab808f
diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rsindex b6b3007656c..a79a73088e1 100644--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs@@ -338,6 +338,41 @@ impl Drop for StyleRecordLease {}}+/// A fork never takes its records, so past [`Self::MAX_RECORDS`] only the fact that they overflowed is kept.+#[derive(Clone, Default)]+pub(crate) struct ReleasedAnimationOverlayRecords {+ records: Vec<FinalStyleRecordID>,+ overflowed: bool,+}++impl ReleasedAnimationOverlayRecords {+ const MAX_RECORDS: usize = 1024;++ fn add(&mut self, record: FinalStyleRecordID) {+ if self.overflowed {+ return;+ }+ if self.records.len() == Self::MAX_RECORDS {+ self.records = Vec::new();+ self.overflowed = true;+ return;+ }+ self.records.push(record);+ }++ fn is_empty(&self) -> bool {+ self.records.is_empty() && !self.overflowed+ }++ pub(crate) fn records(&self) -> impl Iterator<Item = u64> + '_ {+ self.records.iter().map(|record| record.raw())+ }++ pub(crate) fn overflowed(&self) -> bool {+ self.overflowed+ }+}+struct AnimationOverlayRecord {// NB: No sampled value enters a permanent interning table. The current assignment owns one// reference, while detached layout and stabilization baselines can pin an old generation.@@ -885,6 +920,7 @@ pub struct ComputedGroupSets {// above. Dense element assignments and sparse pseudo assignments pin at most one slot each.animation_overlay_slots: animation_overlay_slots::AnimationOverlaySlots,animation_overlay_slots_by_record: HashMap<FinalStyleRecordID, u32>,+ released_animation_overlay_records: ReleasedAnimationOverlayRecords,/// How many leases are held, shared with every copy of the sets: a copy holds no lease of its own.leases: std::sync::Arc<std::sync::atomic::AtomicUsize>,live_animation_overlay_assignments: usize,@@ -920,6 +956,7 @@ impl Default for ComputedGroupSets {columns: PublishedComputedColumns::default(),animation_overlay_slots: animation_overlay_slots::AnimationOverlaySlots::default(),animation_overlay_slots_by_record: HashMap::default(),+ released_animation_overlay_records: ReleasedAnimationOverlayRecords::default(),leases: std::sync::Arc::default(),live_animation_overlay_assignments: 0,next_animation_overlay_generation: 0,@@ -1013,6 +1050,14 @@ impl ComputedGroupSets {self.animation_overlay_slots.retired_records()}+ pub(super) fn has_released_animation_overlay_records(&self) -> bool {+ !self.released_animation_overlay_records.is_empty()+ }++ pub(super) fn take_released_animation_overlay_records(&mut self) -> ReleasedAnimationOverlayRecords {+ std::mem::take(&mut self.released_animation_overlay_records)+ }+pub(crate) fn end_style_record_view_epoch(&mut self) {assert!(self.style_record_view_epoch_depth > 0,@@ -2097,6 +2142,7 @@ impl ComputedGroupSets {assert!(!record.is_assigned && record.pin_count == 0);let final_style_record = self.animation_overlay_slots.retire(slot);self.animation_overlay_slots_by_record.remove(&final_style_record);+ self.released_animation_overlay_records.add(final_style_record);self.free_retired_animation_overlays();}@@ -2169,6 +2215,7 @@ impl ComputedGroupSets {let final_style_record = record.final_style_record;let retired = self.animation_overlay_slots.replace(slot, record);self.animation_overlay_slots_by_record.remove(&retired);+ self.released_animation_overlay_records.add(retired);self.animation_overlay_slots_by_record.insert(final_style_record, slot);self.free_retired_animation_overlays();return AnimationOverlayPublication {@@ -3500,6 +3547,7 @@ impl ComputedGroupSets {shallow [self.animation_overlay_slots,self.animation_overlay_slots_by_record,+ self.released_animation_overlay_records.records,self.columns.animation_overlay_slots,];cached [self.animation_overlay_nested_memory.bytes()];@@ -3988,6 +4036,7 @@ impl Clone for ComputedGroupSets {columns: self.columns.clone(),animation_overlay_slots: self.animation_overlay_slots.clone(),animation_overlay_slots_by_record: self.animation_overlay_slots_by_record.clone(),+ released_animation_overlay_records: self.released_animation_overlay_records.clone(),// NB: Either copy may be the host's, whose published frames hold their leases here.leases: self.leases.clone(),live_animation_overlay_assignments: self.live_animation_overlay_assignments,@@ -4459,6 +4508,59 @@ mod tests {assert_eq!(sets.live_animation_overlay_records(), 0);}+ #[test]+ fn released_animation_overlay_records_name_each_record_the_engine_stops_resolving() {+ let mut sets = ComputedGroupSets::default();+ let node = StyleNodeID::from_raw(1).unwrap();+ let target = ComputedStyleTarget::new(node, u8::MAX);+ let animated_overlay = crate::css::animated_overlay::AnimatedOverlay::default();+ let publish = |sets: &mut ComputedGroupSets, identity| {+ let mut metadata = metadata(0, 0, 0);+ metadata.animation_overlay_identity = identity;+ metadata.animated_overlay = HostShared::new(std::ptr::from_ref(&animated_overlay));+ sets.publish_unowned(Some(target), &[], 0, 0, metadata)+ };+ let released = |sets: &mut ComputedGroupSets| {+ let released = sets.take_released_animation_overlay_records();+ assert!(!released.overflowed());+ released.records().collect::<Vec<_>>()+ };++ let first = publish(&mut sets, 1);+ assert!(!sets.has_released_animation_overlay_records());++ let second = publish(&mut sets, 2);+ assert_eq!(released(&mut sets), [first.style_record_identity.raw()]);++ sets.pin_style_record(second.style_record_identity.raw());+ let third = publish(&mut sets, 3);+ assert_ne!(second.style_record_identity, third.style_record_identity);+ assert!(!sets.has_released_animation_overlay_records());+ sets.unpin_style_record(second.style_record_identity.raw());+ assert_eq!(released(&mut sets), [second.style_record_identity.raw()]);++ sets.remove(node);+ assert_eq!(released(&mut sets), [third.style_record_identity.raw()]);+ assert!(!sets.has_released_animation_overlay_records());+ }++ #[test]+ fn released_animation_overlay_records_beyond_the_limit_keep_only_the_overflow() {+ let mut released = ReleasedAnimationOverlayRecords::default();+ for generation in 1..=ReleasedAnimationOverlayRecords::MAX_RECORDS as u64 {+ released.add(FinalStyleRecordID::animation_overlay(generation));+ }+ assert!(!released.overflowed());+ assert_eq!(released.records().count(), ReleasedAnimationOverlayRecords::MAX_RECORDS);++ released.add(FinalStyleRecordID::animation_overlay(+ ReleasedAnimationOverlayRecords::MAX_RECORDS as u64 + 1,+ ));+ assert!(released.overflowed());+ assert_eq!(released.records().count(), 0);+ assert!(!released.is_empty());+ }+#[test]fn a_style_record_lease_keeps_a_replaced_animation_overlay_until_it_drops() {let mut sets = ComputedGroupSets::default();diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rsindex 2e6ffc5eecc..7ee1ca46a3d 100644--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs@@ -827,6 +827,23 @@ pub(crate) struct EngineMemo {}impl EngineMemo {+ pub(crate) fn forget_released_animation_overlay_records(&self, released: &super::ReleasedAnimationOverlayRecords) {+ if released.overflowed() {+ let is_animation_overlay = |key: u64| {+ super::computed::FinalStyleRecordID::from_raw(key).is_some_and(|record| record.is_animation_overlay())+ };+ self.views.forget_where(is_animation_overlay);+ self.record_environments.forget_where(is_animation_overlay);+ self.dependency_flags.forget_where(is_animation_overlay);+ return;+ }+ for record in released.records() {+ self.views.forget(record);+ self.record_environments.forget(record);+ self.dependency_flags.forget(record);+ }+ }+/// Follows `change`, a write the host queued for the engine, as the host makes it.pub(crate) fn follow(&self, change: &super::boundary::StyleChange) {self.baselines.borrow_mut().follow(change);@@ -1313,6 +1330,23 @@ impl<T: Copy> Memo<T> {pub(crate) fn set(&self, key: u64, answer: T) {self.slot(key).set((key, answer));}++ fn forget(&self, key: u64) {+ let slot = self.slot(key);+ let (held, answer) = slot.get();+ if held == key {+ slot.set((Self::NO_KEY, answer));+ }+ }++ fn forget_where(&self, forgets: impl Fn(u64) -> bool) {+ for slot in &self.slots {+ let (held, answer) = slot.get();+ if held != Self::NO_KEY && forgets(held) {+ slot.set((Self::NO_KEY, answer));+ }+ }+ }}/// The engine's id of the native rule `identity` names, plus one, or 0 for none.diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rsindex 4c02b60729a..f0d8608166a 100644--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs@@ -127,7 +127,7 @@ mod style_invalidation;pub(crate) use style_invalidation::SampleBounds;mod transition_baselines;mod user_agent_selectors;-pub(crate) use computed::{ENGINE_INHERITED_GROUP_COUNT, StyleRecordLease};+pub(crate) use computed::{ENGINE_INHERITED_GROUP_COUNT, ReleasedAnimationOverlayRecords, StyleRecordLease};pub(crate) use publication::RecordDemand;pub(crate) use transition_baselines::{InheritedAnimatedValue, TransitionBaselines};pub mod relative_selector;diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rsindex d44765221b0..53430fc5211 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs@@ -3968,6 +3968,14 @@ impl RetainedState {self.computed_group_sets.style_record_view(style_record)}+ pub(crate) fn take_released_animation_overlay_records(&mut self) -> computed::ReleasedAnimationOverlayRecords {+ // Even an empty take copies the sets while a fork shares them.+ if !self.computed_group_sets.has_released_animation_overlay_records() {+ return computed::ReleasedAnimationOverlayRecords::default();+ }+ self.computed_group_sets.take_released_animation_overlay_records()+ }+pub(crate) fn pin_style_record(&mut self, style_record: u64) {self.computed_group_sets.pin_style_record(style_record);}diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rsindex 355e33d89ca..2ee678b1dca 100644--- a/Libraries/LibWeb/Rust/src/render_state.rs+++ b/Libraries/LibWeb/Rust/src/render_state.rs@@ -179,6 +179,7 @@ impl RenderState {Owed {work: std::mem::take(&mut self.owed),deferred_inputs: self.engine_mut().take_moved_deferred_element_style_inputs(),+ released_animation_overlay_records: self.engine_mut().take_released_animation_overlay_records(),facts,selector_value_text_names,selector_tested_attribute_names,@@ -270,6 +271,7 @@ pub(crate) struct Landing {pub(crate) struct Owed {work: Vec<crate::layout::tree_mutation::HostWorkDue>,deferred_inputs: Option<Vec<crate::css::style::engine_calls::DeferredInput>>,+ released_animation_overlay_records: crate::css::style::ReleasedAnimationOverlayRecords,facts: StateFacts,/// The attribute names whose value text the engine's selectors read, as of `facts`.selector_value_text_names: crate::css::style::SelectorAttributeNames,diff --git a/Libraries/LibWeb/Rust/src/render_state/document_host.rs b/Libraries/LibWeb/Rust/src/render_state/document_host.rsindex caad2ad4344..21b23961298 100644--- a/Libraries/LibWeb/Rust/src/render_state/document_host.rs+++ b/Libraries/LibWeb/Rust/src/render_state/document_host.rs@@ -359,11 +359,14 @@ impl DocumentHost {Owed {work,deferred_inputs,+ released_animation_overlay_records,facts,selector_value_text_names,selector_tested_attribute_names,}: Owed,) {+ self.engine_memo+ .forget_released_animation_overlay_records(&released_animation_overlay_records);self.facts.set(Some(facts));*self.selector_value_text_names.borrow_mut() = selector_value_text_names;*self.selector_tested_attribute_names.borrow_mut() = selector_tested_attribute_names;