[WK2][Cache] We should not speculatively revalidate transient resources
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 30 Nov 2015 19:06:15 +0000 (19:06 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 30 Nov 2015 19:06:15 +0000 (19:06 +0000)
https://bugs.webkit.org/show_bug.cgi?id=151402
<rdar://problem/23092196>

Reviewed by Antti Koivisto.

We should not speculatively revalidate transient resources. This patch
adds a simple and conservative algorithm to detect that a subresource is
transient and then ignores those when doing the speculative revalidation.

The algorithm is question marks as transient all subresources that are
not common to the 2 last loads of a main resource.

This is not perfect as I see the number of non-speculative revalidations
going up to 11-12 from 9 in the context of the warm PLT. However, it is
best to be conservative at first and we can improve this later.

* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::Cache::retrieve):
* NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp:
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::create):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::~PendingFrameLoad):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::registerSubresource):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::markLoadAsCompleted):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::setExistingSubresourcesEntry):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::PendingFrameLoad):
(WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::saveToDiskIfReady):
(WebKit::NetworkCache::SpeculativeLoadManager::registerLoad):
(WebKit::NetworkCache::SpeculativeLoadManager::startSpeculativeRevalidation):
(WebKit::NetworkCache::SpeculativeLoadManager::retrieveSubresourcesEntry):
(WebKit::NetworkCache::SpeculativeLoadManager::retrieve): Deleted.
(WebKit::NetworkCache::SpeculativeLoadManager::revalidateEntry): Deleted.
* NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h:
* NetworkProcess/cache/NetworkCacheSubresourcesEntry.cpp:
(WebKit::NetworkCache::SubresourcesEntry::encodeAsStorageRecord):
(WebKit::NetworkCache::SubresourcesEntry::decodeStorageRecord):
(WebKit::NetworkCache::SubresourcesEntry::SubresourcesEntry):
(WebKit::NetworkCache::SubresourcesEntry::updateSubresourceKeys):
* NetworkProcess/cache/NetworkCacheSubresourcesEntry.h:
(WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::encode):
(WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::decode):
(WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::SubresourceInfo):
(WebKit::NetworkCache::SubresourcesEntry::subresources):

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

Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp
Source/WebKit2/NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp
Source/WebKit2/NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h
Source/WebKit2/NetworkProcess/cache/NetworkCacheSubresourcesEntry.cpp
Source/WebKit2/NetworkProcess/cache/NetworkCacheSubresourcesEntry.h

index 18c27b9..5104ed9 100644 (file)
@@ -1,3 +1,49 @@
+2015-11-30  Chris Dumez  <cdumez@apple.com>
+
+        [WK2][Cache] We should not speculatively revalidate transient resources
+        https://bugs.webkit.org/show_bug.cgi?id=151402
+        <rdar://problem/23092196>
+
+        Reviewed by Antti Koivisto.
+
+        We should not speculatively revalidate transient resources. This patch
+        adds a simple and conservative algorithm to detect that a subresource is
+        transient and then ignores those when doing the speculative revalidation.
+
+        The algorithm is question marks as transient all subresources that are
+        not common to the 2 last loads of a main resource.
+
+        This is not perfect as I see the number of non-speculative revalidations
+        going up to 11-12 from 9 in the context of the warm PLT. However, it is
+        best to be conservative at first and we can improve this later.
+
+        * NetworkProcess/cache/NetworkCache.cpp:
+        (WebKit::NetworkCache::Cache::retrieve):
+        * NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp:
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::create):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::~PendingFrameLoad):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::registerSubresource):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::markLoadAsCompleted):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::setExistingSubresourcesEntry):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::PendingFrameLoad):
+        (WebKit::NetworkCache::SpeculativeLoadManager::PendingFrameLoad::saveToDiskIfReady):
+        (WebKit::NetworkCache::SpeculativeLoadManager::registerLoad):
+        (WebKit::NetworkCache::SpeculativeLoadManager::startSpeculativeRevalidation):
+        (WebKit::NetworkCache::SpeculativeLoadManager::retrieveSubresourcesEntry):
+        (WebKit::NetworkCache::SpeculativeLoadManager::retrieve): Deleted.
+        (WebKit::NetworkCache::SpeculativeLoadManager::revalidateEntry): Deleted.
+        * NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h:
+        * NetworkProcess/cache/NetworkCacheSubresourcesEntry.cpp:
+        (WebKit::NetworkCache::SubresourcesEntry::encodeAsStorageRecord):
+        (WebKit::NetworkCache::SubresourcesEntry::decodeStorageRecord):
+        (WebKit::NetworkCache::SubresourcesEntry::SubresourcesEntry):
+        (WebKit::NetworkCache::SubresourcesEntry::updateSubresourceKeys):
+        * NetworkProcess/cache/NetworkCacheSubresourcesEntry.h:
+        (WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::encode):
+        (WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::decode):
+        (WebKit::NetworkCache::SubresourcesEntry::SubresourceInfo::SubresourceInfo):
+        (WebKit::NetworkCache::SubresourcesEntry::subresources):
+
 2015-11-30  Brent Fulgham  <bfulgham@apple.com>
 
         [Mac] Add font service permission to the sandbox profile
