Skip to content

Truly independent web browser

LibWeb: Sweep only match answers that may have lost their last reference

Andreas Kling58f636acacd7
diff --git a/Libraries/LibWeb/Rust/src/css/style/catalog.rs b/Libraries/LibWeb/Rust/src/css/style/catalog.rs
index 6b0c9fd7997..6df9e637f67 100644
--- a/Libraries/LibWeb/Rust/src/css/style/catalog.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/catalog.rs
@@ -104,6 +104,8 @@ pub(super) struct MatchAnswerCatalogEntry {
pub(super) cascade_payload_accounted: bool,
pub(super) retained_references: u32,
pending_references: u32,
+ /// Whether the catalog's sweep candidates name this answer already.
+ queued_for_sweep: bool,
}
#[derive(Clone)]
@@ -117,6 +119,12 @@ pub(super) struct MatchAnswerCatalog {
pub(super) needs_compaction: bool,
pending_payload_bytes: u64,
pending_memory: MemoryLease,
+ /// Every answer that may have lost its last reference without being removed since the last
+ /// sweep: each new answer, and each one whose cascade or pending count dropped to zero. A
+ /// sweep only has to look at these. Identities are never reused, so the slots of removed
+ /// answers stay behind, and walking all of them made every sweep slower than the last. Each
+ /// answer is noted once, so this holds no more entries than the catalog holds live answers.
+ sweep_candidates: Vec<MatchAnswerID>,
}
impl Default for MatchAnswerCatalog {
@@ -130,6 +138,7 @@ impl Default for MatchAnswerCatalog {
needs_compaction: false,
pending_payload_bytes: 0,
pending_memory: MemoryLease::new(MemoryCategory::BatchScratch),
+ sweep_candidates: Vec::new(),
}
}
}
@@ -175,6 +184,7 @@ impl MatchAnswerCatalog {
if entry.pending_references == 0 {
self.pending_payload_bytes -= Self::answer_payload_bytes(&entry.answer);
self.pending_memory.shrink_to(self.pending_payload_bytes);
+ Self::queue_for_sweep(&mut self.sweep_candidates, entry, identity);
}
// NB: Reclamation remains at the context boundary, after every pending owner
// has either transferred to a retained category or been released.
@@ -262,8 +272,10 @@ impl MatchAnswerCatalog {
cascade_payload_accounted: false,
retained_references: 0,
pending_references: 0,
+ queued_for_sweep: true,
}),
);
+ self.sweep_candidates.push(identity);
identity
}
@@ -331,6 +343,9 @@ impl MatchAnswerCatalog {
.cascade_references
.checked_sub(1)
.expect("releasing an unretained cascade input");
+ if entry.cascade_references == 0 {
+ Self::queue_for_sweep(&mut self.sweep_candidates, entry, identity);
+ }
if entry.cascade_references != 0 || (entry.prefix_references == 0 && entry.retained_references == 0) {
return;
}
@@ -376,20 +391,41 @@ impl MatchAnswerCatalog {
}
}
+ fn queue_for_sweep(
+ sweep_candidates: &mut Vec<MatchAnswerID>,
+ entry: &mut MatchAnswerCatalogEntry,
+ identity: MatchAnswerID,
+ ) {
+ if !entry.queued_for_sweep {
+ entry.queued_for_sweep = true;
+ sweep_candidates.push(identity);
+ }
+ }
+
+ #[cfg(test)]
+ pub(super) fn sweep_candidate_count(&self) -> usize {
+ self.sweep_candidates.len()
+ }
+
pub(super) fn sweep_unreferenced(&mut self) -> u64 {
let mut released_bytes = 0;
- for index in 0..self.answers.len() {
- let Some(entry) = self.answers[index].as_ref() else {
+ let mut candidates = std::mem::take(&mut self.sweep_candidates);
+ for identity in candidates.drain(..) {
+ // A candidate may have been removed since it was noted.
+ let Some(entry) = self.answers[identity].as_mut() else {
continue;
};
+ entry.queued_for_sweep = false;
if entry.prefix_references == 0
&& entry.cascade_references == 0
&& entry.pending_references == 0
&& entry.retained_references == 0
{
- released_bytes += self.remove_unreferenced(MatchAnswerID(index as u32 + 1));
+ released_bytes += self.remove_unreferenced(identity);
}
}
+ // Keep the allocation for the next batch of candidates.
+ self.sweep_candidates = candidates;
released_bytes
}
@@ -478,6 +514,7 @@ impl MatchAnswerCatalog {
self.needs_compaction,
self.pending_payload_bytes,
self.pending_memory,
+ self.sweep_candidates,
];
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/tests.rs b/Libraries/LibWeb/Rust/src/css/style/tests.rs
index 434b97f1127..f2a2883a719 100644
--- a/Libraries/LibWeb/Rust/src/css/style/tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/tests.rs
@@ -1391,6 +1391,41 @@ fn cascade_input_catalog_entries_follow_retained_column_lifetimes() {
);
}
+#[test]
+fn a_sweep_visits_only_answers_that_lost_their_last_reference_since_the_previous_sweep() {
+ let mut catalog = MatchAnswerCatalog::default();
+ let mut answers = RetainedMatchAnswers::default();
+ let mut memory = MemoryController::new();
+ let nodes: Vec<_> = (1..=8).map(StyleNodeID::element).collect();
+
+ // A new answer is a candidate until a sweep finds it referenced.
+ let identity = catalog.intern(&[]);
+ assert_eq!(catalog.sweep_candidate_count(), 1);
+ for &node in &nodes {
+ answers.remember_cascade_input(&mut catalog, node, identity, &mut memory);
+ }
+ catalog.sweep_unreferenced();
+ assert_eq!(catalog.sweep_candidate_count(), 0);
+ assert_eq!(catalog.answers.live_len(), 1);
+
+ // Releases that leave a reference behind note nothing for the next sweep.
+ for &node in &nodes[1..] {
+ answers.forget(&mut catalog, node);
+ }
+ assert_eq!(catalog.sweep_candidate_count(), 0);
+
+ // An answer that loses its last reference twice before a sweep is noted once.
+ answers.forget(&mut catalog, nodes[0]);
+ answers.remember_cascade_input(&mut catalog, nodes[0], identity, &mut memory);
+ answers.forget(&mut catalog, nodes[0]);
+ assert_eq!(catalog.sweep_candidate_count(), 1);
+
+ let released_cascade_payload_bytes = catalog.sweep_unreferenced();
+ answers.release_swept_cascade_payloads(released_cascade_payload_bytes);
+ assert_eq!(catalog.sweep_candidate_count(), 0);
+ assert!(catalog.answers.live_is_empty());
+}
+
#[test]
fn retained_match_answer_payloads_are_evictable_without_losing_identity() {
assert_eq!(size_of::<RetainedRuleMatch>(), 20);