Skip to content
Permalink
Browse files
Modernize lambda captures in the network disk cache implementation
https://bugs.webkit.org/show_bug.cgi?id=158179

Reviewed by Darin Adler.

Modernize lambda captures in the network disk cache implementation.

* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::Cache::retrieve):
(WebKit::NetworkCache::Cache::store):
(WebKit::NetworkCache::Cache::traverse):
* NetworkProcess/cache/NetworkCache.h:
* NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
(WebKit::NetworkCache::inputStreamReadReadyCallback):
(WebKit::NetworkCache::outputStreamWriteReadyCallback):
* NetworkProcess/cache/NetworkCacheStatistics.cpp:
(WebKit::NetworkCache::Statistics::initialize):
(WebKit::NetworkCache::Statistics::shrinkIfNeeded):
(WebKit::NetworkCache::Statistics::queryWasEverRequested):
* NetworkProcess/cache/NetworkCacheStatistics.h:
* NetworkProcess/cache/NetworkCacheStorage.cpp:
(WebKit::NetworkCache::Storage::updateFileModificationTime):
(WebKit::NetworkCache::Storage::clear):


Canonical link: https://commits.webkit.org/176284@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@201484 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed May 28, 2016
1 parent 217c2e6 commit 1db3462d12c8a5e085debc094f5f935999323bf2
@@ -1,3 +1,29 @@
2016-05-28 Chris Dumez <cdumez@apple.com>

Modernize lambda captures in the network disk cache implementation
https://bugs.webkit.org/show_bug.cgi?id=158179

Reviewed by Darin Adler.

Modernize lambda captures in the network disk cache implementation.

* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::Cache::retrieve):
(WebKit::NetworkCache::Cache::store):
(WebKit::NetworkCache::Cache::traverse):
* NetworkProcess/cache/NetworkCache.h:
* NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
(WebKit::NetworkCache::inputStreamReadReadyCallback):
(WebKit::NetworkCache::outputStreamWriteReadyCallback):
* NetworkProcess/cache/NetworkCacheStatistics.cpp:
(WebKit::NetworkCache::Statistics::initialize):
(WebKit::NetworkCache::Statistics::shrinkIfNeeded):
(WebKit::NetworkCache::Statistics::queryWasEverRequested):
* NetworkProcess/cache/NetworkCacheStatistics.h:
* NetworkProcess/cache/NetworkCacheStorage.cpp:
(WebKit::NetworkCache::Storage::updateFileModificationTime):
(WebKit::NetworkCache::Storage::clear):

2016-05-27 Caitlin Potter <caitp@igalia.com>

[JSC] implement async functions proposal
@@ -345,7 +345,7 @@ static StoreDecision makeStoreDecision(const WebCore::ResourceRequest& originalR
return StoreDecision::Yes;
}

