Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(style): bound live stylesheet mutation history

ldm0642e275b0340
diff --git a/moli-renderer-v8/src/live_stylesheet/import_graph.rs b/moli-renderer-v8/src/live_stylesheet/import_graph.rs
index 9107cfc4f..f5538f160 100644
--- a/moli-renderer-v8/src/live_stylesheet/import_graph.rs
+++ b/moli-renderer-v8/src/live_stylesheet/import_graph.rs
@@ -258,13 +258,7 @@ impl LiveStylesheet {
self.for_each_import_ancestor_including_self(|stylesheet| {
let generation = stylesheet.cascade_generation.get().saturating_add(1);
stylesheet.cascade_generation.set(generation);
- stylesheet
- .cascade_mutations
- .lock()
- .push(LiveStylesheetCascadeMutationBatch {
- generation,
- mutation: LiveStylesheetCascadeMutation::Full,
- });
+ stylesheet.publish_cascade_mutation(generation, LiveStylesheetCascadeMutation::Full);
stylesheet.derived_state.clear_dependency_summary();
stylesheet.font_face_cache.borrow_mut().take();
});
diff --git a/moli-renderer-v8/src/live_stylesheet/mod.rs b/moli-renderer-v8/src/live_stylesheet/mod.rs
index 604558cd2..d5dee1060 100644
--- a/moli-renderer-v8/src/live_stylesheet/mod.rs
+++ b/moli-renderer-v8/src/live_stylesheet/mod.rs
@@ -1,7 +1,7 @@
use std::{
borrow::Cow,
cell::{Cell, RefCell},
- collections::{HashMap, HashSet},
+ collections::{HashMap, HashSet, VecDeque},
rc::{Rc, Weak as RcWeak},
sync::{Arc as StdArc, Weak as StdWeak, atomic::AtomicBool},
};
@@ -41,6 +41,14 @@ use moli_css_parse::{
refresh_native_stylesheet_namespaces_after_cssom_mutation,
};
+/// Exact mutation history is an optimization, not a correctness cache. A
+/// consumer older than this window detects the generation gap and performs a
+/// full retained-stylesheet update instead of keeping old Stylo rule trees
+/// alive indefinitely.
+pub(crate) const LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY: usize = 256;
+
+pub(crate) type LiveStylesheetCascadeMutationJournal = VecDeque<LiveStylesheetCascadeMutationBatch>;
+
#[cfg(test)]
thread_local! {
static LIVE_STYLESHEET_CSS_TEXT_PROJECTION_COUNT: Cell<usize> = const { Cell::new(0) };
@@ -993,7 +1001,7 @@ pub(crate) struct LiveStylesheet {
cssom_runtime_state: LiveStylesheetCssomRuntimeState,
contents_revision: Cell<u64>,
cascade_generation: Cell<u64>,
- cascade_mutations: StdArc<Mutex<Vec<LiveStylesheetCascadeMutationBatch>>>,
+ cascade_mutations: StdArc<Mutex<LiveStylesheetCascadeMutationJournal>>,
import_generation: Cell<u64>,
parent: RefCell<Option<LiveStylesheetParent>>,
next_import_edge_id: Cell<u64>,
@@ -1016,8 +1024,7 @@ pub(crate) struct LiveStylesheet {
>,
}
-/// One exact Stylo rule mutation retained until every installation that still
-/// references an older cascade generation can observe it.
+/// One exact Stylo rule mutation retained within the bounded journal window.
#[derive(Clone, Debug)]
pub(crate) struct LiveStylesheetRuleMutation {
rule: CssRule,
@@ -1271,7 +1278,7 @@ impl LiveStylesheet {
cssom_runtime_state,
contents_revision: Cell::new(1),
cascade_generation: Cell::new(1),
- cascade_mutations: StdArc::new(Mutex::new(Vec::new())),
+ cascade_mutations: StdArc::new(Mutex::new(VecDeque::new())),
import_generation: Cell::new(1),
parent: RefCell::new(None),
next_import_edge_id: Cell::new(0),
@@ -1341,10 +1348,21 @@ impl LiveStylesheet {
pub(crate) fn cascade_mutation_journal(
&self,
- ) -> StdArc<Mutex<Vec<LiveStylesheetCascadeMutationBatch>>> {
+ ) -> StdArc<Mutex<LiveStylesheetCascadeMutationJournal>> {
StdArc::clone(&self.cascade_mutations)
}
+ fn publish_cascade_mutation(&self, generation: u64, mutation: LiveStylesheetCascadeMutation) {
+ let mut journal = self.cascade_mutations.lock();
+ if journal.len() == LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY {
+ journal.pop_front();
+ }
+ journal.push_back(LiveStylesheetCascadeMutationBatch {
+ generation,
+ mutation,
+ });
+ }
+
pub(crate) fn import_generation(&self) -> u64 {
self.import_generation.get()
}
diff --git a/moli-renderer-v8/src/live_stylesheet/mutation.rs b/moli-renderer-v8/src/live_stylesheet/mutation.rs
index a258d81fe..8e8d73a9c 100644
--- a/moli-renderer-v8/src/live_stylesheet/mutation.rs
+++ b/moli-renderer-v8/src/live_stylesheet/mutation.rs
@@ -897,7 +897,7 @@ impl LiveStylesheet {
let generation = self.cascade_generation();
let mut batches = self.cascade_mutations.lock();
let batch = batches
- .last_mut()
+ .back_mut()
.expect("a cascade mutation must publish one journal batch");
debug_assert_eq!(batch.generation, generation);
batch.mutation = LiveStylesheetCascadeMutation::Rules(mutations);
diff --git a/moli-renderer-v8/src/style_engine/source/store.rs b/moli-renderer-v8/src/style_engine/source/store.rs
index 3903555fe..52abf400b 100644
--- a/moli-renderer-v8/src/style_engine/source/store.rs
+++ b/moli-renderer-v8/src/style_engine/source/store.rs
@@ -58,7 +58,7 @@ enum StyloStylesheetSourceContents {
contents_revision: u64,
cascade_generation: u64,
cascade_mutations: StdArc<
- parking_lot::Mutex<Vec<crate::live_stylesheet::LiveStylesheetCascadeMutationBatch>>,
+ parking_lot::Mutex<crate::live_stylesheet::LiveStylesheetCascadeMutationJournal>,
>,
derived_state: StdArc<crate::live_stylesheet::LiveStylesheetDerivedState>,
shared_initial_contents: Option<StdArc<crate::live_stylesheet::SharedStylesheetContents>>,
@@ -663,3 +663,163 @@ fn update_style_scope_identity_hash(hasher: &mut Sha256Context, scope_id: StyleS
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use style::{context::QuirksMode, shared_lock::SharedRwLock, stylesheets::AllowImportRules};
+
+ use super::{LiveStylesheetCascadeUpdate, StyloStylesheetSource};
+ use crate::live_stylesheet::{
+ LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY, LiveStylesheetRegistry,
+ };
+
+ #[test]
+ fn live_cascade_mutation_journal_is_bounded_with_safe_full_fallback() {
+ let registry = LiveStylesheetRegistry::default();
+ let stylesheet = registry.create(
+ ".target { color: rgb(0, 0, 0); }",
+ url::Url::parse("https://cascade-journal.test/style.css").expect("stylesheet URL"),
+ QuirksMode::NoQuirks,
+ AllowImportRules::Yes,
+ SharedRwLock::new(),
+ );
+ let initial = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+
+ stylesheet
+ .replace_rule(".target { color: rgb(1, 2, 3); }", 0)
+ .expect("first exact rule replacement");
+ let first_update = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+ assert!(matches!(
+ first_update.live_cascade_update_since(&initial),
+ LiveStylesheetCascadeUpdate::Rules(changes) if !changes.is_empty()
+ ));
+
+ let lagging_installation = first_update;
+ for index in 0..2_048 {
+ stylesheet
+ .replace_rule(
+ &format!(
+ ".target {{ color: rgb({}, {}, {}); }}",
+ index % 251,
+ (index + 1) % 251,
+ (index + 2) % 251,
+ ),
+ 0,
+ )
+ .expect("stress replacement should remain valid");
+ }
+ assert_eq!(
+ stylesheet.cascade_mutation_journal().lock().len(),
+ LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY,
+ "old rule trees must not remain strongly retained without bound",
+ );
+ let after_stress = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+ assert!(matches!(
+ after_stress.live_cascade_update_since(&lagging_installation),
+ LiveStylesheetCascadeUpdate::Full
+ ));
+
+ let caught_up_installation = after_stress;
+ stylesheet
+ .replace_rule(".target { color: rgb(7, 8, 9); }", 0)
+ .expect("latest exact rule replacement");
+ let latest = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+ assert!(matches!(
+ latest.live_cascade_update_since(&caught_up_installation),
+ LiveStylesheetCascadeUpdate::Rules(changes) if !changes.is_empty()
+ ));
+ }
+
+ #[test]
+ fn live_cascade_journal_capacity_boundary_keeps_exact_updates() {
+ let registry = LiveStylesheetRegistry::default();
+ let stylesheet = registry.create(
+ ".target { color: rgb(0, 0, 0); }",
+ url::Url::parse("https://cascade-journal-boundary.test/style.css")
+ .expect("stylesheet URL"),
+ QuirksMode::NoQuirks,
+ AllowImportRules::Yes,
+ SharedRwLock::new(),
+ );
+ let one_generation_too_old = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+ stylesheet
+ .replace_rule(".target { color: rgb(1, 2, 3); }", 0)
+ .expect("boundary seed replacement");
+ let oldest_exact_predecessor = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+
+ for index in 0..LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY {
+ stylesheet
+ .replace_rule(&format!(".target {{ z-index: {}; }}", index % 100), 0)
+ .expect("boundary replacement should remain valid");
+ }
+ let current = StyloStylesheetSource::from_live_stylesheet(&stylesheet);
+ assert!(matches!(
+ current.live_cascade_update_since(&oldest_exact_predecessor),
+ LiveStylesheetCascadeUpdate::Rules(changes) if !changes.is_empty()
+ ));
+ assert!(matches!(
+ current.live_cascade_update_since(&one_generation_too_old),
+ LiveStylesheetCascadeUpdate::Full
+ ));
+ }
+
+ #[test]
+ fn imported_child_mutations_bound_both_child_and_ancestor_journals() {
+ let registry = LiveStylesheetRegistry::default();
+ let root = registry.create(
+ "@import './child.css'; .root { display: block; }",
+ url::Url::parse("https://cascade-import-journal.test/root.css")
+ .expect("root stylesheet URL"),
+ QuirksMode::NoQuirks,
+ AllowImportRules::Yes,
+ SharedRwLock::new(),
+ );
+ let request = root
+ .pending_import_requests()
+ .into_iter()
+ .next()
+ .expect("pending child import");
+ let child = registry
+ .install_import_response(
+ root.id(),
+ root.contents_revision(),
+ request.edge_id,
+ ".child { color: rgb(0, 0, 0); }",
+ request.url,
+ true,
+ true,
+ )
+ .expect("child import should install");
+ let root_before_mutation = StyloStylesheetSource::from_live_stylesheet(&root);
+ let child_before_mutation = StyloStylesheetSource::from_live_stylesheet(&child);
+
+ child
+ .replace_rule(".child { color: rgb(1, 2, 3); }", 0)
+ .expect("child exact replacement");
+ let root_after_mutation = StyloStylesheetSource::from_live_stylesheet(&root);
+ let child_after_mutation = StyloStylesheetSource::from_live_stylesheet(&child);
+ assert!(matches!(
+ child_after_mutation.live_cascade_update_since(&child_before_mutation),
+ LiveStylesheetCascadeUpdate::Rules(changes) if !changes.is_empty()
+ ));
+ assert!(matches!(
+ root_after_mutation.live_cascade_update_since(&root_before_mutation),
+ LiveStylesheetCascadeUpdate::Full
+ ));
+
+ for index in 0..(LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY * 2 + 3) {
+ child
+ .replace_rule(&format!(".child {{ z-index: {}; }}", index % 100), 0)
+ .expect("import stress replacement should remain valid");
+ }
+ assert_eq!(
+ child.cascade_mutation_journal().lock().len(),
+ LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY,
+ );
+ assert_eq!(
+ root.cascade_mutation_journal().lock().len(),
+ LIVE_STYLESHEET_CASCADE_MUTATION_JOURNAL_CAPACITY,
+ "descendant mutations must not grow an ancestor journal without bound",
+ );
+ }
+}