Skip to content

Truly independent web browser

LibWeb: Shrink the group caches after a run of small style passes

Andreas Klingdc8b5ae017fe
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index 54684f71fac..141b77be77e 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -1880,6 +1880,8 @@ pub(in crate::css::style) struct GroupCache {
/// The distinct inputs of the entries, which the keys name by index. The elements of a pass
/// mostly assemble their groups against the same ones.
key_inputs: Vec<GroupInputs>,
+ /// How many passes in a row the cache came out of holding no more than it keeps across passes.
+ small_passes: u8,
}
/// What an element's groups are built from beside their values, but for the color they resolve
@@ -1984,10 +1986,20 @@ impl GroupCache {
pub(in crate::css::style) fn kept(mut self) -> Self {
if self.count > Self::KEPT_ACROSS_PASSES {
self.clear();
+ return self;
+ }
+ // NB: A large pass leaves its room behind for the next one. Where the passes since have all been small, as an
+ // animation's are, the room goes back, and a large pass grows it again.
+ self.small_passes = self.small_passes.saturating_add(1);
+ if self.small_passes == Self::SMALL_PASSES_BEFORE_SHRINKING {
+ self.trim();
}
self
}
+ /// How many small passes in a row give back the room a large pass left.
+ const SMALL_PASSES_BEFORE_SHRINKING: u8 = 32;
+
/// Lets go of the room the cache holds beyond its entries, which it keeps for the next pass: a few elements restyled
/// later, as on hover, still find their payloads.
pub(in crate::css::style) fn trim(&mut self) {
@@ -2142,6 +2154,8 @@ impl GroupCache {
self.key_values.clear();
self.key_inputs.clear();
self.count = 0;
+ // NB: A pass that filled the cache was a large one, whatever it holds once it ends.
+ self.small_passes = 0;
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/per_thread.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/per_thread.rs
index f6dc9bed576..20f844fc004 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/per_thread.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/per_thread.rs
@@ -27,6 +27,14 @@ impl<T: Default> PerThread<T> {
self.0.iter().filter_map(|slot| slot.get().map(|made| &**made))
}
+ /// What the threads that asked keep, with the index of each thread.
+ pub(super) fn made_with_index(&self) -> impl Iterator<Item = (usize, &Mutex<T>)> {
+ self.0
+ .iter()
+ .enumerate()
+ .filter_map(|(index, slot)| slot.get().map(|made| (index, &**made)))
+ }
+
/// How many threads may keep anything.
pub(super) fn len(&self) -> usize {
self.0.len()
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
index c4aced838d3..3f68ea1f449 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
@@ -3057,6 +3057,7 @@ fn style_queued<T: Sync>(
}
}
let mut statistics = Statistics::default();
+ let mut styled_threads = 0u64;
for (index, scratch) in scratches.slots.into_iter().enumerate() {
let Some(mut scratch) = scratch.into_inner().expect("no thread panicked while styling") else {
continue;
@@ -3065,9 +3066,22 @@ fn style_queued<T: Sync>(
&& let Some(cache) = context.group_caches.and_then(|caches| caches.get(index))
{
*cache.lock().expect("no thread panicked while styling") = std::mem::take(&mut scratch.groups).kept();
+ styled_threads |= 1 << index;
}
statistics.add(scratch);
}
+ // NB: The caches of the threads that sat the pass out count it as a small one too, so the room a large pass left
+ // them goes back while only the calling thread styles, as it does for an animation.
+ if context.uses_caches
+ && let Some(caches) = context.group_caches
+ {
+ for (index, cache) in caches.made_with_index() {
+ if styled_threads & (1 << index) == 0 {
+ let mut cache = cache.lock().expect("no thread panicked while styling");
+ *cache = std::mem::take(&mut *cache).kept();
+ }
+ }
+ }
statistics.cache_audit.conclude(context.cache_audit);
statistics
}