<!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>[199438] releases/WebKitGTK/webkit-2.12/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/199438">199438</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2016-04-13 02:13:26 -0700 (Wed, 13 Apr 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Merge <a href="http://trac.webkit.org/projects/webkit/changeset/198584">r198584</a> - Share style by sharing RenderStyle substructures not the object itself
https://bugs.webkit.org/show_bug.cgi?id=155787

Reviewed by Anreas Kling.

The current approach where we share RenderStyle objects between elements leads to lot of awkward and bug-prone code.
Most of the RenderStyle consists of shareable substructures. It is better to just share those.

With this patch we create shared styles with RenderStyle::clone(). Sharing is traced as state in Style::SharingResolver
instead of relying on RenderStyle equality to locate potential sharing cousins.

* rendering/style/StyleRareNonInheritedData.cpp:
(WebCore::StyleRareNonInheritedData::operator==):

    m_altText was missing from operator==
    This was exposed by TreeResolver::resolveElement change, tested by fast/css/alt-inherit-initial.html

* style/StyleSharingResolver.cpp:
(WebCore::Style::elementHasDirectionAuto):
(WebCore::Style::SharingResolver::resolve):

    Save share results to a map.

(WebCore::Style::SharingResolver::findSibling):
(WebCore::Style::SharingResolver::locateCousinList):

    Instead of traversing we can now just do a hash lookup to locate a candidate cousin list.
    There is no need for recursion anymore, the map covers sharing beyond immediate siblings too.
    Remove most tests here as they have been already covered when sharing occured.

(WebCore::Style::canShareStyleWithControl):
* style/StyleSharingResolver.h:
* style/StyleTreeResolver.cpp:
(WebCore::Style::TreeResolver::styleForElement):
(WebCore::Style::TreeResolver::resolveElement):

    No need to do forced setting anymore just to support style sharing.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#releasesWebKitGTKwebkit212SourceWebCoreChangeLog">releases/WebKitGTK/webkit-2.12/Source/WebCore/ChangeLog</a></li>
