<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[287325] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/287325">287325</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2021-12-21 11:36:05 -0800 (Tue, 21 Dec 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>REGRESSION(<a href="http://trac.webkit.org/projects/webkit/changeset/286169">r286169</a>): 0.3% Speedometer regression
https://bugs.webkit.org/show_bug.cgi?id=234549

Reviewed by Alan Bujtas.

There is some additional work in ChildChangeInvalidation even when :has is not used.
Speedemater2 Vanilla-* subtests are very heavy on DOM mutations.

* style/ChildChangeInvalidation.cpp:
(WebCore::Style::ChildChangeInvalidation::invalidateForHasBeforeMutation):
(WebCore::Style::ChildChangeInvalidation::invalidateForHasAfterMutation):
(WebCore::Style::ChildChangeInvalidation::traverseRemovedElements):
(WebCore::Style::ChildChangeInvalidation::traverseAddedElements):
(WebCore::Style::ChildChangeInvalidation::ChildChangeInvalidation): Deleted.
(WebCore::Style::ChildChangeInvalidation::~ChildChangeInvalidation): Deleted.
(WebCore::Style::needsTraversal): Deleted.
* style/ChildChangeInvalidation.h:
(WebCore::Style::ChildChangeInvalidation::ChildChangeInvalidation):
(WebCore::Style::ChildChangeInvalidation::~ChildChangeInvalidation):

Inline the constructor and destructor.
Compute the :has invalidation status only once.

* style/RuleFeature.h:
(WebCore::Style::RuleFeatureSet::usesHasPseudoClass const):
* style/StyleScope.cpp:
(WebCore::Style::Scope::resolver):
(WebCore::Style::Scope::updateActiveStyleSheets):
* style/StyleScope.h:
(WebCore::Style::Scope::usesStyleBasedEditability const):
(WebCore::Style::Scope::usesHasPseudoClass const):

Add a Scope bit for this for faster testing.

