Skip to content

Truly independent web browser

LibWeb: Look for @function rules only after style sheets change

Andreas Kling703d61f814b8
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
index f961fd42751..fdc62f5bb61 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
@@ -586,12 +586,17 @@ void StyleEngine::gather_computation_inputs(LentComputationInputs& lent)
// Each scope's @function rules, published after the media environment is settled: a media
// change rebuilds the definitions. The style engine decides which of them a call names. Most
// documents hold none, and then no scope is published.
- auto has_function_rules = [](StyleScope const& scope) { return !scope.rule_cache().function_rules.is_empty(); };
- bool document_has_function_rules = has_function_rules(document.style_scope());
- document.for_each_shadow_root([&](DOM::ShadowRoot& shadow_root) {
- document_has_function_rules = document_has_function_rules || has_function_rules(shadow_root.style_scope());
- });
- if (document_has_function_rules) {
+ // OPTIMIZATION: Only a change to the document's style sheets or shadow roots moves the answer, so a style update
+ // visits them only after one, and a document with many shadow trees pays nothing per update.
+ if (m_function_rules_checked_generation != document.style_sheet_set_generation()) {
+ m_function_rules_checked_generation = document.style_sheet_set_generation();
+ auto has_function_rules = [](StyleScope const& scope) { return !scope.rule_cache().function_rules.is_empty(); };
+ m_document_has_function_rules = has_function_rules(document.style_scope());
+ document.for_each_shadow_root([&](DOM::ShadowRoot& shadow_root) {
+ m_document_has_function_rules = m_document_has_function_rules || has_function_rules(shadow_root.style_scope());
+ });
+ }
+ if (m_document_has_function_rules) {
auto& style_engine = m_style_computer->style_engine();
auto publish_scope = [&](StyleScope const& scope) {
StyleEngineFFI::FfiCustomFunctionEntry entry {
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index 9a5b7c5ea45..b91ce5704f7 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -447,6 +447,8 @@ private:
Optional<StyleSheetResourceContexts> m_style_sheet_resource_contexts;
u64 m_style_sheet_resource_contexts_version { 0 };
+ Optional<u64> m_function_rules_checked_generation;
+ bool m_document_has_function_rules { false };
NonnullRefPtr<Layout::RenderDocument> m_render_document;
u64 m_published_font_environment_generation { 0 };