<!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>[179546] trunk/Source/WebKit2</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/179546">179546</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2015-02-03 04:41:52 -0800 (Tue, 03 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Update cache header after revalidation without rewriting the body data
https://bugs.webkit.org/show_bug.cgi?id=141182

Reviewed by Andreas Kling.

Currently we just rewrite the entire entry after revalidation.

* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::decodeStorageEntry):

    Include the strorage entry to the cache response so we can more easily update it.

(WebKit::NetworkCache::update):

    Call the storage update function with the new and the exisiting storage entry.

* NetworkProcess/cache/NetworkCache.h:
* NetworkProcess/cache/NetworkCacheStorage.h:
* NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
(WebKit::openFileForKey):

    Added an option for opening a file for writing without creating a new one.
    Renamed for clarity.

(WebKit::encodeEntryHeader):

    Separate header encoding to a function.

(WebKit::encodeEntry):
(WebKit::NetworkCacheStorage::dispatchRetrieveOperation):
(WebKit::NetworkCacheStorage::store):
(WebKit::NetworkCacheStorage::update):

    New update function.
    If the page-rounded header size would stay unchanged we can just write the new header over the old one.
    In the unlikely event it doesn't we rewrite the whole thing.

(WebKit::createIOChannelForKey): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCachecpp">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheh">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageh">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageCocoamm">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (179545 => 179546)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-02-03 09:48:01 UTC (rev 179545)
+++ trunk/Source/WebKit2/ChangeLog        2015-02-03 12:41:52 UTC (rev 179546)
</span><span class="lines">@@ -1,3 +1,44 @@
</span><ins>+2015-02-02  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Update cache header after revalidation without rewriting the body data
+        https://bugs.webkit.org/show_bug.cgi?id=141182
+
+        Reviewed by Andreas Kling.
+
+        Currently we just rewrite the entire entry after revalidation.
+
+        * NetworkProcess/cache/NetworkCache.cpp:
+        (WebKit::decodeStorageEntry):
+
+            Include the strorage entry to the cache response so we can more easily update it.
+
+        (WebKit::NetworkCache::update):
+
+            Call the storage update function with the new and the exisiting storage entry.
+
+        * NetworkProcess/cache/NetworkCache.h:
+        * NetworkProcess/cache/NetworkCacheStorage.h:
+        * NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
+        (WebKit::openFileForKey):
+
+            Added an option for opening a file for writing without creating a new one.
+            Renamed for clarity.
+
+        (WebKit::encodeEntryHeader):
+
+            Separate header encoding to a function.
+
+        (WebKit::encodeEntry):
+        (WebKit::NetworkCacheStorage::dispatchRetrieveOperation):
+        (WebKit::NetworkCacheStorage::store):
+        (WebKit::NetworkCacheStorage::update):
+
+            New update function.
+            If the page-rounded header size would stay unchanged we can just write the new header over the old one.
+            In the unlikely event it doesn't we rewrite the whole thing.
+
+        (WebKit::createIOChannelForKey): Deleted.
+
</ins><span class="cx"> 2015-02-02  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r179540.
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCachecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp (179545 => 179546)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp        2015-02-03 09:48:01 UTC (rev 179545)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp        2015-02-03 12:41:52 UTC (rev 179546)
</span><span class="lines">@@ -167,6 +167,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     auto entry = std::make_unique&lt;NetworkCache::Entry&gt;();
</span><ins>+    entry-&gt;storageEntry = storageEntry;
</ins><span class="cx">     entry-&gt;needsRevalidation = needsRevalidation;
</span><span class="cx"> 
</span><span class="cx">     cachedResponse.setSource(needsRevalidation ? WebCore::ResourceResponse::Source::DiskCacheAfterValidation : WebCore::ResourceResponse::Source::DiskCache);
</span><span class="lines">@@ -316,11 +317,10 @@
</span><span class="cx">     WebCore::ResourceResponse response = entry.response;
</span><span class="cx">     WebCore::updateResponseHeadersAfterRevalidation(response, validatingResponse);
</span><span class="cx"> 
</span><del>-    // FIXME: This rewrites the entire resource instead of just the header.
</del><span class="cx">     auto key = makeCacheKey(originalRequest);
</span><del>-    auto storageEntry = encodeStorageEntry(originalRequest, response, entry.buffer);
</del><ins>+    auto updateEntry = encodeStorageEntry(originalRequest, response, entry.buffer);
</ins><span class="cx"> 
</span><del>-    m_storage-&gt;store(key, storageEntry, [](bool success) {
</del><ins>+    m_storage-&gt;update(key, updateEntry, entry.storageEntry, [](bool success) {
</ins><span class="cx">         LOG(NetworkCache, &quot;(NetworkProcess) updated, success=%d&quot;, success);
</span><span class="cx">     });
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h (179545 => 179546)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h        2015-02-03 09:48:01 UTC (rev 179545)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h        2015-02-03 12:41:52 UTC (rev 179546)
</span><span class="lines">@@ -53,6 +53,7 @@
</span><span class="cx">     bool isEnabled() const { return !!m_storage; }
</span><span class="cx"> 
</span><span class="cx">     struct Entry {
</span><ins>+        NetworkCacheStorage::Entry storageEntry;
</ins><span class="cx">         WebCore::ResourceResponse response;
</span><span class="cx">         RefPtr&lt;WebCore::SharedBuffer&gt; buffer;
</span><span class="cx"> #if ENABLE(SHAREABLE_RESOURCE)
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h (179545 => 179546)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h        2015-02-03 09:48:01 UTC (rev 179545)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h        2015-02-03 12:41:52 UTC (rev 179546)
</span><span class="lines">@@ -134,6 +134,7 @@
</span><span class="cx">     // This may call completion handler synchronously on failure.
</span><span class="cx">     void retrieve(const NetworkCacheKey&amp;, unsigned priority, std::function&lt;bool (std::unique_ptr&lt;Entry&gt;)&gt;);
</span><span class="cx">     void store(const NetworkCacheKey&amp;, const Entry&amp;, std::function&lt;void (bool success)&gt;);
</span><ins>+    void update(const NetworkCacheKey&amp;, const Entry&amp; updateEntry, const Entry&amp; existingEntry, std::function&lt;void (bool success)&gt;);
</ins><span class="cx"> 
</span><span class="cx">     void setMaximumSize(size_t);
</span><span class="cx">     void clear();
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageCocoamm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm (179545 => 179546)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm        2015-02-03 09:48:01 UTC (rev 179545)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm        2015-02-03 12:41:52 UTC (rev 179546)
</span><span class="lines">@@ -161,19 +161,23 @@
</span><span class="cx">     return WebCore::pathByAppendingComponent(directoryPathForKey(key, cachePath), key.hashAsString());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-enum class IOChannelType { Read, Write };
-static DispatchPtr&lt;dispatch_io_t&gt; createIOChannelForKey(const NetworkCacheKey&amp; key, IOChannelType type, const String&amp; cachePath, int&amp; fd)
</del><ins>+enum class FileOpenType { Read, Write, Create };
+static DispatchPtr&lt;dispatch_io_t&gt; openFileForKey(const NetworkCacheKey&amp; key, FileOpenType type, const String&amp; cachePath, int&amp; fd)
</ins><span class="cx"> {
</span><span class="cx">     int oflag;
</span><span class="cx">     mode_t mode;
</span><span class="cx"> 
</span><span class="cx">     switch (type) {
</span><del>-    case IOChannelType::Write:
</del><ins>+    case FileOpenType::Create:
</ins><span class="cx">         oflag = O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK;
</span><span class="cx">         mode = S_IRUSR | S_IWUSR;
</span><span class="cx">         WebCore::makeAllDirectories(directoryPathForKey(key, cachePath));
</span><span class="cx">         break;
</span><del>-    case IOChannelType::Read:
</del><ins>+    case FileOpenType::Write:
+        oflag = O_WRONLY | O_NONBLOCK;
+        mode = S_IRUSR | S_IWUSR;
+        break;
+    case FileOpenType::Read:
</ins><span class="cx">         oflag = O_RDONLY | O_NONBLOCK;
</span><span class="cx">         mode = 0;
</span><span class="cx">     }
</span><span class="lines">@@ -311,7 +315,7 @@
</span><span class="cx">     return adoptDispatch(dispatch_data_create(encoder.buffer(), encoder.bufferSize(), nullptr, DISPATCH_DATA_DESTRUCTOR_DEFAULT));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static DispatchPtr&lt;dispatch_data_t&gt; encodeEntry(const NetworkCacheKey&amp; key, const NetworkCacheStorage::Entry&amp; entry)
</del><ins>+static DispatchPtr&lt;dispatch_data_t&gt; encodeEntryHeader(const NetworkCacheKey&amp; key, const NetworkCacheStorage::Entry&amp; entry)
</ins><span class="cx"> {
</span><span class="cx">     EntryMetaData metaData(key);
</span><span class="cx">     metaData.timeStamp = entry.timeStamp;
</span><span class="lines">@@ -329,10 +333,13 @@
</span><span class="cx">     size_t dataOffset = round_page(headerSize);
</span><span class="cx">     Vector&lt;uint8_t, 4096&gt; filler(dataOffset - headerSize, 0);
</span><span class="cx">     auto alignmentData = adoptDispatch(dispatch_data_create(filler.data(), filler.size(), nullptr, DISPATCH_DATA_DESTRUCTOR_DEFAULT));
</span><ins>+    return adoptDispatch(dispatch_data_create_concat(headerData.get(), alignmentData.get()));
+}
</ins><span class="cx"> 
</span><del>-    auto headerWithAlignmentData = adoptDispatch(dispatch_data_create_concat(headerData.get(), alignmentData.get()));
-
-    return adoptDispatch(dispatch_data_create_concat(headerWithAlignmentData.get(), entry.body.dispatchData()));
</del><ins>+static DispatchPtr&lt;dispatch_data_t&gt; encodeEntry(const NetworkCacheKey&amp; key, const NetworkCacheStorage::Entry&amp; entry)
+{
+    auto encodedHeader = encodeEntryHeader(key, entry);
+    return adoptDispatch(dispatch_data_create_concat(encodedHeader.get(), entry.body.dispatchData()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void NetworkCacheStorage::removeEntry(const NetworkCacheKey&amp; key)
</span><span class="lines">@@ -360,7 +367,7 @@
</span><span class="cx">     StringCapture cachePathCapture(m_directoryPath);
</span><span class="cx">     dispatch_async(m_ioQueue.get(), [this, retrieve, cachePathCapture] {
</span><span class="cx">         int fd;
</span><del>-        auto channel = createIOChannelForKey(retrieve.key, IOChannelType::Read, cachePathCapture.string(), fd);
</del><ins>+        auto channel = openFileForKey(retrieve.key, FileOpenType::Read, cachePathCapture.string(), fd);
</ins><span class="cx"> 
</span><span class="cx">         bool didCallCompletionHandler = false;
</span><span class="cx">         dispatch_io_read(channel.get(), 0, std::numeric_limits&lt;size_t&gt;::max(), dispatch_get_main_queue(), [this, fd, retrieve, didCallCompletionHandler](bool done, dispatch_data_t fileData, int error) mutable {
</span><span class="lines">@@ -428,8 +435,9 @@
</span><span class="cx">     StringCapture cachePathCapture(m_directoryPath);
</span><span class="cx">     dispatch_async(m_backgroundIOQueue.get(), [this, key, entry, cachePathCapture, completionHandler] {
</span><span class="cx">         auto data = encodeEntry(key, entry);
</span><ins>+
</ins><span class="cx">         int fd;
</span><del>-        auto channel = createIOChannelForKey(key, IOChannelType::Write, cachePathCapture.string(), fd);
</del><ins>+        auto channel = openFileForKey(key, FileOpenType::Create, cachePathCapture.string(), fd);
</ins><span class="cx">         dispatch_io_write(channel.get(), 0, data.get(), dispatch_get_main_queue(), [this, key, completionHandler](bool done, dispatch_data_t, int error) {
</span><span class="cx">             ASSERT_UNUSED(done, done);
</span><span class="cx">             LOG(NetworkCacheStorage, &quot;(NetworkProcess) write complete error=%d&quot;, error);
</span><span class="lines">@@ -443,6 +451,45 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void NetworkCacheStorage::update(const NetworkCacheKey&amp; key, const Entry&amp; updateEntry, const Entry&amp; existingEntry, std::function&lt;void (bool success)&gt; completionHandler)
+{
+    ASSERT(RunLoop::isMain());
+
+    if (!m_keyFilter.mayContain(key.hash())) {
+        LOG(NetworkCacheStorage, &quot;(NetworkProcess) existing entry not found, storing full entry&quot;);
+        store(key, updateEntry, completionHandler);
+        return;
+    }
+
+    // Try to update the header of an existing entry.
+    StringCapture cachePathCapture(m_directoryPath);
+    dispatch_async(m_backgroundIOQueue.get(), [this, key, updateEntry, existingEntry, cachePathCapture, completionHandler] {
+        auto headerData = encodeEntryHeader(key, updateEntry);
+        auto existingHeaderData = encodeEntryHeader(key, existingEntry);
+
+        bool pageRoundedHeaderSizeChanged = dispatch_data_get_size(headerData.get()) != dispatch_data_get_size(existingHeaderData.get());
+        if (pageRoundedHeaderSizeChanged) {
+            LOG(NetworkCacheStorage, &quot;(NetworkProcess) page-rounded header size changed, storing full entry&quot;);
+            dispatch_async(dispatch_get_main_queue(), [this, key, updateEntry, completionHandler] {
+                store(key, updateEntry, completionHandler);
+            });
+            return;
+        }
+
+        int fd;
+        auto channel = openFileForKey(key, FileOpenType::Write, cachePathCapture.string(), fd);
+        dispatch_io_write(channel.get(), 0, headerData.get(), dispatch_get_main_queue(), [this, key, completionHandler](bool done, dispatch_data_t, int error) {
+            ASSERT_UNUSED(done, done);
+            LOG(NetworkCacheStorage, &quot;(NetworkProcess) update complete error=%d&quot;, error);
+
+            if (error)
+                removeEntry(key);
+
+            completionHandler(!error);
+        });
+    });
+}
+
</ins><span class="cx"> void NetworkCacheStorage::setMaximumSize(size_t size)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(RunLoop::isMain());
</span></span></pre>
</div>
</div>

</body>
</html>