Title: [253690] trunk/Source/WebCore
Revision
253690
Author
[email protected]
Date
2019-12-18 09:22:37 -0800 (Wed, 18 Dec 2019)

Log Message

Style::Invalidator should be able to invalidate using multiple RuleSets in one pass
https://bugs.webkit.org/show_bug.cgi?id=205391

Reviewed by Zalan Bujtas.

It is inefficient to do multiple passes over DOM if there are multiple RuleSets to invalidate with.

* style/AttributeChangeInvalidation.cpp:
(WebCore::Style::AttributeChangeInvalidation::invalidateStyle):

Collect to a HashMap per MatchElement.

(WebCore::Style::AttributeChangeInvalidation::invalidateStyleWithRuleSets):

Call to common invalidation function in Style::Invalidator.

* style/AttributeChangeInvalidation.h:
* style/ClassChangeInvalidation.cpp:
(WebCore::Style::ClassChangeInvalidation::computeInvalidation):
(WebCore::Style::ClassChangeInvalidation::invalidateStyleWithRuleSets):

Same for classes.

* style/ClassChangeInvalidation.h:
* style/StyleInvalidator.cpp:
(WebCore::Style::Invalidator::Invalidator):

Take a Vector of RuleSets.

(WebCore::Style::Invalidator::invalidateIfNeeded):

Loop as needed.

(WebCore::Style::Invalidator::invalidateStyle):
(WebCore::Style::Invalidator::invalidateInShadowTreeIfNeeded):
(WebCore::Style::Invalidator::invalidateWithMatchElementRuleSets):

Factor the invalidation loop and HashMap to Invalidator.

* style/StyleInvalidator.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (253689 => 253690)


--- trunk/Source/WebCore/ChangeLog	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/ChangeLog	2019-12-18 17:22:37 UTC (rev 253690)
@@ -1,3 +1,46 @@
+2019-12-18  Antti Koivisto  <[email protected]>
+
+        Style::Invalidator should be able to invalidate using multiple RuleSets in one pass
+        https://bugs.webkit.org/show_bug.cgi?id=205391
+
+        Reviewed by Zalan Bujtas.
+
+        It is inefficient to do multiple passes over DOM if there are multiple RuleSets to invalidate with.
+
+        * style/AttributeChangeInvalidation.cpp:
+        (WebCore::Style::AttributeChangeInvalidation::invalidateStyle):
+
+        Collect to a HashMap per MatchElement.
+
+        (WebCore::Style::AttributeChangeInvalidation::invalidateStyleWithRuleSets):
+
+        Call to common invalidation function in Style::Invalidator.
+
+        * style/AttributeChangeInvalidation.h:
+        * style/ClassChangeInvalidation.cpp:
+        (WebCore::Style::ClassChangeInvalidation::computeInvalidation):
+        (WebCore::Style::ClassChangeInvalidation::invalidateStyleWithRuleSets):
+
+        Same for classes.
+
+        * style/ClassChangeInvalidation.h:
+        * style/StyleInvalidator.cpp:
+        (WebCore::Style::Invalidator::Invalidator):
+
+        Take a Vector of RuleSets.
+
+        (WebCore::Style::Invalidator::invalidateIfNeeded):
+
+        Loop as needed.
+
+        (WebCore::Style::Invalidator::invalidateStyle):
+        (WebCore::Style::Invalidator::invalidateInShadowTreeIfNeeded):
+        (WebCore::Style::Invalidator::invalidateWithMatchElementRuleSets):
+
+        Factor the invalidation loop and HashMap to Invalidator.
+
+        * style/StyleInvalidator.h:
+
 2019-12-18  Simon Fraser  <[email protected]>
 
         Move m_exposedContentRect into the DelegatedScrollingGeometry optional

Modified: trunk/Source/WebCore/style/AttributeChangeInvalidation.cpp (253689 => 253690)


--- trunk/Source/WebCore/style/AttributeChangeInvalidation.cpp	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/AttributeChangeInvalidation.cpp	2019-12-18 17:22:37 UTC (rev 253690)
@@ -28,8 +28,6 @@
 
 #include "ElementIterator.h"
 #include "StyleInvalidationFunctions.h"
