<!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>[207286] 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/207286">207286</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2016-10-13 04:59:19 -0700 (Thu, 13 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Share inline stylesheets between shadow trees
https://bugs.webkit.org/show_bug.cgi?id=163353

Reviewed by Ryosuke Niwa and Andreas Kling.

If shadow trees have identical inline stylesheets the data structures can be shared.
In future this will also allow sharing style resolvers.

* css/parser/CSSParserMode.h:
(WebCore::CSSParserContextHash::hash):
(WebCore::CSSParserContextHash::equal):
(WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::constructDeletedValue):
(WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::isDeletedValue):
(WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::emptyValue):

    Make CSSParserContext hashable.

* dom/InlineStyleSheetOwner.cpp:
(WebCore::makeInlineStyleSheetCacheKey):
(WebCore::inlineStyleSheetCache):

    Implement a simple cache for sharing stylesheets with identical text and context.

(WebCore::InlineStyleSheetOwner::createSheet):
(WebCore::InlineStyleSheetOwner::clearCache):
* dom/InlineStyleSheetOwner.h:
* platform/MemoryPressureHandler.cpp:
(WebCore::MemoryPressureHandler::releaseNoncriticalMemory):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssparserCSSParserModeh">trunk/Source/WebCore/css/parser/CSSParserMode.h</a></li>
<li><a href="#trunkSourceWebCoredomInlineStyleSheetOwnercpp">trunk/Source/WebCore/dom/InlineStyleSheetOwner.cpp</a></li>
<li><a href="#trunkSourceWebCoredomInlineStyleSheetOwnerh">trunk/Source/WebCore/dom/InlineStyleSheetOwner.h</a></li>
<li><a href="#trunkSourceWebCoreplatformMemoryPressureHandlercpp">trunk/Source/WebCore/platform/MemoryPressureHandler.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (207285 => 207286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-10-13 11:58:07 UTC (rev 207285)
+++ trunk/Source/WebCore/ChangeLog        2016-10-13 11:59:19 UTC (rev 207286)
</span><span class="lines">@@ -1,5 +1,36 @@
</span><span class="cx"> 2016-10-13  Antti Koivisto  &lt;antti@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Share inline stylesheets between shadow trees
+        https://bugs.webkit.org/show_bug.cgi?id=163353
+
+        Reviewed by Ryosuke Niwa and Andreas Kling.
+
+        If shadow trees have identical inline stylesheets the data structures can be shared.
+        In future this will also allow sharing style resolvers.
+
+        * css/parser/CSSParserMode.h:
+        (WebCore::CSSParserContextHash::hash):
+        (WebCore::CSSParserContextHash::equal):
+        (WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::constructDeletedValue):
+        (WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::isDeletedValue):
+        (WTF::HashTraits&lt;WebCore::CSSParserContext&gt;::emptyValue):
+
+            Make CSSParserContext hashable.
+
+        * dom/InlineStyleSheetOwner.cpp:
+        (WebCore::makeInlineStyleSheetCacheKey):
+        (WebCore::inlineStyleSheetCache):
+
+            Implement a simple cache for sharing stylesheets with identical text and context.
+
+        (WebCore::InlineStyleSheetOwner::createSheet):
+        (WebCore::InlineStyleSheetOwner::clearCache):
+        * dom/InlineStyleSheetOwner.h:
+        * platform/MemoryPressureHandler.cpp:
+        (WebCore::MemoryPressureHandler::releaseNoncriticalMemory):
+
+2016-10-13  Antti Koivisto  &lt;antti@apple.com&gt;
+
</ins><span class="cx">         Revert patch landed with wrong commit message.
</span><span class="cx"> 
</span><span class="cx"> 2016-10-12  Per Arne Vollan  &lt;pvollan@apple.com&gt;
</span></span></pre></div>
<a id="trunkSourceWebCorecssparserCSSParserModeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/parser/CSSParserMode.h (207285 => 207286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/parser/CSSParserMode.h        2016-10-13 11:58:07 UTC (rev 207285)
+++ trunk/Source/WebCore/css/parser/CSSParserMode.h        2016-10-13 11:59:19 UTC (rev 207286)
</span><span class="lines">@@ -32,6 +32,9 @@
</span><span class="cx"> #define CSSParserMode_h
</span><span class="cx"> 
</span><span class="cx"> #include &quot;URL.h&quot;
</span><ins>+#include &quot;URLHash.h&quot;
+#include &lt;wtf/HashFunctions.h&gt;
+#include &lt;wtf/text/StringHash.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -114,6 +117,50 @@
</span><span class="cx"> 
</span><span class="cx"> WEBCORE_EXPORT const CSSParserContext&amp; strictCSSParserContext();
</span><span class="cx"> 
</span><ins>+struct CSSParserContextHash {
+    static unsigned hash(const CSSParserContext&amp; key)
+    {
+        auto hash = URLHash::hash(key.baseURL);
+        hash ^= StringHash::hash(key.charset);
+        unsigned bits = key.isHTMLDocument                  &lt;&lt; 0
+            &amp; key.isHTMLDocument                            &lt;&lt; 1
+#if ENABLE(CSS_GRID_LAYOUT)
+            &amp; key.cssGridLayoutEnabled                      &lt;&lt; 2
+#endif
+#if ENABLE(TEXT_AUTOSIZING)
+            &amp; key.textAutosizingEnabled                     &lt;&lt; 3
+#endif
+            &amp; key.needsSiteSpecificQuirks                   &lt;&lt; 4
+            &amp; key.enforcesCSSMIMETypeInNoQuirksMode         &lt;&lt; 5
+            &amp; key.useLegacyBackgroundSizeShorthandBehavior  &lt;&lt; 6
+            &amp; key.springTimingFunctionEnabled               &lt;&lt; 7
+            &amp; key.useNewParser                              &lt;&lt; 8
+#if ENABLE(VARIATION_FONTS)
+            &amp; key.variationFontsEnabled                     &lt;&lt; 9
+#endif
+            &amp; key.mode                                      &lt;&lt; 10;
+        hash ^= WTF::intHash(bits);
+        return hash;
+    }
+    static bool equal(const CSSParserContext&amp; a, const CSSParserContext&amp; b)
+    {
+        return a == b;
+    }
+    static const bool safeToCompareToEmptyOrDeleted = false;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><ins>+}
+
+namespace WTF {
+template&lt;&gt; struct HashTraits&lt;WebCore::CSSParserContext&gt; : GenericHashTraits&lt;WebCore::CSSParserContext&gt; {
+    static void constructDeletedValue(WebCore::CSSParserContext&amp; slot) { new (NotNull, &amp;slot.baseURL) WebCore::URL(WTF::HashTableDeletedValue); }
+    static bool isDeletedValue(const WebCore::CSSParserContext&amp; value) { return value.baseURL.isHashTableDeletedValue(); }
+    static WebCore::CSSParserContext emptyValue() { return WebCore::CSSParserContext(WebCore::HTMLStandardMode); }
+};
+
+template&lt;&gt; struct DefaultHash&lt;WebCore::CSSParserContext&gt; {
+    typedef WebCore::CSSParserContextHash Hash;
+};
+}
+
</ins><span class="cx"> #endif // CSSParserMode_h
</span></span></pre></div>
<a id="trunkSourceWebCoredomInlineStyleSheetOwnercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/InlineStyleSheetOwner.cpp (207285 => 207286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/InlineStyleSheetOwner.cpp        2016-10-13 11:58:07 UTC (rev 207285)
+++ trunk/Source/WebCore/dom/InlineStyleSheetOwner.cpp        2016-10-13 11:59:19 UTC (rev 207286)
</span><span class="lines">@@ -30,10 +30,40 @@
</span><span class="cx"> #include &quot;StyleScope.h&quot;
</span><span class="cx"> #include &quot;StyleSheetContents.h&quot;
</span><span class="cx"> #include &quot;TextNodeTraversal.h&quot;
</span><ins>+#include &lt;wtf/HashMap.h&gt;
</ins><span class="cx"> #include &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+using InlineStyleSheetCacheKey = std::pair&lt;String, CSSParserContext&gt;;
+using InlineStyleSheetCache = HashMap&lt;InlineStyleSheetCacheKey, RefPtr&lt;StyleSheetContents&gt;&gt;;
+
+static InlineStyleSheetCache&amp; inlineStyleSheetCache()
+{
+    static NeverDestroyed&lt;InlineStyleSheetCache&gt; cache;
+    return cache;
+}
+
+static URL inlineStyleSheetBaseURLForElement(const Element&amp; element)
+{
+    auto* shadowRoot = element.containingShadowRoot();
+    // User agent shadow trees can't contain document-relative URLs. Use blank URL as base allowing cross-document sharing.
+    return shadowRoot &amp;&amp; shadowRoot-&gt;mode() == ShadowRoot::Mode::UserAgent ? blankURL() : element.document().baseURL();
+}
+
+static Optional&lt;InlineStyleSheetCacheKey&gt; makeInlineStyleSheetCacheKey(const String&amp; text, const Element&amp; element)
+{
+    if (text.isEmpty())
+        return { };
+    // Only cache for shadow trees. Main document inline stylesheets are generally unique and can't be shared between documents.
+    // FIXME: This could be relaxed when a stylesheet does not contain document-relative URLs (or #urls).
+    if (!element.isInShadowTree())
+        return { };
+
+    CSSParserContext context { element.document(), inlineStyleSheetBaseURLForElement(element) };
+    return std::make_pair(text, context);
+}
+
</ins><span class="cx"> InlineStyleSheetOwner::InlineStyleSheetOwner(Document&amp; document, bool createdByParser)
</span><span class="cx">     : m_isParsingChildren(createdByParser)
</span><span class="cx">     , m_loading(false)
</span><span class="lines">@@ -152,9 +182,20 @@
</span><span class="cx">     if (m_styleScope)
</span><span class="cx">         m_styleScope-&gt;addPendingSheet();
</span><span class="cx"> 
</span><ins>+    auto cacheKey = makeInlineStyleSheetCacheKey(text, element);
+    if (cacheKey) {
+        if (auto* cachedSheet = inlineStyleSheetCache().get(*cacheKey)) {
+            ASSERT(cachedSheet-&gt;isCacheable());
+            m_sheet = CSSStyleSheet::create(*cachedSheet, element);
+            sheetLoaded(element);
+            element.notifyLoadedSheetAndAllCriticalSubresources(false);
+            return;
+        }
+    }
+
</ins><span class="cx">     m_loading = true;
</span><span class="cx"> 
</span><del>-    m_sheet = CSSStyleSheet::createInline(element, URL(), m_startTextPosition, document.encoding());
</del><ins>+    m_sheet = CSSStyleSheet::createInline(element, inlineStyleSheetBaseURLForElement(element), m_startTextPosition, document.encoding());
</ins><span class="cx">     m_sheet-&gt;setMediaQueries(mediaQueries.releaseNonNull());
</span><span class="cx">     m_sheet-&gt;setTitle(element.title());
</span><span class="cx">     m_sheet-&gt;contents().parseStringAtPosition(text, m_startTextPosition, m_isParsingChildren);
</span><span class="lines">@@ -163,6 +204,15 @@
</span><span class="cx"> 
</span><span class="cx">     if (m_sheet)
</span><span class="cx">         m_sheet-&gt;contents().checkLoaded();
</span><ins>+
+    if (cacheKey &amp;&amp; m_sheet &amp;&amp; m_sheet-&gt;contents().isCacheable()) {
+        inlineStyleSheetCache().add(*cacheKey, &amp;m_sheet-&gt;contents());
+
+        // Prevent pathological growth.
+        const size_t maximumInlineStyleSheetCacheSize = 50;
+        if (inlineStyleSheetCache().size() &gt; maximumInlineStyleSheetCacheSize)
+            inlineStyleSheetCache().remove(inlineStyleSheetCache().begin());
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool InlineStyleSheetOwner::isLoading() const
</span><span class="lines">@@ -189,4 +239,9 @@
</span><span class="cx">         m_styleScope-&gt;addPendingSheet();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void InlineStyleSheetOwner::clearCache()
+{
+    inlineStyleSheetCache().clear();
</ins><span class="cx"> }
</span><ins>+
+}
</ins></span></pre></div>
<a id="trunkSourceWebCoredomInlineStyleSheetOwnerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/InlineStyleSheetOwner.h (207285 => 207286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/InlineStyleSheetOwner.h        2016-10-13 11:58:07 UTC (rev 207285)
+++ trunk/Source/WebCore/dom/InlineStyleSheetOwner.h        2016-10-13 11:59:19 UTC (rev 207286)
</span><span class="lines">@@ -52,6 +52,8 @@
</span><span class="cx"> 
</span><span class="cx">     Style::Scope* styleScope() { return m_styleScope; }
</span><span class="cx"> 
</span><ins>+    static void clearCache();
+
</ins><span class="cx"> private:
</span><span class="cx">     void createSheet(Element&amp;, const String&amp; text);
</span><span class="cx">     void createSheetFromTextContents(Element&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformMemoryPressureHandlercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/MemoryPressureHandler.cpp (207285 => 207286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/MemoryPressureHandler.cpp        2016-10-13 11:58:07 UTC (rev 207285)
+++ trunk/Source/WebCore/platform/MemoryPressureHandler.cpp        2016-10-13 11:59:19 UTC (rev 207286)
</span><span class="lines">@@ -33,6 +33,7 @@
</span><span class="cx"> #include &quot;FontCache.h&quot;
</span><span class="cx"> #include &quot;GCController.h&quot;
</span><span class="cx"> #include &quot;HTMLMediaElement.h&quot;
</span><ins>+#include &quot;InlineStyleSheetOwner.h&quot;
</ins><span class="cx"> #include &quot;InspectorInstrumentation.h&quot;
</span><span class="cx"> #include &quot;Logging.h&quot;
</span><span class="cx"> #include &quot;MemoryCache.h&quot;
</span><span class="lines">@@ -103,6 +104,11 @@
</span><span class="cx">         ReliefLogger log(&quot;Prune presentation attribute cache&quot;);
</span><span class="cx">         StyledElement::clearPresentationAttributeCache();
</span><span class="cx">     }
</span><ins>+
+    {
+        ReliefLogger log(&quot;Clear inline stylesheet cache&quot;);
+        InlineStyleSheetOwner::clearCache();
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MemoryPressureHandler::releaseCriticalMemory(Synchronous synchronous)
</span></span></pre>
</div>
</div>

</body>
</html>