void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameID& frameID, std::function<void (std::unique_ptr<Entry>)> completionHandler)
void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameID& frameID, std::function<void (std::unique_ptr<Entry>)>&& completionHandler)
{
ASSERT(isEnabled());
ASSERT(request.url().protocolIsInHTTPFamily());
@@ -384,7 +384,7 @@ void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameI
auto startTime = std::chrono::system_clock::now();
auto priority = static_cast<unsigned>(request.priority());

m_storage->retrieve(storageKey, priority, [this, request, completionHandler, startTime, storageKey, frameID](std::unique_ptr<Storage::Record> record) {
m_storage->retrieve(storageKey, priority, [this, request, completionHandler = WTFMove(completionHandler), startTime, storageKey, frameID](std::unique_ptr<Storage::Record> record) {
if (!record) {
LOG(NetworkCache, "(NetworkProcess) not found in storage");

@@ -422,7 +422,7 @@ void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameI
});
}

std::unique_ptr<Entry> Cache::store(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, RefPtr<WebCore::SharedBuffer>&& responseData, std::function<void (MappedBody&)> completionHandler)
std::unique_ptr<Entry> Cache::store(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, RefPtr<WebCore::SharedBuffer>&& responseData, std::function<void (MappedBody&)>&& completionHandler)
{
ASSERT(isEnabled());
ASSERT(responseData);
@@ -446,11 +446,10 @@ std::unique_ptr<Entry> Cache::store(const WebCore::ResourceRequest& request, con
return nullptr;
}

std::unique_ptr<Entry> cacheEntry = std::make_unique<Entry>(makeCacheKey(request), response, WTFMove(responseData), collectVaryingRequestHeaders(request, response));

auto cacheEntry = std::make_unique<Entry>(makeCacheKey(request), response, WTFMove(responseData), collectVaryingRequestHeaders(request, response));
auto record = cacheEntry->encodeAsStorageRecord();

m_storage->store(record, [completionHandler](const Data& bodyData) {
m_storage->store(record, [completionHandler = WTFMove(completionHandler)](const Data& bodyData) {
MappedBody mappedBody;
#if ENABLE(SHAREABLE_RESOURCE)
if (RefPtr<SharedMemory> sharedMemory = bodyData.tryCreateSharedMemory()) {
@@ -521,7 +520,7 @@ void Cache::remove(const WebCore::ResourceRequest& request)
remove(makeCacheKey(request));
}

void Cache::traverse(const std::function<void (const TraversalEntry*)>& traverseHandler)
void Cache::traverse(std::function<void (const TraversalEntry*)>&& traverseHandler)
{
ASSERT(isEnabled());

@@ -530,15 +529,15 @@ void Cache::traverse(const std::function<void (const TraversalEntry*)>& traverse
if (m_traverseCount >= maximumTraverseCount) {
WTFLogAlways("Maximum parallel cache traverse count exceeded. Ignoring traversal request.");

RunLoop::main().dispatch([traverseHandler] {
RunLoop::main().dispatch([traverseHandler = WTFMove(traverseHandler)] {
traverseHandler(nullptr);
});
return;
}

++m_traverseCount;

m_storage->traverse(resourceType(), 0, [this, traverseHandler](const Storage::Record* record, const Storage::RecordInfo& recordInfo) {
m_storage->traverse(resourceType(), 0, [this, traverseHandler = WTFMove(traverseHandler)](const Storage::Record* record, const Storage::RecordInfo& recordInfo) {
if (!record) {
--m_traverseCount;
traverseHandler(nullptr);
@@ -104,16 +104,16 @@ class Cache {
bool isEnabled() const { return !!m_storage; }

// Completion handler may get called back synchronously on failure.
void retrieve(const WebCore::ResourceRequest&, const GlobalFrameID&, std::function<void (std::unique_ptr<Entry>)>);
std::unique_ptr<Entry> store(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, RefPtr<WebCore::SharedBuffer>&&, std::function<void (MappedBody&)>);
void retrieve(const WebCore::ResourceRequest&, const GlobalFrameID&, std::function<void (std::unique_ptr<Entry>)>&&);
std::unique_ptr<Entry> store(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, RefPtr<WebCore::SharedBuffer>&&, std::function<void (MappedBody&)>&&);
std::unique_ptr<Entry> storeRedirect(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, const WebCore::ResourceRequest& redirectRequest);
std::unique_ptr<Entry> update(const WebCore::ResourceRequest&, const GlobalFrameID&, const Entry&, const WebCore::ResourceResponse& validatingResponse);

struct TraversalEntry {
const Entry& entry;
const Storage::RecordInfo& recordInfo;
};
void traverse(const std::function<void (const TraversalEntry*)>&);
void traverse(std::function<void (const TraversalEntry*)>&&);
void remove(const Key&);
void remove(const WebCore::ResourceRequest&);

@@ -114,19 +114,15 @@ static void inputStreamReadReadyCallback(GInputStream* stream, GAsyncResult* res
gssize bytesRead = g_input_stream_read_finish(stream, result, nullptr);
if (bytesRead == -1) {
WorkQueue* queue = asyncData->queue.get();
auto* asyncDataPtr = asyncData.release();
runTaskInQueue([asyncDataPtr] {
std::unique_ptr<ReadAsyncData> asyncData(asyncDataPtr);
runTaskInQueue([asyncData = WTFMove(asyncData)] {
asyncData->completionHandler(asyncData->data, -1);
}, queue);
return;
}

if (!bytesRead) {
WorkQueue* queue = asyncData->queue.get();
auto* asyncDataPtr = asyncData.release();
runTaskInQueue([asyncDataPtr] {
std::unique_ptr<ReadAsyncData> asyncData(asyncDataPtr);
runTaskInQueue([asyncData = WTFMove(asyncData)] {
asyncData->completionHandler(asyncData->data, 0);
}, queue);
return;
@@ -138,9 +134,7 @@ static void inputStreamReadReadyCallback(GInputStream* stream, GAsyncResult* res
size_t pendingBytesToRead = asyncData->bytesToRead - asyncData->data.size();
if (!pendingBytesToRead) {
WorkQueue* queue = asyncData->queue.get();
auto* asyncDataPtr = asyncData.release();
runTaskInQueue([asyncDataPtr] {
std::unique_ptr<ReadAsyncData> asyncData(asyncDataPtr);
runTaskInQueue([asyncData = WTFMove(asyncData)] {
asyncData->completionHandler(asyncData->data, 0);
}, queue);
return;
@@ -234,9 +228,7 @@ static void outputStreamWriteReadyCallback(GOutputStream* stream, GAsyncResult*
gssize bytesWritten = g_output_stream_write_finish(stream, result, nullptr);
if (bytesWritten == -1) {
WorkQueue* queue = asyncData->queue.get();
auto* asyncDataPtr = asyncData.release();
runTaskInQueue([asyncDataPtr] {
std::unique_ptr<WriteAsyncData> asyncData(asyncDataPtr);
runTaskInQueue([asyncData = WTFMove(asyncData)] {
asyncData->completionHandler(-1);
}, queue);
return;
@@ -245,9 +237,7 @@ static void outputStreamWriteReadyCallback(GOutputStream* stream, GAsyncResult*
gssize pendingBytesToWrite = asyncData->buffer->length - bytesWritten;
if (!pendingBytesToWrite) {
WorkQueue* queue = asyncData->queue.get();
auto* asyncDataPtr = asyncData.release();
runTaskInQueue([asyncDataPtr] {
std::unique_ptr<WriteAsyncData> asyncData(asyncDataPtr);
runTaskInQueue([asyncData = WTFMove(asyncData)] {
asyncData->completionHandler(0);
}, queue);
return;
@@ -94,12 +94,9 @@ void Statistics::initialize(const String& databasePath)

auto startTime = std::chrono::system_clock::now();

StringCapture databasePathCapture(databasePath);
StringCapture networkCachePathCapture(singleton().recordsPath());
serialBackgroundIOQueue().dispatch([this, databasePathCapture, networkCachePathCapture, startTime] {
serialBackgroundIOQueue().dispatch([this, databasePath = databasePath.isolatedCopy(), networkCachePath = singleton().recordsPath().isolatedCopy(), startTime] {
WebCore::SQLiteTransactionInProgressAutoCounter transactionCounter;

String databasePath = databasePathCapture.string();
if (!WebCore::makeAllDirectories(WebCore::directoryName(databasePath)))
return;

@@ -128,7 +125,7 @@ void Statistics::initialize(const String& databasePath)
LOG(NetworkCache, "(NetworkProcess) Network cache statistics database load complete, entries=%lu time=%" PRIi64 "ms", static_cast<size_t>(m_approximateEntryCount), elapsedMS);

if (!m_approximateEntryCount) {
bootstrapFromNetworkCache(networkCachePathCapture.string());
bootstrapFromNetworkCache(networkCachePath);
#if !LOG_DISABLED
elapsedMS = static_cast<int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - startTime).count());
#endif
@@ -176,9 +173,8 @@ void Statistics::shrinkIfNeeded()

clear();

StringCapture networkCachePathCapture(singleton().recordsPath());
serialBackgroundIOQueue().dispatch([this, networkCachePathCapture] {
bootstrapFromNetworkCache(networkCachePathCapture.string());
serialBackgroundIOQueue().dispatch([this, networkCachePath = singleton().recordsPath().isolatedCopy()] {
bootstrapFromNetworkCache(networkCachePath);
LOG(NetworkCache, "(NetworkProcess) statistics cache shrink completed m_approximateEntryCount=%lu", static_cast<size_t>(m_approximateEntryCount));
});
}
@@ -348,7 +344,7 @@ void Statistics::writeTimerFired()
shrinkIfNeeded();
}

void Statistics::queryWasEverRequested(const String& hash, NeedUncachedReason needUncachedReason, const RequestedCompletionHandler& completionHandler)
void Statistics::queryWasEverRequested(const String& hash, NeedUncachedReason needUncachedReason, RequestedCompletionHandler&& completionHandler)
{
ASSERT(RunLoop::isMain());

@@ -364,7 +360,7 @@ void Statistics::queryWasEverRequested(const String& hash, NeedUncachedReason ne
}

// Query the database.
auto everRequestedQuery = std::make_unique<EverRequestedQuery>(EverRequestedQuery { hash, needUncachedReason == NeedUncachedReason::Yes, completionHandler });
auto everRequestedQuery = std::make_unique<EverRequestedQuery>(EverRequestedQuery { hash, needUncachedReason == NeedUncachedReason::Yes, WTFMove(completionHandler) });
auto& query = *everRequestedQuery;
m_activeQueries.add(WTFMove(everRequestedQuery));
serialBackgroundIOQueue().dispatch([this, wasAlreadyRequested, &query] () mutable {
@@ -68,7 +68,7 @@ class Statistics {

typedef std::function<void (bool wasEverRequested, const Optional<StoreDecision>&)> RequestedCompletionHandler;
enum class NeedUncachedReason { No, Yes };
void queryWasEverRequested(const String&, NeedUncachedReason, const RequestedCompletionHandler&);
void queryWasEverRequested(const String&, NeedUncachedReason, RequestedCompletionHandler&&);
void markAsRequested(const String& hash);

struct EverRequestedQuery {
@@ -553,9 +553,8 @@ void Storage::remove(const Key& key)

void Storage::updateFileModificationTime(const String& path)
{
StringCapture filePathCapture(path);
serialBackgroundIOQueue().dispatch([filePathCapture] {
updateFileModificationTimeIfNeeded(filePathCapture.string());
serialBackgroundIOQueue().dispatch([path = path.isolatedCopy()] {
updateFileModificationTimeIfNeeded(path);
});
}

@@ -893,10 +892,9 @@ void Storage::clear(const String& type, std::chrono::system_clock::time_point mo
m_blobFilter->clear();
m_approximateRecordsSize = 0;

StringCapture typeCapture(type);
ioQueue().dispatch([this, modifiedSinceTime, completionHandler = WTFMove(completionHandler), typeCapture] () mutable {
ioQueue().dispatch([this, modifiedSinceTime, completionHandler = WTFMove(completionHandler), type = type.isolatedCopy()] () mutable {
auto recordsPath = this->recordsPath();
traverseRecordsFiles(recordsPath, typeCapture.string(), [modifiedSinceTime](const String& fileName, const String& hashString, const String& type, bool isBlob, const String& recordDirectoryPath) {
traverseRecordsFiles(recordsPath, type, [modifiedSinceTime](const String& fileName, const String& hashString, const String& type, bool isBlob, const String& recordDirectoryPath) {
auto filePath = WebCore::pathByAppendingComponent(recordDirectoryPath, fileName);
if (modifiedSinceTime > std::chrono::system_clock::time_point::min()) {
auto times = fileTimes(filePath);

0 comments on commit 1db3462

Please sign in to comment.