index 555cd55..174b62a 100644 (file)
@@ -356,12 +356,8 @@ void Cache::retrieve(const WebCore::ResourceRequest& originalRequest, const Glob
     Key storageKey = makeCacheKey(originalRequest);
 
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
-    if (m_speculativeLoadManager) {
+    if (m_speculativeLoadManager)
         m_speculativeLoadManager->registerLoad(frameID, originalRequest, storageKey);
-        RunLoop::main().dispatch([this, originalRequest, frameID, storageKey] {
-            m_speculativeLoadManager->startSpeculativeRevalidation(originalRequest, frameID, storageKey);
-        });
-    }
 #endif
 
     auto retrieveDecision = makeRetrieveDecision(originalRequest);
index 0412ba6..4449668 100644 (file)
@@ -34,6 +34,7 @@
 #include "NetworkCacheSubresourcesEntry.h"
 #include <WebCore/HysteresisActivity.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/RefCounted.h>
 #include <wtf/RunLoop.h>
 
 namespace WebKit {
@@ -109,52 +110,86 @@ private:
     std::function<void()> m_lifetimeReachedHandler;
 };
 
-class SpeculativeLoadManager::PendingFrameLoad {
-    WTF_MAKE_FAST_ALLOCATED;
+class SpeculativeLoadManager::PendingFrameLoad : public RefCounted<PendingFrameLoad> {
 public:
-    PendingFrameLoad(const Key& mainResourceKey, std::function<void()>&& completionHandler)
-        : m_mainResourceKey(mainResourceKey)
-        , m_completionHandler(WTF::move(completionHandler))
-        , m_loadHysteresisActivity([this](HysteresisState state) { if (state == HysteresisState::Stopped) m_completionHandler(); })
-    { }
+    static Ref<PendingFrameLoad> create(Storage& storage, const Key& mainResourceKey, std::function<void()>&& loadCompletionHandler)
+    {
+        return adoptRef(*new PendingFrameLoad(storage, mainResourceKey, WTF::move(loadCompletionHandler)));
+    }
+
+    ~PendingFrameLoad()
+    {
+        ASSERT(m_didFinishLoad);
+        ASSERT(m_didRetrieveExistingEntry);
+    }
 
     void registerSubresource(const Key& subresourceKey)
     {
         ASSERT(RunLoop::isMain());
-        m_subresourceKeys.add(subresourceKey);
+        m_subresourceKeys.append(subresourceKey);
         m_loadHysteresisActivity.impulse();
     }
 
-    Optional<Storage::Record> encodeAsSubresourcesRecord()
+    void markLoadAsCompleted()
     {
         ASSERT(RunLoop::isMain());
-        if (m_subresourceKeys.isEmpty())
-            return { };
+        if (m_didFinishLoad)
+            return;
+
+        m_didFinishLoad = true;
+        saveToDiskIfReady();
+        m_loadCompletionHandler();
+    }
+
+    void setExistingSubresourcesEntry(std::unique_ptr<SubresourcesEntry> entry)
+    {
+        ASSERT(!m_existingEntry);
+        ASSERT(!m_didRetrieveExistingEntry);
+
+        m_existingEntry = WTF::move(entry);
+        m_didRetrieveExistingEntry = true;
+        saveToDiskIfReady();
+    }
+
+private:
+    PendingFrameLoad(Storage& storage, const Key& mainResourceKey, std::function<void()>&& loadCompletionHandler)
+        : m_storage(storage)
+        , m_mainResourceKey(mainResourceKey)
+        , m_loadCompletionHandler(WTF::move(loadCompletionHandler))
+        , m_loadHysteresisActivity([this](HysteresisState state) { if (state == HysteresisState::Stopped) markLoadAsCompleted(); })
+    { }
+
+    void saveToDiskIfReady()
+    {
+        if (!m_didFinishLoad || !m_didRetrieveExistingEntry)
+            return;
 
-        auto subresourcesStorageKey = makeSubresourcesKey(m_mainResourceKey);
-        Vector<Key> subresourceKeys;
-        copyToVector(m_subresourceKeys, subresourceKeys);
+        if (m_subresourceKeys.isEmpty())
+            return;
 
 #if !LOG_DISABLED
         LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Saving to disk list of subresources for '%s':", m_mainResourceKey.identifier().utf8().data());
-        for (auto& subresourceKey : subresourceKeys)
+        for (auto& subresourceKey : m_subresourceKeys)
             LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) * Subresource: '%s'.", subresourceKey.identifier().utf8().data());
 #endif
 
-        return SubresourcesEntry(WTF::move(subresourcesStorageKey), WTF::move(subresourceKeys)).encodeAsStorageRecord();
-    }
-
-    void markAsCompleted()
-    {
-        ASSERT(RunLoop::isMain());
-        m_completionHandler();
+        if (m_existingEntry) {
+            m_existingEntry->updateSubresourceKeys(m_subresourceKeys);
+            m_storage.store(m_existingEntry->encodeAsStorageRecord(), [](const Data&) { });
+        } else {
+            SubresourcesEntry entry(makeSubresourcesKey(m_mainResourceKey), m_subresourceKeys);
+            m_storage.store(entry.encodeAsStorageRecord(), [](const Data&) { });
+        }
     }
 
-private:
+    Storage& m_storage;
     Key m_mainResourceKey;
-    HashSet<Key> m_subresourceKeys;
-    std::function<void()> m_completionHandler;
+    Vector<Key> m_subresourceKeys;
+    std::function<void()> m_loadCompletionHandler;
     HysteresisActivity m_loadHysteresisActivity;
+    std::unique_ptr<SubresourcesEntry> m_existingEntry;
+    bool m_didFinishLoad { false };
+    bool m_didRetrieveExistingEntry { false };
 };
 
 SpeculativeLoadManager::SpeculativeLoadManager(Storage& storage)
@@ -200,16 +235,24 @@ void SpeculativeLoadManager::registerLoad(const GlobalFrameID& frameID, const Re
     if (isMainResource) {
         // Mark previous load in this frame as completed if necessary.
         if (auto* pendingFrameLoad = m_pendingFrameLoads.get(frameID))
-            pendingFrameLoad->markAsCompleted();
+            pendingFrameLoad->markLoadAsCompleted();
+
+        ASSERT(!m_pendingFrameLoads.contains(frameID));
 
         // Start tracking loads in this frame.
-        m_pendingFrameLoads.add(frameID, std::make_unique<PendingFrameLoad>(resourceKey, [this, frameID]() {
-            auto frameLoad = m_pendingFrameLoads.take(frameID);
-            auto optionalRecord = frameLoad->encodeAsSubresourcesRecord();
-            if (!optionalRecord)
-                return;
-            m_storage.store(optionalRecord.value(), [](const Data&) { });
-        }));
+        RefPtr<PendingFrameLoad> pendingFrameLoad = PendingFrameLoad::create(m_storage, resourceKey, [this, frameID] {
+            bool wasRemoved = m_pendingFrameLoads.remove(frameID);
+            ASSERT_UNUSED(wasRemoved, wasRemoved);
+        });
+        m_pendingFrameLoads.add(frameID, pendingFrameLoad);
+
+        // Retrieve the subresources entry if it exists to start speculative revalidation and to update it.
+        retrieveSubresourcesEntry(resourceKey, [this, frameID, pendingFrameLoad](std::unique_ptr<SubresourcesEntry> entry) {
+            if (entry)
+                startSpeculativeRevalidation(frameID, *entry);
+
+            pendingFrameLoad->setExistingSubresourcesEntry(WTF::move(entry));
+        });
         return;
     }
 
@@ -306,24 +349,33 @@ void SpeculativeLoadManager::preloadEntry(const Key& key, const GlobalFrameID& f
     });
 }
 