(WebCore::Style::Scope::usesStyleBasedEditability): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorestyleChildChangeInvalidationcpp">trunk/Source/WebCore/style/ChildChangeInvalidation.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleChildChangeInvalidationh">trunk/Source/WebCore/style/ChildChangeInvalidation.h</a></li>
<li><a href="#trunkSourceWebCorestyleRuleFeatureh">trunk/Source/WebCore/style/RuleFeature.h</a></li>
<li><a href="#trunkSourceWebCorestyleStyleScopecpp">trunk/Source/WebCore/style/StyleScope.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleStyleScopeh">trunk/Source/WebCore/style/StyleScope.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/ChangeLog      2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2021-12-21  Antti Koivisto  <antti@apple.com>
+
+        REGRESSION(r286169): 0.3% Speedometer regression
+        https://bugs.webkit.org/show_bug.cgi?id=234549
+
+        Reviewed by Alan Bujtas.
+
+        There is some additional work in ChildChangeInvalidation even when :has is not used.
+        Speedemater2 Vanilla-* subtests are very heavy on DOM mutations.
+
+        * style/ChildChangeInvalidation.cpp:
+        (WebCore::Style::ChildChangeInvalidation::invalidateForHasBeforeMutation):
+        (WebCore::Style::ChildChangeInvalidation::invalidateForHasAfterMutation):
+        (WebCore::Style::ChildChangeInvalidation::traverseRemovedElements):
+        (WebCore::Style::ChildChangeInvalidation::traverseAddedElements):
+        (WebCore::Style::ChildChangeInvalidation::ChildChangeInvalidation): Deleted.
+        (WebCore::Style::ChildChangeInvalidation::~ChildChangeInvalidation): Deleted.
+        (WebCore::Style::needsTraversal): Deleted.
+        * style/ChildChangeInvalidation.h:
+        (WebCore::Style::ChildChangeInvalidation::ChildChangeInvalidation):
+        (WebCore::Style::ChildChangeInvalidation::~ChildChangeInvalidation):
+
+        Inline the constructor and destructor.
+        Compute the :has invalidation status only once.
+
+        * style/RuleFeature.h:
+        (WebCore::Style::RuleFeatureSet::usesHasPseudoClass const):
+        * style/StyleScope.cpp:
+        (WebCore::Style::Scope::resolver):
+        (WebCore::Style::Scope::updateActiveStyleSheets):
+        * style/StyleScope.h:
+        (WebCore::Style::Scope::usesStyleBasedEditability const):
+        (WebCore::Style::Scope::usesHasPseudoClass const):
+
+        Add a Scope bit for this for faster testing.
+
+        (WebCore::Style::Scope::usesStyleBasedEditability): Deleted.
+
</ins><span class="cx"> 2021-12-21  Sam Weinig  <weinig@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Gradient encode/decode does not validate that the serialized stops were sorted
</span></span></pre></div>
<a id="trunkSourceWebCorestyleChildChangeInvalidationcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/ChildChangeInvalidation.cpp (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/ChildChangeInvalidation.cpp   2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/style/ChildChangeInvalidation.cpp      2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -36,31 +36,6 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore::Style {
</span><span class="cx"> 
</span><del>-ChildChangeInvalidation::ChildChangeInvalidation(ContainerNode& container, const ContainerNode::ChildChange& childChange)
-    : m_parentElement(is<Element>(container) ? downcast<Element>(&container) : nullptr)
-    , m_isEnabled(m_parentElement ? m_parentElement->needsStyleInvalidation() : false)
-    , m_childChange(childChange)
-{
-    if (!m_isEnabled)
-        return;
-
-    traverseRemovedElements([&](auto& changedElement) {
-        invalidateForChangedElement(changedElement);
-    });
-}
-
-ChildChangeInvalidation::~ChildChangeInvalidation()
-{
-    if (!m_isEnabled)
-        return;
-
-    traverseAddedElements([&](auto& changedElement) {
-        invalidateForChangedElement(changedElement);
-    });
-    
-    invalidateAfterChange();
-}
-
</del><span class="cx"> void ChildChangeInvalidation::invalidateForChangedElement(Element& changedElement)
</span><span class="cx"> {
</span><span class="cx">     auto& ruleSets = parentElement().styleResolver().ruleSets();
</span><span class="lines">@@ -108,19 +83,30 @@
</span><span class="cx">     Invalidator::invalidateWithMatchElementRuleSets(changedElement, matchElementRuleSets);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static bool needsTraversal(const RuleFeatureSet& features, const ContainerNode::ChildChange& childChange)
</del><ins>+void ChildChangeInvalidation::invalidateForHasBeforeMutation()
</ins><span class="cx"> {
</span><del>-    if (features.usesMatchElement(MatchElement::HasChild))
-        return true;
-    if (features.usesMatchElement(MatchElement::HasDescendant))
-        return true;
-    if (features.usesMatchElement(MatchElement::HasSiblingDescendant))
-        return true;
-    if (features.usesMatchElement(MatchElement::HasNonSubject))
-        return true;
-    return features.usesMatchElement(MatchElement::HasSibling) && childChange.previousSiblingElement;
-};
</del><ins>+    ASSERT(m_needsHasInvalidation);
</ins><span class="cx"> 
</span><ins>+    if (m_childChange.isInsertion() && m_childChange.type != ContainerNode::ChildChange::Type::AllChildrenReplaced)
+        return;
+
+    traverseRemovedElements([&](auto& changedElement) {
+        invalidateForChangedElement(changedElement);
+    });
+}
+
+void ChildChangeInvalidation::invalidateForHasAfterMutation()
+{
+    ASSERT(m_needsHasInvalidation);
+
+    if (!m_childChange.isInsertion())
+        return;
+
+    traverseAddedElements([&](auto& changedElement) {
+        invalidateForChangedElement(changedElement);
+    });
+}
+
</ins><span class="cx"> static bool needsDescendantTraversal(const RuleFeatureSet& features)
</span><span class="cx"> {
</span><span class="cx">     if (features.usesMatchElement(MatchElement::HasNonSubject))
</span><span class="lines">@@ -131,13 +117,7 @@
</span><span class="cx"> template<typename Function>
</span><span class="cx"> void ChildChangeInvalidation::traverseRemovedElements(Function&& function)
</span><span class="cx"> {
</span><del>-    if (m_childChange.isInsertion() && m_childChange.type != ContainerNode::ChildChange::Type::AllChildrenReplaced)
-        return;
-
</del><span class="cx">     auto& features = parentElement().styleResolver().ruleSets().features();
</span><del>-    if (!needsTraversal(features, m_childChange))
-        return;
-
</del><span class="cx">     bool needsDescendantTraversal = Style::needsDescendantTraversal(features);
</span><span class="cx"> 
</span><span class="cx">     auto* toRemove = m_childChange.previousSiblingElement ? m_childChange.previousSiblingElement->nextElementSibling() : parentElement().firstElementChild();
</span><span class="lines">@@ -155,9 +135,6 @@
</span><span class="cx"> template<typename Function>
</span><span class="cx"> void ChildChangeInvalidation::traverseAddedElements(Function&& function)
</span><span class="cx"> {
</span><del>-    if (!m_childChange.isInsertion())
-        return;
-
</del><span class="cx">     auto* newElement = [&] {
</span><span class="cx">         auto* previous = m_childChange.previousSiblingElement;
</span><span class="cx">         auto* candidate = previous ? ElementTraversal::nextSibling(*previous) : ElementTraversal::firstChild(parentElement());
</span><span class="lines">@@ -169,12 +146,9 @@
</span><span class="cx">     if (!newElement)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    auto& features = parentElement().styleResolver().ruleSets().features();
-    if (!needsTraversal(features, m_childChange))
-        return;
-
</del><span class="cx">     function(*newElement);
</span><span class="cx"> 
</span><ins>+    auto& features = parentElement().styleResolver().ruleSets().features();
</ins><span class="cx">     if (!needsDescendantTraversal(features))
</span><span class="cx">         return;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorestyleChildChangeInvalidationh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/ChildChangeInvalidation.h (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/ChildChangeInvalidation.h     2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/style/ChildChangeInvalidation.h        2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include "Element.h"
</span><span class="cx"> #include "StyleInvalidator.h"
</span><ins>+#include "StyleScope.h"
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> namespace Style {
</span><span class="lines">@@ -39,6 +40,8 @@
</span><span class="cx">     static void invalidateAfterFinishedParsingChildren(Element&);
</span><span class="cx"> 
</span><span class="cx"> private:
</span><ins>+    void invalidateForHasBeforeMutation();
+    void invalidateForHasAfterMutation();
</ins><span class="cx">     void invalidateAfterChange();
</span><span class="cx">     void checkForSiblingStyleChanges();
</span><span class="cx">     void invalidateForChangedElement(Element&);
</span><span class="lines">@@ -49,10 +52,35 @@
</span><span class="cx">     Element& parentElement() { return *m_parentElement; }
</span><span class="cx"> 
</span><span class="cx">     Element* m_parentElement { nullptr };
</span><ins>+    const ContainerNode::ChildChange& m_childChange;
+
</ins><span class="cx">     const bool m_isEnabled;
</span><del>-
-    const ContainerNode::ChildChange& m_childChange;
</del><ins>+    const bool m_needsHasInvalidation;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><ins>+inline ChildChangeInvalidation::ChildChangeInvalidation(ContainerNode& container, const ContainerNode::ChildChange& childChange)
+    : m_parentElement(is<Element>(container) ? downcast<Element>(&container) : nullptr)
+    , m_childChange(childChange)
+    , m_isEnabled(m_parentElement ? m_parentElement->needsStyleInvalidation() : false)
+    , m_needsHasInvalidation(m_isEnabled && Scope::forNode(*m_parentElement).usesHasPseudoClass())
+{
+    if (!m_isEnabled)
+        return;
+
+    if (m_needsHasInvalidation)
+        invalidateForHasBeforeMutation();
</ins><span class="cx"> }
</span><ins>+
+inline ChildChangeInvalidation::~ChildChangeInvalidation()
+{
+    if (!m_isEnabled)
+        return;
+
+    if (m_needsHasInvalidation)
+        invalidateForHasAfterMutation();
+
+    invalidateAfterChange();
</ins><span class="cx"> }
</span><ins>+
+}
+}
</ins></span></pre></div>
<a id="trunkSourceWebCorestyleRuleFeatureh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/RuleFeature.h (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/RuleFeature.h 2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/style/RuleFeature.h    2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -85,6 +85,7 @@
</span><span class="cx">     void collectFeatures(const RuleData&);
</span><span class="cx">     void registerContentAttribute(const AtomString&);
</span><span class="cx"> 
</span><ins>+    bool usesHasPseudoClass() const;
</ins><span class="cx">     bool usesMatchElement(MatchElement matchElement) const  { return usedMatchElements[static_cast<uint8_t>(matchElement)]; }
</span><span class="cx">     void setUsesMatchElement(MatchElement matchElement) { usedMatchElements[static_cast<uint8_t>(matchElement)] = true; }
</span><span class="cx"> 
</span><span class="lines">@@ -135,5 +136,14 @@
</span><span class="cx">     return static_cast<InvalidationKeyType>(std::get<1>(key)) == InvalidationKeyType::Universal;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline bool RuleFeatureSet::usesHasPseudoClass() const
+{
+    return usesMatchElement(MatchElement::HasChild)
+        || usesMatchElement(MatchElement::HasDescendant)
+        || usesMatchElement(MatchElement::HasSiblingDescendant)
+        || usesMatchElement(MatchElement::HasSibling)
+        || usesMatchElement(MatchElement::HasNonSubject);
+}
+
</ins><span class="cx"> } // namespace Style
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorestyleStyleScopecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/StyleScope.cpp (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/StyleScope.cpp        2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/style/StyleScope.cpp   2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -85,6 +85,9 @@
</span><span class="cx">             createOrFindSharedShadowTreeResolver();
</span><span class="cx">         else
</span><span class="cx">             createDocumentResolver();
</span><ins>+        
+        if (m_resolver->ruleSets().features().usesHasPseudoClass())
+            m_usesHasPseudoClass = true;
</ins><span class="cx">     }
</span><span class="cx">     return *m_resolver;
</span><span class="cx"> }
</span><span class="lines">@@ -506,6 +509,9 @@
</span><span class="cx">             m_usesStyleBasedEditability = true;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    if (m_resolver && m_resolver->ruleSets().features().usesHasPseudoClass())
+        m_usesHasPseudoClass = true;
+
</ins><span class="cx">     invalidateStyleAfterStyleSheetChange(styleSheetChange);
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorestyleStyleScopeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/StyleScope.h (287324 => 287325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/StyleScope.h  2021-12-21 19:15:59 UTC (rev 287324)
+++ trunk/Source/WebCore/style/StyleScope.h     2021-12-21 19:36:05 UTC (rev 287325)
</span><span class="lines">@@ -88,7 +88,8 @@
</span><span class="cx">     bool hasPendingSheetInBody(const Element&) const;
</span><span class="cx">     bool hasPendingSheet(const ProcessingInstruction&) const;
</span><span class="cx"> 
</span><del>-    bool usesStyleBasedEditability() { return m_usesStyleBasedEditability; }
</del><ins>+    bool usesStyleBasedEditability() const { return m_usesStyleBasedEditability; }
+    bool usesHasPseudoClass() const { return m_usesHasPseudoClass; }
</ins><span class="cx"> 
</span><span class="cx">     bool activeStyleSheetsContains(const CSSStyleSheet*) const;
</span><span class="cx"> 
</span><span class="lines">@@ -196,6 +197,7 @@
</span><span class="cx"> 
</span><span class="cx">     bool m_hasDescendantWithPendingUpdate { false };
</span><span class="cx">     bool m_usesStyleBasedEditability { false };
</span><ins>+    bool m_usesHasPseudoClass { false };
</ins><span class="cx">     bool m_isUpdatingStyleResolver { false };
</span><span class="cx"> 
</span><span class="cx">     std::optional<MediaQueryViewportState> m_viewportStateOnPreviousMediaQueryEvaluation;
</span></span></pre>
</div>
</div>

</body>
</html>