Skip to content

Truly independent web browser

LibWeb: Keep routes of style sheets kept for reattachment

Andreas Kling75f48d778d17
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index b6b23bdc564..d51802a5638 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -61,6 +61,7 @@ void style_engine_attach_sheet(DocumentHost const*, SheetID sheet, TreeScopeID t
void style_engine_detach_sheet(DocumentHost const*, SheetID sheet, TreeScopeID tree_scope);
void style_engine_attach_sheet_occurrence(DocumentHost const*, SheetID sheet, TreeScopeID tree_scope, u64 identity, u64 before, bool conditions_hold);
void style_engine_detach_sheet_occurrence(DocumentHost const*, TreeScopeID tree_scope, u64 identity);
+void style_engine_set_sheet_kept_for_reattachment(DocumentHost const*, SheetID sheet, bool kept);
void style_engine_set_sheet_occurrence_conditions(DocumentHost const*, TreeScopeID tree_scope, u64 identity, bool conditions_hold);
void style_engine_set_element_part_exposure(DocumentHost const*, StyleNodeID node, StyleAtomID exposure);
void style_engine_set_element_directionality(DocumentHost const*, StyleNodeID node, StyleAtomID directionality);
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index 66c8994065f..abfacf111d2 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -2404,8 +2404,10 @@ static RefPtr<SharedCompiledStyleSheet> shared_compiled_style_sheet_for(StyleShe
// Keep a separate program when another occurrence already occupies this scope.
if ((*existing)->is_attached_to(tree_scope) && sheet.native_rules().has_anonymous_layer())
return nullptr;
- if (!(*existing)->has_attachments())
+ if (!(*existing)->has_attachments()) {
style_computer.detached_shared_compiled_style_sheets().remove_first_matching([&](auto const& detached) { return detached == key; });
+ StyleEngineFFI::style_engine_set_sheet_kept_for_reattachment(style_engine.host(), (*existing)->sheet_id(), false);
+ }
return *existing;
}
auto contents = StyleSheetState::create(sheet.native_rules().clone_shared_contents(), &document, RustMediaList {}, {});
@@ -2428,6 +2430,7 @@ static void release_shared_compiled_style_sheet(SharedCompiledStyleSheetKey cons
auto& shared_compiled_style_sheets = style_computer.shared_compiled_style_sheets();
auto sheet_id = shared_compiled_style_sheets.get(key).value()->sheet_id();
auto& style_engine = style_computer.style_engine();
+ StyleEngineFFI::style_engine_set_sheet_kept_for_reattachment(style_engine.host(), sheet_id, false);
StyleEngineFFI::style_engine_begin_sheet_rules_replacement(style_engine.host(), sheet_id.value());
style_engine.finish_sheet_rules_replacement(sheet_id);
shared_compiled_style_sheets.remove(key);
@@ -2448,6 +2451,7 @@ static void detach_shared_compiled_style_sheet(SharedCompiledStyleSheet& sheet,
// and since it holds the parsed contents, the parse cache answers the arriving text without parsing it again.
auto& detached = style_computer.detached_shared_compiled_style_sheets();
detached.append(sheet.key());
+ StyleEngineFFI::style_engine_set_sheet_kept_for_reattachment(style_engine.host(), sheet.sheet_id(), true);
if (detached.size() > StyleComputer::max_detached_shared_compiled_style_sheets)
release_shared_compiled_style_sheet(detached.take_first(), style_computer);
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/boundary.rs b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
index 1701d01f871..2947f1ac32f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/boundary.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
@@ -254,6 +254,8 @@ style_boundary! {
sheet: SheetID, tree_scope: TreeScopeID, identity: u64, before: u64, conditions_hold: bool
};
detach_sheet_occurrence => DetachSheetOccurrence { tree_scope: TreeScopeID, identity: u64 };
+ set_sheet_kept_for_reattachment => SetSheetKeptForReattachment { sheet: SheetID, kept: bool } =>
+ engine.retained.set_sheet_kept_for_reattachment(sheet, kept);
set_sheet_occurrence_conditions => SetSheetOccurrenceConditions {
tree_scope: TreeScopeID, identity: u64, conditions_hold: bool
};
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 1f395c76e9d..610816db7b1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1193,6 +1193,24 @@ impl RetainedState {
sheet
}
+ /// Whether a sheet's rules keep their routing entry points: it is attached somewhere, or the host keeps it for
+ /// reattachment.
+ pub(super) fn sheet_keeps_routing(&self, sheet: SheetID) -> bool {
+ self.program.sheet_is_attached_somewhere(sheet) || self.sheets_kept_for_reattachment.contains(sheet.0 as usize)
+ }
+
+ /// Note that the host keeps a detached sheet compiled for reattachment, or no longer does.
+ pub(super) fn set_sheet_kept_for_reattachment(&mut self, sheet: SheetID, kept: bool) {
+ if !self.sheets_kept_for_reattachment.set(sheet.0 as usize, kept).0 {
+ return;
+ }
+ if kept {
+ self.restore_routing_for_reattached_sheet(sheet);
+ } else {
+ self.routing_needs_detachment_sweep = true;
+ }
+ }
+
/// A sheet whose routes were shed while it was detached contributes routes again the moment
/// it reattaches. The registry must be whole before the attachment's transaction plans, so
/// this runs at recording time rather than waiting for the next sweep.
@@ -1970,6 +1988,7 @@ impl StyleEngine {
declaration_block_version: Arc::new(std::sync::atomic::AtomicU32::new(1)),
last_transaction_only_derived_child_reactions: false,
sheets_excluded_from_routing: BitColumn::default(),
+ sheets_kept_for_reattachment: BitColumn::default(),
routing_needs_detachment_sweep: false,
match_workspace: MatchScratch::default(),
exact_covered_scratch: Vec::new(),
@@ -3375,6 +3394,7 @@ impl RetainedState {
declaration_block_version: _,
last_transaction_only_derived_child_reactions: _,
sheets_excluded_from_routing: _,
+ sheets_kept_for_reattachment: _,
routing_needs_detachment_sweep: _,
match_workspace: _,
// Scratch of one candidate evaluation.
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index 98fd7644f1e..82253e6008a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -791,6 +791,9 @@ pub struct RetainedState {
/// sheet's rules decide nothing, so routing every input past their entry points is pure cost
/// that grows with every sheet that ever came and went.
sheets_excluded_from_routing: BitColumn,
+ /// Detached sheets the host keeps compiled because it expects them back. Their routes stay registered, since
+ /// shedding them would only have reattachment derive them all again.
+ sheets_kept_for_reattachment: BitColumn,
/// Whether a sheet detached since the last routing shed, so the registry may hold entry
/// points for rules that can no longer decide.
routing_needs_detachment_sweep: bool,
diff --git a/Libraries/LibWeb/Rust/src/css/style/ordering.rs b/Libraries/LibWeb/Rust/src/css/style/ordering.rs
index be95f5a132c..3123a4f7197 100644
--- a/Libraries/LibWeb/Rust/src/css/style/ordering.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/ordering.rs
@@ -1961,7 +1961,7 @@ impl RetainedState {
self.facts.apply_staged(&mut self.memory);
}
- /// Drop routing entry points for rules whose sheet is attached nowhere.
+ /// Drop routing entry points for rules whose sheet is attached nowhere and not kept for reattachment.
///
/// The router already skips such rules one route at a time, but enumerating their entry points
/// is itself a cost that grows with every sheet that ever came and went: a page that repeatedly
@@ -1976,7 +1976,7 @@ impl RetainedState {
let mut excluded_sheets = BitColumn::default();
let mut attached_sheets = Vec::new();
for (sheet, attached) in self.program.sheets_with_attachment_state() {
- if attached {
+ if attached || self.sheets_kept_for_reattachment.contains(sheet.0 as usize) {
attached_sheets.push(sheet);
} else {
excluded_sheets.set(sheet.0 as usize, true);
@@ -2034,7 +2034,7 @@ impl RetainedState {
// A detached sheet's rules keep no routing entry points; see
// `shed_routing_for_detached_sheets`.
let sheet = self.program.rule_sheet(rule);
- if !self.program.sheet_is_attached_somewhere(sheet) {
+ if !self.sheet_keeps_routing(sheet) {
excluded_sheets.set(sheet.0 as usize, true);
continue;
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/tests.rs b/Libraries/LibWeb/Rust/src/css/style/tests.rs
index 58d40ed3cc1..3656308656e 100644
--- a/Libraries/LibWeb/Rust/src/css/style/tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/tests.rs
@@ -10961,6 +10961,41 @@ fn detachment_routing_rebuild_excludes_retired_rules() {
);
}
+#[test]
+fn a_sheet_kept_for_reattachment_keeps_its_routes_while_detached() {
+ let mut engine = StyleEngine::new();
+ let sheet = engine.add_sheet(StyleSheetObjectID(1), CascadeOrigin::Author);
+ engine.attach_sheet(sheet, TreeScopeID::DOCUMENT);
+ let program = engine
+ .programs
+ .add(test_selector_program(".target", &[("target", StyleAtomID(1))]));
+ let rule = engine.append_rule(sheet, None, RuleKind::Style);
+ engine.add_routing_rule(rule, program);
+ let mut version = engine.program.rule_version(rule);
+ version.selector_program = Some(program);
+ engine.replace_rule_version(rule, version);
+ discard_transaction(&mut engine);
+
+ engine.detach_sheet(sheet, TreeScopeID::DOCUMENT);
+ engine.set_sheet_kept_for_reattachment(sheet, true);
+ discard_transaction(&mut engine);
+ assert_eq!(engine.routing.len(), 1);
+ assert!(!engine.program.rule_can_decide(rule));
+
+ let routing_before_reattachment = Arc::clone(&engine.routing);
+ engine.set_sheet_kept_for_reattachment(sheet, false);
+ engine.attach_sheet(sheet, TreeScopeID::DOCUMENT);
+ discard_transaction(&mut engine);
+ assert!(Arc::ptr_eq(&routing_before_reattachment, &engine.routing));
+
+ engine.detach_sheet(sheet, TreeScopeID::DOCUMENT);
+ engine.set_sheet_kept_for_reattachment(sheet, true);
+ discard_transaction(&mut engine);
+ engine.set_sheet_kept_for_reattachment(sheet, false);
+ discard_transaction(&mut engine);
+ assert_eq!(engine.routing.len(), 0);
+}
+
#[test]
fn a_declaration_edit_journals_only_the_declaration_field() {
let (mut engine, _, rule) = authoring_engine();