<li><a href="#releasesWebKitGTKwebkit212SourceWebCorerenderingstyleStyleRareNonInheritedDatacpp">releases/WebKitGTK/webkit-2.12/Source/WebCore/rendering/style/StyleRareNonInheritedData.cpp</a></li>
<li><a href="#releasesWebKitGTKwebkit212SourceWebCorestyleStyleSharingResolvercpp">releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.cpp</a></li>
<li><a href="#releasesWebKitGTKwebkit212SourceWebCorestyleStyleSharingResolverh">releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.h</a></li>
<li><a href="#releasesWebKitGTKwebkit212SourceWebCorestyleStyleTreeResolvercpp">releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleTreeResolver.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="releasesWebKitGTKwebkit212SourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: releases/WebKitGTK/webkit-2.12/Source/WebCore/ChangeLog (199437 => 199438)</h4>
<pre class="diff"><span>
<span class="info">--- releases/WebKitGTK/webkit-2.12/Source/WebCore/ChangeLog        2016-04-13 09:11:57 UTC (rev 199437)
+++ releases/WebKitGTK/webkit-2.12/Source/WebCore/ChangeLog        2016-04-13 09:13:26 UTC (rev 199438)
</span><span class="lines">@@ -1,3 +1,43 @@
</span><ins>+2016-03-23  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Share style by sharing RenderStyle substructures not the object itself
+        https://bugs.webkit.org/show_bug.cgi?id=155787
+
+        Reviewed by Anreas Kling.
+
+        The current approach where we share RenderStyle objects between elements leads to lot of awkward and bug-prone code.
+        Most of the RenderStyle consists of shareable substructures. It is better to just share those.
+
+        With this patch we create shared styles with RenderStyle::clone(). Sharing is traced as state in Style::SharingResolver
+        instead of relying on RenderStyle equality to locate potential sharing cousins.
+
+        * rendering/style/StyleRareNonInheritedData.cpp:
+        (WebCore::StyleRareNonInheritedData::operator==):
+
+            m_altText was missing from operator==
+            This was exposed by TreeResolver::resolveElement change, tested by fast/css/alt-inherit-initial.html
+
+        * style/StyleSharingResolver.cpp:
+        (WebCore::Style::elementHasDirectionAuto):
+        (WebCore::Style::SharingResolver::resolve):
+
+            Save share results to a map.
+
+        (WebCore::Style::SharingResolver::findSibling):
+        (WebCore::Style::SharingResolver::locateCousinList):
+
+            Instead of traversing we can now just do a hash lookup to locate a candidate cousin list.
+            There is no need for recursion anymore, the map covers sharing beyond immediate siblings too.
+            Remove most tests here as they have been already covered when sharing occured.
+
+        (WebCore::Style::canShareStyleWithControl):
+        * style/StyleSharingResolver.h:
+        * style/StyleTreeResolver.cpp:
+        (WebCore::Style::TreeResolver::styleForElement):
+        (WebCore::Style::TreeResolver::resolveElement):
+
+            No need to do forced setting anymore just to support style sharing.
+
</ins><span class="cx"> 2016-03-22  Darin Adler  &lt;darin@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         showModalDialog code runs with “first window” set to wrong window
</span></span></pre></div>
<a id="releasesWebKitGTKwebkit212SourceWebCorerenderingstyleStyleRareNonInheritedDatacpp"></a>
<div class="modfile"><h4>Modified: releases/WebKitGTK/webkit-2.12/Source/WebCore/rendering/style/StyleRareNonInheritedData.cpp (199437 => 199438)</h4>
<pre class="diff"><span>
<span class="info">--- releases/WebKitGTK/webkit-2.12/Source/WebCore/rendering/style/StyleRareNonInheritedData.cpp        2016-04-13 09:11:57 UTC (rev 199437)
+++ releases/WebKitGTK/webkit-2.12/Source/WebCore/rendering/style/StyleRareNonInheritedData.cpp        2016-04-13 09:13:26 UTC (rev 199438)
</span><span class="lines">@@ -242,6 +242,7 @@
</span><span class="cx"> #endif
</span><span class="cx">         &amp;&amp; contentDataEquivalent(o)
</span><span class="cx">         &amp;&amp; arePointingToEqualData(m_counterDirectives, o.m_counterDirectives)
</span><ins>+        &amp;&amp; m_altText == o.m_altText
</ins><span class="cx">         &amp;&amp; arePointingToEqualData(m_boxShadow, o.m_boxShadow)
</span><span class="cx">         &amp;&amp; arePointingToEqualData(m_willChange, o.m_willChange)
</span><span class="cx">         &amp;&amp; arePointingToEqualData(m_boxReflect, o.m_boxReflect)
</span></span></pre></div>
<a id="releasesWebKitGTKwebkit212SourceWebCorestyleStyleSharingResolvercpp"></a>
<div class="modfile"><h4>Modified: releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.cpp (199437 => 199438)</h4>
<pre class="diff"><span>
<span class="info">--- releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.cpp        2016-04-13 09:11:57 UTC (rev 199437)
+++ releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.cpp        2016-04-13 09:13:26 UTC (rev 199438)
</span><span class="lines">@@ -42,7 +42,6 @@
</span><span class="cx"> namespace Style {
</span><span class="cx"> 
</span><span class="cx"> static const unsigned cStyleSearchThreshold = 10;
</span><del>-static const unsigned cStyleSearchLevelThreshold = 10;
</del><span class="cx"> 
</span><span class="cx"> struct SharingResolver::Context {
</span><span class="cx">     const StyledElement&amp; element;
</span><span class="lines">@@ -68,7 +67,7 @@
</span><span class="cx">     return is&lt;HTMLElement&gt;(element) &amp;&amp; downcast&lt;HTMLElement&gt;(element).hasDirectionAuto();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-const Element* SharingResolver::resolve(const Element&amp; searchElement) const
</del><ins>+RefPtr&lt;RenderStyle&gt; SharingResolver::resolve(const Element&amp; searchElement)
</ins><span class="cx"> {
</span><span class="cx">     if (!is&lt;StyledElement&gt;(searchElement))
</span><span class="cx">         return nullptr;
</span><span class="lines">@@ -101,14 +100,13 @@
</span><span class="cx"> 
</span><span class="cx">     // Check previous siblings and their cousins.
</span><span class="cx">     unsigned count = 0;
</span><del>-    unsigned visitedNodeCount = 0;
</del><span class="cx">     StyledElement* shareElement = nullptr;
</span><span class="cx">     Node* cousinList = element.previousSibling();
</span><span class="cx">     while (cousinList) {
</span><span class="cx">         shareElement = findSibling(context, cousinList, count);
</span><span class="cx">         if (shareElement)
</span><span class="cx">             break;
</span><del>-        cousinList = locateCousinList(cousinList-&gt;parentElement(), visitedNodeCount);
</del><ins>+        cousinList = locateCousinList(cousinList-&gt;parentElement());
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // If we have exhausted all our budget or our cousins.
</span><span class="lines">@@ -125,7 +123,9 @@
</span><span class="cx">     if (parentElementPreventsSharing(parentElement))
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><del>-    return shareElement;
</del><ins>+    m_elementsSharingStyle.add(&amp;element, shareElement);
+
+    return RenderStyle::clone(shareElement-&gt;renderStyle());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> StyledElement* SharingResolver::findSibling(const Context&amp; context, Node* node, unsigned&amp; count) const
</span><span class="lines">@@ -141,47 +141,18 @@
</span><span class="cx">     return downcast&lt;StyledElement&gt;(node);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-Node* SharingResolver::locateCousinList(Element* parent, unsigned&amp; visitedNodeCount) const
</del><ins>+Node* SharingResolver::locateCousinList(const Element* parent) const
</ins><span class="cx"> {
</span><del>-    if (visitedNodeCount &gt;= cStyleSearchThreshold * cStyleSearchLevelThreshold)
-        return nullptr;
-    if (!is&lt;StyledElement&gt;(parent))
-        return nullptr;
-    auto&amp; styledParent = downcast&lt;StyledElement&gt;(*parent);
-    if (styledParent.inlineStyle())
-        return nullptr;
-    if (is&lt;SVGElement&gt;(styledParent) &amp;&amp; downcast&lt;SVGElement&gt;(styledParent).animatedSMILStyleProperties())
-        return nullptr;
-    if (styledParent.hasID() &amp;&amp; m_ruleSets.features().idsInRules.contains(styledParent.idForStyleResolution().impl()))
-        return nullptr;
-
-    RenderStyle* parentStyle = styledParent.renderStyle();
-    unsigned subcount = 0;
-    Node* thisCousin = &amp;styledParent;
-    Node* currentNode = styledParent.previousSibling();
-
-    // Reserve the tries for this level. This effectively makes sure that the algorithm
-    // will never go deeper than cStyleSearchLevelThreshold levels into recursion.
-    visitedNodeCount += cStyleSearchThreshold;
-    while (thisCousin) {
-        for (; currentNode; currentNode = currentNode-&gt;previousSibling()) {
-            if (++subcount &gt; cStyleSearchThreshold)
-                return nullptr;
-            if (!is&lt;Element&gt;(*currentNode))
-                continue;
-            auto&amp; currentElement = downcast&lt;Element&gt;(*currentNode);
-            if (currentElement.renderStyle() != parentStyle)
-                continue;
-            if (!currentElement.lastChild())
-                continue;
-            if (!parentElementPreventsSharing(currentElement)) {
-                // Adjust for unused reserved tries.
-                visitedNodeCount -= cStyleSearchThreshold - subcount;
-                return currentNode-&gt;lastChild();
-            }
</del><ins>+    const unsigned maximumSearchCount = 10;
+    for (unsigned count = 0; count &lt; maximumSearchCount; ++count) {
+        auto* elementSharingParentStyle = m_elementsSharingStyle.get(parent);
+        if (!elementSharingParentStyle)
+            return nullptr;
+        if (!parentElementPreventsSharing(*elementSharingParentStyle)) {
+            if (auto* cousin = elementSharingParentStyle-&gt;lastChild())
+                return cousin;
</ins><span class="cx">         }
</span><del>-        currentNode = locateCousinList(thisCousin-&gt;parentElement(), visitedNodeCount);
-        thisCousin = currentNode;
</del><ins>+        parent = elementSharingParentStyle;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     return nullptr;
</span></span></pre></div>
<a id="releasesWebKitGTKwebkit212SourceWebCorestyleStyleSharingResolverh"></a>
<div class="modfile"><h4>Modified: releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.h (199437 => 199438)</h4>
<pre class="diff"><span>
<span class="info">--- releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.h        2016-04-13 09:11:57 UTC (rev 199437)
+++ releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleSharingResolver.h        2016-04-13 09:13:26 UTC (rev 199438)
</span><span class="lines">@@ -26,12 +26,15 @@
</span><span class="cx"> #ifndef StyleSharingResolver_h
</span><span class="cx"> #define StyleSharingResolver_h
</span><span class="cx"> 
</span><ins>+#include &lt;wtf/HashMap.h&gt;
+
</ins><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class Document;
</span><span class="cx"> class DocumentRuleSets;
</span><span class="cx"> class Element;
</span><span class="cx"> class Node;
</span><ins>+class RenderStyle;
</ins><span class="cx"> class RuleSet;
</span><span class="cx"> class SelectorFilter;
</span><span class="cx"> class SpaceSplitString;
</span><span class="lines">@@ -43,13 +46,13 @@
</span><span class="cx"> public:
</span><span class="cx">     SharingResolver(const Document&amp;, const DocumentRuleSets&amp;, const SelectorFilter&amp;);
</span><span class="cx"> 
</span><del>-    const Element* resolve(const Element&amp;) const;
</del><ins>+    RefPtr&lt;RenderStyle&gt; resolve(const Element&amp;);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     struct Context;
</span><span class="cx"> 
</span><span class="cx">     StyledElement* findSibling(const Context&amp;, Node*, unsigned&amp; count) const;
</span><del>-    Node* locateCousinList(Element* parent, unsigned&amp; visitedNodeCount) const;
</del><ins>+    Node* locateCousinList(const Element* parent) const;
</ins><span class="cx">     bool canShareStyleWithElement(const Context&amp;, const StyledElement&amp; candidateElement) const;
</span><span class="cx">     bool styleSharingCandidateMatchesRuleSet(const StyledElement&amp;, const RuleSet*) const;
</span><span class="cx">     bool sharingCandidateHasIdenticalStyleAffectingAttributes(const Context&amp;, const StyledElement&amp; sharingCandidate) const;
</span><span class="lines">@@ -58,6 +61,8 @@
</span><span class="cx">     const Document&amp; m_document;
</span><span class="cx">     const DocumentRuleSets&amp; m_ruleSets;
</span><span class="cx">     const SelectorFilter&amp; m_selectorFilter;
</span><ins>+
+    HashMap&lt;const Element*, const Element*&gt; m_elementsSharingStyle;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="releasesWebKitGTKwebkit212SourceWebCorestyleStyleTreeResolvercpp"></a>
<div class="modfile"><h4>Modified: releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleTreeResolver.cpp (199437 => 199438)</h4>
<pre class="diff"><span>
<span class="info">--- releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleTreeResolver.cpp        2016-04-13 09:11:57 UTC (rev 199437)
+++ releases/WebKitGTK/webkit-2.12/Source/WebCore/style/StyleTreeResolver.cpp        2016-04-13 09:13:26 UTC (rev 199438)
</span><span class="lines">@@ -183,8 +183,8 @@
</span><span class="cx">             return style.releaseNonNull();
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (auto* sharingElement = scope().sharingResolver.resolve(element))
-        return *sharingElement-&gt;renderStyle();
</del><ins>+    if (auto style = scope().sharingResolver.resolve(element))
+        return *style;
</ins><span class="cx"> 
</span><span class="cx">     return scope().styleResolver.styleForElement(element, &amp;inheritedStyle, MatchAllRules, nullptr, &amp;scope().selectorFilter);
</span><span class="cx"> }
</span><span class="lines">@@ -691,11 +691,6 @@
</span><span class="cx">     if (RenderElement* renderer = current.renderer()) {
</span><span class="cx">         if (localChange != NoChange || pseudoStyleCacheIsInvalid(renderer, newStyle.get()) || (parent().change == Force &amp;&amp; renderer-&gt;requiresForcedStyleRecalcPropagation()) || current.styleChangeType() == SyntheticStyleChange)
</span><span class="cx">             renderer-&gt;setAnimatableStyle(*newStyle, current.styleChangeType() == SyntheticStyleChange ? StyleDifferenceRecompositeLayer : StyleDifferenceEqual);
</span><del>-        else if (current.needsStyleRecalc()) {
-            // Although no change occurred, we use the new style so that the cousin style sharing code won't get
-            // fooled into believing this style is the same.
-            renderer-&gt;setStyleInternal(*newStyle);
-        }
</del><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // If &quot;rem&quot; units are used anywhere in the document, and if the document element's font size changes, then force font updating
</span></span></pre>
</div>
</div>

</body>
</html>