-void SpeculativeLoadManager::startSpeculativeRevalidation(const ResourceRequest& originalRequest, const GlobalFrameID& frameID, const Key& storageKey)
+void SpeculativeLoadManager::startSpeculativeRevalidation(const GlobalFrameID& frameID, SubresourcesEntry& entry)
 {
-    if (originalRequest.requester() != ResourceRequest::Requester::Main)
-        return;
+    for (auto& subresource : entry.subresources()) {
+        if (!subresource.value.isTransient)
+            preloadEntry(subresource.key, frameID);
+        else
+            LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Not preloading '%s' because it is marked as transient", subresource.key.identifier().utf8().data());
+    }
+}
 
+void SpeculativeLoadManager::retrieveSubresourcesEntry(const Key& storageKey, std::function<void (std::unique_ptr<SubresourcesEntry>)> completionHandler)
+{
+    ASSERT(storageKey.type() == "resource");
     auto subresourcesStorageKey = makeSubresourcesKey(storageKey);
-
-    m_storage.retrieve(subresourcesStorageKey, static_cast<unsigned>(ResourceLoadPriority::Medium), [this, frameID](std::unique_ptr<Storage::Record> record) {
-        if (!record)
+    m_storage.retrieve(subresourcesStorageKey, static_cast<unsigned>(ResourceLoadPriority::Medium), [completionHandler](std::unique_ptr<Storage::Record> record) {
+        if (!record) {
+            completionHandler(nullptr);
             return false;
+        }
 
         auto subresourcesEntry = SubresourcesEntry::decodeStorageRecord(*record);
-        if (!subresourcesEntry)
+        if (!subresourcesEntry) {
+            completionHandler(nullptr);
             return false;
+        }
 
-        for (auto& subresourceKey : subresourcesEntry->subresourceKeys())
-            preloadEntry(subresourceKey, frameID);
-
+        completionHandler(WTF::move(subresourcesEntry));
         return true;
     });
 }
index 364c58c..21ce5c0 100644 (file)
@@ -40,6 +40,7 @@ namespace NetworkCache {
 
 class Entry;
 class SpeculativeLoad;
+class SubresourcesEntry;
 
 class SpeculativeLoadManager {
 public:
@@ -51,19 +52,19 @@ public:
     typedef std::function<void (std::unique_ptr<Entry>)> RetrieveCompletionHandler;
     bool retrieve(const Key& storageKey, const RetrieveCompletionHandler&);
 
-    void startSpeculativeRevalidation(const WebCore::ResourceRequest&, const GlobalFrameID&, const Key& storageKey);
-
 private:
     void addPreloadedEntry(std::unique_ptr<Entry>);
     void preloadEntry(const Key&, const GlobalFrameID&);
     void retrieveEntryFromStorage(const Key&, const RetrieveCompletionHandler&);
     void revalidateEntry(std::unique_ptr<Entry>, const GlobalFrameID&);
     bool satisfyPendingRequests(const Key&, Entry*);
+    void retrieveSubresourcesEntry(const Key& storageKey, std::function<void (std::unique_ptr<SubresourcesEntry>)>);
+    void startSpeculativeRevalidation(const GlobalFrameID&, SubresourcesEntry&);
 
     Storage& m_storage;
 
     class PendingFrameLoad;
-    HashMap<GlobalFrameID, std::unique_ptr<PendingFrameLoad>> m_pendingFrameLoads;
+    HashMap<GlobalFrameID, RefPtr<PendingFrameLoad>> m_pendingFrameLoads;
 
     HashMap<Key, std::unique_ptr<SpeculativeLoad>> m_pendingPreloads;
     HashMap<Key, std::unique_ptr<Vector<RetrieveCompletionHandler>>> m_pendingRetrieveRequests;
index 6cb11d0..2eb7fae 100644 (file)
@@ -39,7 +39,7 @@ namespace NetworkCache {
 Storage::Record SubresourcesEntry::encodeAsStorageRecord() const
 {
     Encoder encoder;
-    encoder << m_subresourceKeys;
+    encoder << m_subresources;
 
     encoder.encodeChecksum();
 
@@ -51,7 +51,7 @@ std::unique_ptr<SubresourcesEntry> SubresourcesEntry::decodeStorageRecord(const
     auto entry = std::make_unique<SubresourcesEntry>(storageEntry);
 
     Decoder decoder(storageEntry.header.data(), storageEntry.header.size());
-    if (!decoder.decode(entry->m_subresourceKeys))
+    if (!decoder.decode(entry->m_subresources))
         return nullptr;
 
     if (!decoder.verifyChecksum()) {
@@ -69,12 +69,24 @@ SubresourcesEntry::SubresourcesEntry(const Storage::Record& storageEntry)
     ASSERT(m_key.type() == "subresources");
 }
 
-SubresourcesEntry::SubresourcesEntry(Key&& key, Vector<Key>&& subresourceKeys)
+SubresourcesEntry::SubresourcesEntry(Key&& key, const Vector<Key>& subresourceKeys)
     : m_key(WTF::move(key))
     , m_timeStamp(std::chrono::system_clock::now())
-    , m_subresourceKeys(WTF::move(subresourceKeys))
 {
     ASSERT(m_key.type() == "subresources");
+    for (auto& key : subresourceKeys)
+        m_subresources.add(key, SubresourceInfo());
+}
+
+void SubresourcesEntry::updateSubresourceKeys(const Vector<Key>& subresourceKeys)
+{
+    auto oldSubresources = WTF::move(m_subresources);
+
+    // Mark keys that are common with last load as non-Transient.
+    for (auto& key : subresourceKeys) {
+        bool isTransient = !oldSubresources.contains(key);
+        m_subresources.add(key, SubresourceInfo(isTransient));
+    }
 }
 
 } // namespace WebKit
index 973f2a4..d12282f 100644 (file)
 
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
 
+#include "NetworkCacheDecoder.h"
+#include "NetworkCacheEncoder.h"
 #include "NetworkCacheStorage.h"
+#include <wtf/HashMap.h>
 
 namespace WebKit {
 namespace NetworkCache {
@@ -36,7 +39,16 @@ namespace NetworkCache {
 class SubresourcesEntry {
     WTF_MAKE_NONCOPYABLE(SubresourcesEntry); WTF_MAKE_FAST_ALLOCATED;
 public:
-    SubresourcesEntry(Key&&, Vector<Key>&& subresourceKeys);
+    struct SubresourceInfo {
+        void encode(Encoder& encoder) const { encoder << isTransient; }
+        static bool decode(Decoder& decoder, SubresourceInfo& info) { return decoder.decode(info.isTransient); }
+
+        SubresourceInfo() = default;
+        SubresourceInfo(bool isTransient) : isTransient(isTransient) { }
+
+        bool isTransient { false };
+    };
+    SubresourcesEntry(Key&&, const Vector<Key>& subresourceKeys);
     explicit SubresourcesEntry(const Storage::Record&);
 
     Storage::Record encodeAsStorageRecord() const;
@@ -44,12 +56,14 @@ public:
 
     const Key& key() const { return m_key; }
     std::chrono::system_clock::time_point timeStamp() const { return m_timeStamp; }
-    const Vector<Key>& subresourceKeys() const { return m_subresourceKeys; }
+    const HashMap<Key, SubresourceInfo>& subresources() const { return m_subresources; }
+
+    void updateSubresourceKeys(const Vector<Key>&);
 
 private:
     Key m_key;
     std::chrono::system_clock::time_point m_timeStamp;
-    Vector<Key> m_subresourceKeys;
+    HashMap<Key, SubresourceInfo> m_subresources;
 };
 
 } // namespace WebKit