Changeset 210364 in webkit


Ignore:
Timestamp:
Jan 5, 2017 11:10:23 AM (7 years ago)
Author:
Antti Koivisto
Message:

Use WTF::Function instead of std::function in network cache code
https://bugs.webkit.org/show_bug.cgi?id=166721

Reviewed by Andreas Kling.

Use better move-only type. Fix some unnecessary function copies.

  • NetworkProcess/cache/NetworkCache.cpp:

(WebKit::NetworkCache::Cache::retrieve):
(WebKit::NetworkCache::Cache::clear):

  • NetworkProcess/cache/NetworkCache.h:
  • NetworkProcess/cache/NetworkCacheData.h:
  • NetworkProcess/cache/NetworkCacheDataCocoa.mm:

(WebKit::NetworkCache::Data::apply):

  • NetworkProcess/cache/NetworkCacheDataSoup.cpp:

(WebKit::NetworkCache::Data::apply):

  • NetworkProcess/cache/NetworkCacheFileSystem.cpp:

(WebKit::NetworkCache::traverseDirectory):

  • NetworkProcess/cache/NetworkCacheFileSystem.h:
  • NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp:

(WebKit::NetworkCache::SpeculativeLoadManager::canRetrieve):
(WebKit::NetworkCache::SpeculativeLoadManager::retrieve):

Split retrieve() to canRetrieve() and retrieve() functions.
This avoids the need to copy the completion handler in the caller.

  • NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h:
  • NetworkProcess/cache/NetworkCacheStorage.cpp:

(WebKit::NetworkCache::Storage::ReadOperation::ReadOperation):
(WebKit::NetworkCache::Storage::traverse):
(WebKit::NetworkCache::Storage::clear):

  • NetworkProcess/cache/NetworkCacheStorage.h:
Location:
trunk/Source/WebKit2
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r210363 r210364  
     12017-01-05  Antti Koivisto  <antti@apple.com>
     2
     3        Use WTF::Function instead of std::function in network cache code
     4        https://bugs.webkit.org/show_bug.cgi?id=166721
     5
     6        Reviewed by Andreas Kling.
     7
     8        Use better move-only type. Fix some unnecessary function copies.
     9
     10        * NetworkProcess/cache/NetworkCache.cpp:
     11        (WebKit::NetworkCache::Cache::retrieve):
     12        (WebKit::NetworkCache::Cache::clear):
     13        * NetworkProcess/cache/NetworkCache.h:
     14        * NetworkProcess/cache/NetworkCacheData.h:
     15        * NetworkProcess/cache/NetworkCacheDataCocoa.mm:
     16        (WebKit::NetworkCache::Data::apply):
     17        * NetworkProcess/cache/NetworkCacheDataSoup.cpp:
     18        (WebKit::NetworkCache::Data::apply):
     19        * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
     20        (WebKit::NetworkCache::traverseDirectory):
     21        * NetworkProcess/cache/NetworkCacheFileSystem.h:
     22        * NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp:
     23        (WebKit::NetworkCache::SpeculativeLoadManager::canRetrieve):
     24        (WebKit::NetworkCache::SpeculativeLoadManager::retrieve):
     25
     26            Split retrieve() to canRetrieve() and retrieve() functions.
     27            This avoids the need to copy the completion handler in the caller.
     28
     29        * NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h:
     30        * NetworkProcess/cache/NetworkCacheStorage.cpp:
     31        (WebKit::NetworkCache::Storage::ReadOperation::ReadOperation):
     32        (WebKit::NetworkCache::Storage::traverse):
     33        (WebKit::NetworkCache::Storage::clear):
     34        * NetworkProcess/cache/NetworkCacheStorage.h:
     35
    1362017-01-05  Enrica Casucci  <enrica@apple.com>
    237
  • trunk/Source/WebKit2/NetworkProcess/NetworkResourceLoader.cpp

    r208880 r210364  
    178178
    179179    RefPtr<NetworkResourceLoader> loader(this);
    180     NetworkCache::singleton().retrieve(request, { m_parameters.webPageID, m_parameters.webFrameID }, [loader, request](auto entry) {
     180    NetworkCache::singleton().retrieve(request, { m_parameters.webPageID, m_parameters.webFrameID }, [loader = WTFMove(loader), request](auto entry) {
    181181        if (loader->hasOneRef()) {
    182182            // The loader has been aborted and is only held alive by this lambda.
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp

    r209554 r210364  
    310310}
    311311
    312 void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameID& frameID, std::function<void (std::unique_ptr<Entry>)>&& completionHandler)
     312void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameID& frameID, Function<void (std::unique_ptr<Entry>)>&& completionHandler)
    313313{
    314314    ASSERT(isEnabled());
     
    338338
    339339#if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
    340     if (canUseSpeculativeRevalidation && m_speculativeLoadManager->retrieve(frameID, storageKey, request, [request, completionHandler](std::unique_ptr<Entry> entry) {
    341         if (entry && WebCore::verifyVaryingRequestHeaders(entry->varyingRequestHeaders(), request))
    342             completionHandler(WTFMove(entry));
    343         else
    344             completionHandler(nullptr);
    345     }))
     340    if (canUseSpeculativeRevalidation && m_speculativeLoadManager->canRetrieve(storageKey, request, frameID)) {
     341        m_speculativeLoadManager->retrieve(storageKey, [request, completionHandler = WTFMove(completionHandler)](std::unique_ptr<Entry> entry) {
     342            if (entry && WebCore::verifyVaryingRequestHeaders(entry->varyingRequestHeaders(), request))
     343                completionHandler(WTFMove(entry));
     344            else
     345                completionHandler(nullptr);
     346        });
    346347        return;
     348    }
    347349#endif
    348350
     
    587589}
    588590
    589 void Cache::clear(std::chrono::system_clock::time_point modifiedSince, std::function<void ()>&& completionHandler)
     591void Cache::clear(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler)
    590592{
    591593    LOG(NetworkCache, "(NetworkProcess) clearing cache");
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h

    r209554 r210364  
    106106
    107107    // Completion handler may get called back synchronously on failure.
    108     void retrieve(const WebCore::ResourceRequest&, const GlobalFrameID&, std::function<void (std::unique_ptr<Entry>)>&&);
     108    void retrieve(const WebCore::ResourceRequest&, const GlobalFrameID&, Function<void (std::unique_ptr<Entry>)>&&);
    109109    std::unique_ptr<Entry> store(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, RefPtr<WebCore::SharedBuffer>&&, Function<void (MappedBody&)>&&);
    110110    std::unique_ptr<Entry> storeRedirect(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, const WebCore::ResourceRequest& redirectRequest);
     
    120120
    121121    void clear();
    122     void clear(std::chrono::system_clock::time_point modifiedSince, std::function<void ()>&& completionHandler);
     122    void clear(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler);
    123123
    124124    void dumpContentsToFile();
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheData.h

    r209554 r210364  
    129129    Data subrange(size_t offset, size_t) const;
    130130
    131     bool apply(const std::function<bool (const uint8_t*, size_t)>&&) const;
     131    bool apply(const Function<bool (const uint8_t*, size_t)>&) const;
    132132
    133133    Data mapToFile(const char* path) const;
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheDataCocoa.mm

    r187753 r210364  
    7272}
    7373
    74 bool Data::apply(const std::function<bool (const uint8_t*, size_t)>&& applier) const
     74bool Data::apply(const Function<bool (const uint8_t*, size_t)>& applier) const
    7575{
    7676    if (!m_size)
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheDataSoup.cpp

    r194496 r210364  
    7171}
    7272
    73 bool Data::apply(const std::function<bool (const uint8_t*, size_t)>&& applier) const
     73bool Data::apply(const Function<bool (const uint8_t*, size_t)>& applier) const
    7474{
    7575    if (!m_size)
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheFileSystem.cpp

    r185504 r210364  
    3333#include <sys/stat.h>
    3434#include <sys/time.h>
     35#include <wtf/Function.h>
    3536#include <wtf/text/CString.h>
    3637
     
    5657}
    5758
    58 void traverseDirectory(const String& path, const std::function<void (const String&, DirectoryEntryType)>& function)
     59void traverseDirectory(const String& path, const Function<void (const String&, DirectoryEntryType)>& function)
    5960{
    6061    DIR* dir = opendir(WebCore::fileSystemRepresentation(path).data());
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheFileSystem.h

    r185412 r210364  
    3636
    3737enum class DirectoryEntryType { Directory, File };
    38 void traverseDirectory(const String& path, const std::function<void (const String& fileName, DirectoryEntryType)>&);
     38void traverseDirectory(const String& path, const Function<void (const String& fileName, DirectoryEntryType)>&);
    3939
    4040void deleteDirectoryRecursively(const String& path);
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.cpp

    r209554 r210364  
    302302}
    303303
    304 bool SpeculativeLoadManager::retrieve(const GlobalFrameID& frameID, const Key& storageKey, const WebCore::ResourceRequest& request, RetrieveCompletionHandler&& completionHandler)
     304bool SpeculativeLoadManager::canRetrieve(const Key& storageKey, const WebCore::ResourceRequest& request, const GlobalFrameID& frameID) const
    305305{
    306306    // Check already preloaded entries.
    307     if (auto preloadedEntry = m_preloadedEntries.take(storageKey)) {
     307    if (auto preloadedEntry = m_preloadedEntries.get(storageKey)) {
    308308        if (!canUsePreloadedEntry(*preloadedEntry, request)) {
    309309            LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: Could not use preloaded entry to satisfy request for '%s' due to HTTP headers mismatch:", storageKey.identifier().utf8().data());
     
    314314        LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: Using preloaded entry to satisfy request for '%s':", storageKey.identifier().utf8().data());
    315315        logSpeculativeLoadingDiagnosticMessage(frameID, preloadedEntry->wasRevalidated() ? DiagnosticLoggingKeys::successfulSpeculativeWarmupWithRevalidationKey() : DiagnosticLoggingKeys::successfulSpeculativeWarmupWithoutRevalidationKey());
    316 
    317         completionHandler(preloadedEntry->takeCacheEntry());
    318316        return true;
    319317    }
     
    322320    auto* pendingPreload = m_pendingPreloads.get(storageKey);
    323321    if (!pendingPreload) {
    324         if (m_notPreloadedEntries.remove(storageKey))
     322        if (m_notPreloadedEntries.get(storageKey))
    325323            logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::entryWronglyNotWarmedUpKey());
    326324        else
     
    338336    LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: revalidation already in progress for '%s':", storageKey.identifier().utf8().data());
    339337
     338    return true;
     339}
     340
     341void SpeculativeLoadManager::retrieve(const Key& storageKey, RetrieveCompletionHandler&& completionHandler)
     342{
     343    if (auto preloadedEntry = m_preloadedEntries.take(storageKey)) {
     344        completionHandler(preloadedEntry->takeCacheEntry());
     345        return;
     346    }
     347    ASSERT(m_pendingPreloads.contains(storageKey));
    340348    // FIXME: This breaks incremental loading when the revalidation is not successful.
    341     auto addResult = m_pendingRetrieveRequests.ensure(storageKey, [] { return std::make_unique<Vector<RetrieveCompletionHandler>>(); });
     349    auto addResult = m_pendingRetrieveRequests.ensure(storageKey, [] {
     350        return std::make_unique<Vector<RetrieveCompletionHandler>>();
     351    });
    342352    addResult.iterator->value->append(WTFMove(completionHandler));
    343     return true;
    344353}
    345354
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheSpeculativeLoadManager.h

    r208985 r210364  
    5252    void registerLoad(const GlobalFrameID&, const WebCore::ResourceRequest&, const Key& resourceKey);
    5353
    54     typedef std::function<void (std::unique_ptr<Entry>)> RetrieveCompletionHandler;
    55     bool retrieve(const GlobalFrameID&, const Key& storageKey, const WebCore::ResourceRequest&, RetrieveCompletionHandler&&);
     54    typedef Function<void (std::unique_ptr<Entry>)> RetrieveCompletionHandler;
     55
     56    bool canRetrieve(const Key& storageKey, const WebCore::ResourceRequest&, const GlobalFrameID&) const;
     57    void retrieve(const Key& storageKey, RetrieveCompletionHandler&&);
    5658
    5759private:
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.cpp

    r209554 r210364  
    5454    WTF_MAKE_FAST_ALLOCATED;
    5555public:
    56     ReadOperation(const Key& key, const RetrieveCompletionHandler& completionHandler)
     56    ReadOperation(const Key& key, RetrieveCompletionHandler&& completionHandler)
    5757        : key(key)
    58         , completionHandler(completionHandler)
     58        , completionHandler(WTFMove(completionHandler))
    5959    { }
    6060
     
    805805    ASSERT(RunLoop::isMain());
    806806    ASSERT(traverseHandler);
    807     // Avoid non-thread safe std::function copies.
     807    // Avoid non-thread safe Function copies.
    808808
    809809    auto traverseOperationPtr = std::make_unique<TraverseOperation>(type, flags, WTFMove(traverseHandler));
     
    889889}
    890890
    891 void Storage::clear(const String& type, std::chrono::system_clock::time_point modifiedSinceTime, std::function<void ()>&& completionHandler)
     891void Storage::clear(const String& type, std::chrono::system_clock::time_point modifiedSinceTime, Function<void ()>&& completionHandler)
    892892{
    893893    ASSERT(RunLoop::isMain());
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h

    r209554 r210364  
    6060    };
    6161    // This may call completion handler synchronously on failure.
    62     typedef std::function<bool (std::unique_ptr<Record>)> RetrieveCompletionHandler;
     62    typedef Function<bool (std::unique_ptr<Record>)> RetrieveCompletionHandler;
    6363    void retrieve(const Key&, unsigned priority, RetrieveCompletionHandler&&);
    6464
     
    6767
    6868    void remove(const Key&);
    69     void clear(const String& type, std::chrono::system_clock::time_point modifiedSinceTime, std::function<void ()>&& completionHandler);
     69    void clear(const String& type, std::chrono::system_clock::time_point modifiedSinceTime, Function<void ()>&& completionHandler);
    7070
    7171    struct RecordInfo {
     
    180180
    181181// FIXME: Remove, used by NetworkCacheStatistics only.
    182 using RecordFileTraverseFunction = std::function<void (const String& fileName, const String& hashString, const String& type, bool isBlob, const String& recordDirectoryPath)>;
     182using RecordFileTraverseFunction = Function<void (const String& fileName, const String& hashString, const String& type, bool isBlob, const String& recordDirectoryPath)>;
    183183void traverseRecordsFiles(const String& recordsPath, const String& type, const RecordFileTraverseFunction&);
    184184
Note: See TracChangeset for help on using the changeset viewer.