[Win] Encode function pointers.
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 13 Oct 2016 11:47:41 +0000 (11:47 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 13 Oct 2016 11:47:41 +0000 (11:47 +0000)
https://bugs.webkit.org/show_bug.cgi?id=163331

Patch by Per Arne Vollan <pvollan@apple.com> on 2016-10-12
Reviewed by Brent Fulgham.

We should encode stored function pointers.

* platform/win/SoftLinking.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@207284 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/css/parser/CSSParserMode.h
Source/WebCore/dom/InlineStyleSheetOwner.cpp
Source/WebCore/dom/InlineStyleSheetOwner.h
Source/WebCore/platform/MemoryPressureHandler.cpp

index 585982a..9ac3580 100644 (file)
 
 2016-10-13  Antti Koivisto  <antti@apple.com>
 
+        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<WebCore::CSSParserContext>::constructDeletedValue):
+        (WTF::HashTraits<WebCore::CSSParserContext>::isDeletedValue):
+        (WTF::HashTraits<WebCore::CSSParserContext>::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  <antti@apple.com>
+
         Support scoped style for user agent shadow trees
         https://bugs.webkit.org/show_bug.cgi?id=163212
         <rdar://problem/28715318>
index df199ef..e9da019 100644 (file)
@@ -32,6 +32,9 @@
 #define CSSParserMode_h
 
 #include "URL.h"
+#include "URLHash.h"
+#include <wtf/HashFunctions.h>
+#include <wtf/text/StringHash.h>
 
 namespace WebCore {
 
@@ -114,6 +117,50 @@ inline bool operator!=(const CSSParserContext& a, const CSSParserContext& b) { r
 
 WEBCORE_EXPORT const CSSParserContext& strictCSSParserContext();
 
+struct CSSParserContextHash {
+    static unsigned hash(const CSSParserContext& key)
+    {
+        auto hash = URLHash::hash(key.baseURL);
+        hash ^= StringHash::hash(key.charset);
+        unsigned bits = key.isHTMLDocument                  << 0
+            & key.isHTMLDocument                            << 1
+#if ENABLE(CSS_GRID_LAYOUT)
+            & key.cssGridLayoutEnabled                      << 2
+#endif
+#if ENABLE(TEXT_AUTOSIZING)
+            & key.textAutosizingEnabled                     << 3
+#endif
+            & key.needsSiteSpecificQuirks                   << 4
+            & key.enforcesCSSMIMETypeInNoQuirksMode         << 5
+            & key.useLegacyBackgroundSizeShorthandBehavior  << 6
+            & key.springTimingFunctionEnabled               << 7
+            & key.useNewParser                              << 8
+#if ENABLE(VARIATION_FONTS)
+            & key.variationFontsEnabled                     << 9
+#endif
+            & key.mode                                      << 10;
+        hash ^= WTF::intHash(bits);
+        return hash;
+    }
+    static bool equal(const CSSParserContext& a, const CSSParserContext& b)
+    {
+        return a == b;
+    }
+    static const bool safeToCompareToEmptyOrDeleted = false;
 };
 
+}
+
+namespace WTF {
+template<> struct HashTraits<WebCore::CSSParserContext> : GenericHashTraits<WebCore::CSSParserContext> {
+    static void constructDeletedValue(WebCore::CSSParserContext& slot) { new (NotNull, &slot.baseURL) WebCore::URL(WTF::HashTableDeletedValue); }
+    static bool isDeletedValue(const WebCore::CSSParserContext& value) { return value.baseURL.isHashTableDeletedValue(); }
+    static WebCore::CSSParserContext emptyValue() { return WebCore::CSSParserContext(WebCore::HTMLStandardMode); }
+};
+
+template<> struct DefaultHash<WebCore::CSSParserContext> {
+    typedef WebCore::CSSParserContextHash Hash;
+};
+}
+
 #endif // CSSParserMode_h
index f64c47a..043b61a 100644 (file)
 #include "StyleScope.h"
 #include "StyleSheetContents.h"
 #include "TextNodeTraversal.h"
+#include <wtf/HashMap.h>
 #include <wtf/NeverDestroyed.h>
 
 namespace WebCore {
 
+using InlineStyleSheetCacheKey = std::pair<String, CSSParserContext>;
+using InlineStyleSheetCache = HashMap<InlineStyleSheetCacheKey, RefPtr<StyleSheetContents>>;
+
+static InlineStyleSheetCache& inlineStyleSheetCache()
+{
+    static NeverDestroyed<InlineStyleSheetCache> cache;
+    return cache;
+}
+
+static URL inlineStyleSheetBaseURLForElement(const Element& 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 && shadowRoot->mode() == ShadowRoot::Mode::UserAgent ? blankURL() : element.document().baseURL();
+}
+
+static Optional<InlineStyleSheetCacheKey> makeInlineStyleSheetCacheKey(const String& text, const Element& 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);
+}
+
 InlineStyleSheetOwner::InlineStyleSheetOwner(Document& document, bool createdByParser)
     : m_isParsingChildren(createdByParser)
     , m_loading(false)
@@ -152,9 +182,20 @@ void InlineStyleSheetOwner::createSheet(Element& element, const String& text)
     if (m_styleScope)
         m_styleScope->addPendingSheet();
 
+    auto cacheKey = makeInlineStyleSheetCacheKey(text, element);
+    if (cacheKey) {
+        if (auto* cachedSheet = inlineStyleSheetCache().get(*cacheKey)) {
+            ASSERT(cachedSheet->isCacheable());
+            m_sheet = CSSStyleSheet::create(*cachedSheet, element);
+            sheetLoaded(element);
+            element.notifyLoadedSheetAndAllCriticalSubresources(false);
+            return;
+        }
+    }
+
     m_loading = true;
 
-    m_sheet = CSSStyleSheet::createInline(element, URL(), m_startTextPosition, document.encoding());
+    m_sheet = CSSStyleSheet::createInline(element, inlineStyleSheetBaseURLForElement(element), m_startTextPosition, document.encoding());
     m_sheet->setMediaQueries(mediaQueries.releaseNonNull());
     m_sheet->setTitle(element.title());
     m_sheet->contents().parseStringAtPosition(text, m_startTextPosition, m_isParsingChildren);
@@ -163,6 +204,15 @@ void InlineStyleSheetOwner::createSheet(Element& element, const String& text)
 
     if (m_sheet)
         m_sheet->contents().checkLoaded();
+
+    if (cacheKey && m_sheet && m_sheet->contents().isCacheable()) {
+        inlineStyleSheetCache().add(*cacheKey, &m_sheet->contents());
+
+        // Prevent pathological growth.
+        const size_t maximumInlineStyleSheetCacheSize = 50;
+        if (inlineStyleSheetCache().size() > maximumInlineStyleSheetCacheSize)
+            inlineStyleSheetCache().remove(inlineStyleSheetCache().begin());
+    }
 }
 
 bool InlineStyleSheetOwner::isLoading() const
@@ -189,4 +239,9 @@ void InlineStyleSheetOwner::startLoadingDynamicSheet(Element&)
         m_styleScope->addPendingSheet();
 }
 
+void InlineStyleSheetOwner::clearCache()
+{
+    inlineStyleSheetCache().clear();
+}
+
 }
index ad58dd4..e4067c9 100644 (file)
@@ -52,6 +52,8 @@ public:
 
     Style::Scope* styleScope() { return m_styleScope; }
 
+    static void clearCache();
+
 private:
     void createSheet(Element&, const String& text);
     void createSheetFromTextContents(Element&);
index 078e10b..ad7f7f6 100644 (file)
@@ -33,6 +33,7 @@
 #include "FontCache.h"
 #include "GCController.h"
 #include "HTMLMediaElement.h"
+#include "InlineStyleSheetOwner.h"
 #include "InspectorInstrumentation.h"
 #include "Logging.h"
 #include "MemoryCache.h"
@@ -103,6 +104,11 @@ void MemoryPressureHandler::releaseNoncriticalMemory()
         ReliefLogger log("Prune presentation attribute cache");
         StyledElement::clearPresentationAttributeCache();
     }
+
+    {
+        ReliefLogger log("Clear inline stylesheet cache");
+        InlineStyleSheetOwner::clearCache();
+    }
 }
 
 void MemoryPressureHandler::releaseCriticalMemory(Synchronous synchronous)