-#include "StyleInvalidator.h"
-#include <wtf/SetForScope.h>
 
 namespace WebCore {
 namespace Style {
@@ -80,7 +78,7 @@
             bool oldMatches = !oldValue.isNull() && SelectorChecker::attributeSelectorMatches(m_element, attributeName, oldValue, *selector);
             bool newMatches = !newValue.isNull() && SelectorChecker::attributeSelectorMatches(m_element, attributeName, newValue, *selector);
             if (oldMatches != newMatches) {
-                m_invalidationRuleSets.append(&invalidationRuleSet);
+                Invalidator::addToMatchElementRuleSets(m_matchElementRuleSets, invalidationRuleSet);
                 break;
             }
         }
@@ -89,12 +87,7 @@
 
 void AttributeChangeInvalidation::invalidateStyleWithRuleSets()
 {
-    SetForScope<bool> isInvalidating(m_element.styleResolver().ruleSets().isInvalidatingStyleWithRuleSets(), true);
-
-    for (auto* invalidationRuleSet : m_invalidationRuleSets) {
-        Invalidator invalidator(*invalidationRuleSet->ruleSet);
-        invalidator.invalidateStyleWithMatchElement(m_element, invalidationRuleSet->matchElement);
-    }
+    Invalidator::invalidateWithMatchElementRuleSets(m_element, m_matchElementRuleSets);
 }
 
 

Modified: trunk/Source/WebCore/style/AttributeChangeInvalidation.h (253689 => 253690)


--- trunk/Source/WebCore/style/AttributeChangeInvalidation.h	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/AttributeChangeInvalidation.h	2019-12-18 17:22:37 UTC (rev 253690)
@@ -26,12 +26,11 @@
 #pragma once
 
 #include "Element.h"
+#include "StyleInvalidator.h"
 
 namespace WebCore {
 namespace Style {
 
-struct InvalidationRuleSet;
-
 class AttributeChangeInvalidation {
 public:
     AttributeChangeInvalidation(Element&, const QualifiedName&, const AtomString& oldValue, const AtomString& newValue);
@@ -44,7 +43,7 @@
     const bool m_isEnabled;
     Element& m_element;
 
-    Vector<const InvalidationRuleSet*, 4> m_invalidationRuleSets;
+    Invalidator::MatchElementRuleSets m_matchElementRuleSets;
 };
 
 inline AttributeChangeInvalidation::AttributeChangeInvalidation(Element& element, const QualifiedName& attributeName, const AtomString& oldValue, const AtomString& newValue)

Modified: trunk/Source/WebCore/style/ClassChangeInvalidation.cpp (253689 => 253690)


--- trunk/Source/WebCore/style/ClassChangeInvalidation.cpp	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/ClassChangeInvalidation.cpp	2019-12-18 17:22:37 UTC (rev 253690)
@@ -29,9 +29,7 @@
 #include "ElementChildIterator.h"
 #include "SpaceSplitString.h"
 #include "StyleInvalidationFunctions.h"
-#include "StyleInvalidator.h"
 #include <wtf/BitVector.h>
-#include <wtf/SetForScope.h>
 
 namespace WebCore {
 namespace Style {
@@ -113,7 +111,7 @@
     for (auto* changedClass : changedClasses) {
         if (auto* invalidationRuleSets = ruleSets.classInvalidationRuleSets(changedClass)) {
             for (auto& invalidationRuleSet : *invalidationRuleSets)
-                m_invalidationRuleSets.append(&invalidationRuleSet);
+                Invalidator::addToMatchElementRuleSets(m_matchElementRuleSets, invalidationRuleSet);
         }
     }
 }
@@ -120,12 +118,7 @@
 
 void ClassChangeInvalidation::invalidateStyleWithRuleSets()
 {
-    SetForScope<bool> isInvalidating(m_element.styleResolver().ruleSets().isInvalidatingStyleWithRuleSets(), true);
-
-    for (auto* invalidationRuleSet : m_invalidationRuleSets) {
-        Invalidator invalidator(*invalidationRuleSet->ruleSet);
-        invalidator.invalidateStyleWithMatchElement(m_element, invalidationRuleSet->matchElement);
-    }
+    Invalidator::invalidateWithMatchElementRuleSets(m_element, m_matchElementRuleSets);
 }
 
 }

Modified: trunk/Source/WebCore/style/ClassChangeInvalidation.h (253689 => 253690)


--- trunk/Source/WebCore/style/ClassChangeInvalidation.h	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/ClassChangeInvalidation.h	2019-12-18 17:22:37 UTC (rev 253690)
@@ -26,17 +26,15 @@
 #pragma once
 
 #include "Element.h"
+#include "StyleInvalidator.h"
 #include <wtf/Vector.h>
 
 namespace WebCore {
 
-class RuleSet;
 class SpaceSplitString;
 
 namespace Style {
 
-struct InvalidationRuleSet;
-
 class ClassChangeInvalidation {
 public:
     ClassChangeInvalidation(Element&, const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses);
@@ -49,7 +47,7 @@
     const bool m_isEnabled;
     Element& m_element;
 
-    Vector<const InvalidationRuleSet*, 4> m_invalidationRuleSets;
+    Invalidator::MatchElementRuleSets m_matchElementRuleSets;
 };
 
 inline ClassChangeInvalidation::ClassChangeInvalidation(Element& element, const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses)

Modified: trunk/Source/WebCore/style/StyleInvalidator.cpp (253689 => 253690)


--- trunk/Source/WebCore/style/StyleInvalidator.cpp	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/StyleInvalidator.cpp	2019-12-18 17:22:37 UTC (rev 253690)
@@ -35,8 +35,11 @@
 #include "RuntimeEnabledFeatures.h"
 #include "SelectorFilter.h"
 #include "ShadowRoot.h"
+#include "StyleResolver.h"
 #include "StyleRuleImport.h"
+#include "StyleScopeRuleSets.h"
 #include "StyleSheetContents.h"
+#include <wtf/SetForScope.h>
 
 namespace WebCore {
 namespace Style {
@@ -81,7 +84,7 @@
 
 Invalidator::Invalidator(const Vector<StyleSheetContents*>& sheets, const MediaQueryEvaluator& mediaQueryEvaluator)
     : m_ownedRuleSet(makeUnique<RuleSet>())
-    , m_ruleSet(*m_ownedRuleSet)
+    , m_ruleSets({ m_ownedRuleSet.get() })
     , m_dirtiesAllStyle(shouldDirtyAllStyle(sheets))
 {
     if (m_dirtiesAllStyle)
@@ -92,9 +95,10 @@
         m_ownedRuleSet->addRulesFromSheet(*sheet, mediaQueryEvaluator);
 }
 
-Invalidator::Invalidator(const RuleSet& ruleSet)
-    : m_ruleSet(ruleSet)
+Invalidator::Invalidator(const Vector<const RuleSet*, 1>& ruleSets)
+    : m_ruleSets(ruleSets)
 {
+    ASSERT(m_ruleSets.size());
 }
 
 Invalidator::CheckDescendants Invalidator::invalidateIfNeeded(Element& element, const SelectorFilter* filter)
@@ -101,7 +105,14 @@
 {
     invalidateInShadowTreeIfNeeded(element);
 
-    bool shouldCheckForSlots = !m_ruleSet.slottedPseudoElementRules().isEmpty() && !m_didInvalidateHostChildren;
+    bool shouldCheckForSlots = [&] {
+        for (auto* ruleSet : m_ruleSets) {
+            if (!ruleSet->slottedPseudoElementRules().isEmpty() && !m_didInvalidateHostChildren)
+                return true;
+        }
+        return false;
+    }();
+
     if (shouldCheckForSlots && is<HTMLSlotElement>(element)) {
         auto* containingShadowRoot = element.containingShadowRoot();
         if (containingShadowRoot && containingShadowRoot->host()) {
@@ -114,11 +125,13 @@
 
     switch (element.styleValidity()) {
     case Style::Validity::Valid: {
-        ElementRuleCollector ruleCollector(element, m_ruleSet, filter);
-        ruleCollector.setMode(SelectorChecker::Mode::CollectingRulesIgnoringVirtualPseudoElements);
+        for (auto* ruleSet : m_ruleSets) {
+            ElementRuleCollector ruleCollector(element, *ruleSet, filter);
+            ruleCollector.setMode(SelectorChecker::Mode::CollectingRulesIgnoringVirtualPseudoElements);
 
-        if (ruleCollector.matchesAnyAuthorRules())
-            element.invalidateStyleInternal();
+            if (ruleCollector.matchesAnyAuthorRules())
+                element.invalidateStyleInternal();
+        }
 
         return CheckDescendants::Yes;
     }
@@ -187,8 +200,10 @@
 {
     ASSERT(!m_dirtiesAllStyle);
 
-    if (!m_ruleSet.hostPseudoClassRules().isEmpty() && shadowRoot.host())
-        shadowRoot.host()->invalidateStyleInternal();
+    for (auto* ruleSet : m_ruleSets) {
+        if (!ruleSet->hostPseudoClassRules().isEmpty() && shadowRoot.host())
+            shadowRoot.host()->invalidateStyleInternal();
+    }
 
     for (auto& child : childrenOfType<Element>(shadowRoot)) {
         SelectorFilter filter;
@@ -282,16 +297,35 @@
     if (!shadowRoot)
         return;
 
-    if (m_ruleSet.hasShadowPseudoElementRules()) {
-        // FIXME: This could do actual rule matching too.
-        element.invalidateStyleForSubtreeInternal();
+
+    for (auto* ruleSet : m_ruleSets) {
+        if (ruleSet->hasShadowPseudoElementRules()) {
+            // FIXME: This could do actual rule matching too.
+            element.invalidateStyleForSubtreeInternal();
+        }
+
+        // FIXME: More fine-grained invalidation for ::part()
+        if (!ruleSet->partPseudoElementRules().isEmpty())
+            invalidateShadowParts(*shadowRoot);
     }
+}
 
-    // FIXME: More fine-grained invalidation for ::part()
-    if (!m_ruleSet.partPseudoElementRules().isEmpty())
-        invalidateShadowParts(*shadowRoot);
+void Invalidator::addToMatchElementRuleSets(Invalidator::MatchElementRuleSets& matchElementRuleSets, const InvalidationRuleSet& invalidationRuleSet)
+{
+    matchElementRuleSets.ensure(invalidationRuleSet.matchElement, [] {
+        return Vector<const RuleSet*, 1> { };
+    }).iterator->value.append(invalidationRuleSet.ruleSet.get());
 }
 
+void Invalidator::invalidateWithMatchElementRuleSets(Element& element, const MatchElementRuleSets& matchElementRuleSets)
+{
+    SetForScope<bool> isInvalidating(element.styleResolver().ruleSets().isInvalidatingStyleWithRuleSets(), true);
 
+    for (auto& matchElementAndRuleSet : matchElementRuleSets) {
+        Invalidator invalidator(matchElementAndRuleSet.value);
+        invalidator.invalidateStyleWithMatchElement(element, matchElementAndRuleSet.key);
+    }
 }
+
 }
+}

Modified: trunk/Source/WebCore/style/StyleInvalidator.h (253689 => 253690)


--- trunk/Source/WebCore/style/StyleInvalidator.h	2019-12-18 17:01:24 UTC (rev 253689)
+++ trunk/Source/WebCore/style/StyleInvalidator.h	2019-12-18 17:22:37 UTC (rev 253690)
@@ -27,6 +27,7 @@
 
 #include "RuleFeature.h"
 #include <wtf/Forward.h>
+#include <wtf/HashMap.h>
 
 namespace WebCore {
 
@@ -40,20 +41,24 @@
 namespace Style {
 
 class RuleSet;
+struct InvalidationRuleSet;
 
 class Invalidator {
 public:
     Invalidator(const Vector<StyleSheetContents*>&, const MediaQueryEvaluator&);
-    Invalidator(const RuleSet&);
+    Invalidator(const Vector<const RuleSet*, 1>&);
 
     bool dirtiesAllStyle() const { return m_dirtiesAllStyle; }
     void invalidateStyle(Document&);
     void invalidateStyle(ShadowRoot&);
     void invalidateStyle(Element&);
-    void invalidateStyleWithMatchElement(Element&, MatchElement);
 
     static void invalidateShadowParts(ShadowRoot&);
 
+    using MatchElementRuleSets = HashMap<MatchElement, Vector<const RuleSet*, 1>, WTF::IntHash<MatchElement>, WTF::StrongEnumHashTraits<MatchElement>>;
+    static void addToMatchElementRuleSets(Invalidator::MatchElementRuleSets&, const InvalidationRuleSet&);
+    static void invalidateWithMatchElementRuleSets(Element&, const MatchElementRuleSets&);
+
 private:
     enum class CheckDescendants { Yes, No };
     CheckDescendants invalidateIfNeeded(Element&, const SelectorFilter*);
@@ -60,9 +65,10 @@
     void invalidateStyleForTree(Element&, SelectorFilter*);
     void invalidateStyleForDescendants(Element&, SelectorFilter*);
     void invalidateInShadowTreeIfNeeded(Element&);
+    void invalidateStyleWithMatchElement(Element&, MatchElement);
 
     std::unique_ptr<RuleSet> m_ownedRuleSet;
-    const RuleSet& m_ruleSet;
+    Vector<const RuleSet*, 1> m_ruleSets;
     bool m_dirtiesAllStyle { false };
     bool m_didInvalidateHostChildren { false };
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to