Title: [241220] trunk/Source/WebCore
Revision
241220
Author
[email protected]
Date
2019-02-08 16:26:40 -0800 (Fri, 08 Feb 2019)

Log Message

[Cocoa] Optimize ResourceResponse::platformLazyInit()
https://bugs.webkit.org/show_bug.cgi?id=194438

Reviewed by Alex Christensen.

Optimize ResourceResponse::platformLazyInit(). Most of the CPU time currently goes into getting the
HTTP headers from CFNetwork:
"""
Sample Count, Samples %, CPU %, Symbol
46, 0.0%, 0.0%, WebCore::initializeHTTPHeaders(WebCore::OnlyCommonHeaders, NSHTTPURLResponse*, WebCore::HTTPHeaderMap&) (in WebCore)
34, 0.0%, 0.0%,     HTTPHeaderDict::copyAsOrdinaryDict(__CFAllocator const*) const (in CFNetwork)
11, 0.0%, 0.0%,     CFDictionaryApplyFunction (in CoreFoundation)
"""

We currently have 2 levels of initialization: CommonFieldsOnly & AllFields. With WebKit2, most ResourceResponses get sent over IPC
and thus end up getting initialized twice, once with CommonFieldsOnly and then with AllFields.
This would cause us to call the expensive HTTPHeaderDict::copyAsOrdinaryDict() twice instead of once, simply to initialize the common
HTTP headers first and then the uncommon ones later.

This patch updates ResourceResponse::platformLazyInit() to initialize all HTTP headers at once, as soon as CommonFieldsOnly
initialization is requested, so that we no longer copy all HTTP headers twice.

* platform/network/cocoa/ResourceResponseCocoa.mm:
(WebCore::initializeHTTPHeaders):
(WebCore::ResourceResponse::platformLazyInit):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (241219 => 241220)


--- trunk/Source/WebCore/ChangeLog	2019-02-09 00:25:46 UTC (rev 241219)
+++ trunk/Source/WebCore/ChangeLog	2019-02-09 00:26:40 UTC (rev 241220)
@@ -1,3 +1,31 @@
+2019-02-08  Chris Dumez  <[email protected]>
+
+        [Cocoa] Optimize ResourceResponse::platformLazyInit()
+        https://bugs.webkit.org/show_bug.cgi?id=194438
+
+        Reviewed by Alex Christensen.
+
+        Optimize ResourceResponse::platformLazyInit(). Most of the CPU time currently goes into getting the
+        HTTP headers from CFNetwork:
+        """
+        Sample Count, Samples %, CPU %, Symbol
+        46, 0.0%, 0.0%, WebCore::initializeHTTPHeaders(WebCore::OnlyCommonHeaders, NSHTTPURLResponse*, WebCore::HTTPHeaderMap&) (in WebCore)
+        34, 0.0%, 0.0%,     HTTPHeaderDict::copyAsOrdinaryDict(__CFAllocator const*) const (in CFNetwork)
+        11, 0.0%, 0.0%,     CFDictionaryApplyFunction (in CoreFoundation)
+        """
+
+        We currently have 2 levels of initialization: CommonFieldsOnly & AllFields. With WebKit2, most ResourceResponses get sent over IPC
+        and thus end up getting initialized twice, once with CommonFieldsOnly and then with AllFields.
+        This would cause us to call the expensive HTTPHeaderDict::copyAsOrdinaryDict() twice instead of once, simply to initialize the common
+        HTTP headers first and then the uncommon ones later.
+
+        This patch updates ResourceResponse::platformLazyInit() to initialize all HTTP headers at once, as soon as CommonFieldsOnly
+        initialization is requested, so that we no longer copy all HTTP headers twice.
+
+        * platform/network/cocoa/ResourceResponseCocoa.mm:
+        (WebCore::initializeHTTPHeaders):
+        (WebCore::ResourceResponse::platformLazyInit):
+
 2019-02-08  Justin Fan  <[email protected]>
 
         [Web GPU] Build fix for MTLStorageMode availability on different Cocoa platforms

Modified: trunk/Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm (241219 => 241220)


--- trunk/Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm	2019-02-09 00:25:46 UTC (rev 241219)
+++ trunk/Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm	2019-02-09 00:26:40 UTC (rev 241220)
@@ -137,23 +137,14 @@
     return StringView(value).substring(1, length - 2).toAtomicString();
 }
 
-enum class OnlyCommonHeaders { No, Yes };
-static inline void initializeHTTPHeaders(OnlyCommonHeaders onlyCommonHeaders, NSHTTPURLResponse *httpResponse, HTTPHeaderMap& headersMap)
+static inline HTTPHeaderMap initializeHTTPHeaders(CFHTTPMessageRef messageRef)
 {
-    headersMap.clear();
-    auto messageRef = CFURLResponseGetHTTPResponse([httpResponse _CFURLResponse]);
-
     // Avoid calling [NSURLResponse allHeaderFields] to minimize copying (<rdar://problem/26778863>).
     auto headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(messageRef));
-    if (_onlyCommonHeaders_ == OnlyCommonHeaders::Yes) {
-        for (auto& commonHeader : commonHeaderFields) {
-            const void* value;
-            if (CFDictionaryGetValueIfPresent(headers.get(), commonHeader, &value))
-                headersMap.set(commonHeader, (CFStringRef) value);
-        }
-        return;
-    }
+
+    HTTPHeaderMap headersMap;
     CFDictionaryApplyFunction(headers.get(), addToHTTPHeaderMap, &headersMap);
+    return headersMap;
 }
 
 static inline AtomicString extractHTTPStatusText(CFHTTPMessageRef messageRef)
@@ -177,7 +168,7 @@
     
     @autoreleasepool {
 
-        NSHTTPURLResponse *httpResponse = [m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]] ? (NSHTTPURLResponse *)m_nsResponse.get() : nullptr;
+        auto messageRef = [m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]] ? CFURLResponseGetHTTPResponse([ (NSHTTPURLResponse *)m_nsResponse.get() _CFURLResponse]) : nullptr;
 
         if (m_initLevel < CommonFieldsOnly) {
             m_url = [m_nsResponse.get() URL];
@@ -185,18 +176,14 @@
             m_expectedContentLength = [m_nsResponse.get() expectedContentLength];
             // Stripping double quotes as a workaround for <rdar://problem/8757088>, can be removed once that is fixed.
             m_textEncodingName = stripLeadingAndTrailingDoubleQuote([m_nsResponse.get() textEncodingName]);
-            m_httpStatusCode = httpResponse ? [httpResponse statusCode] : 0;
+            m_httpStatusCode = messageRef ? CFHTTPMessageGetResponseStatusCode(messageRef) : 0;
+            if (messageRef)
+                m_httpHeaderFields = initializeHTTPHeaders(messageRef);
         }
-        if (httpResponse) {
-            if (initLevel == AllFields) {
-                auto messageRef = CFURLResponseGetHTTPResponse([httpResponse _CFURLResponse]);
-                m_httpStatusText = extractHTTPStatusText(messageRef);
-                m_httpVersion = String(adoptCF(CFHTTPMessageCopyVersion(messageRef)).get()).convertToASCIIUppercase();
-                initializeHTTPHeaders(OnlyCommonHeaders::No, httpResponse, m_httpHeaderFields);
-            } else
-                initializeHTTPHeaders(OnlyCommonHeaders::Yes, httpResponse, m_httpHeaderFields);
+        if (messageRef && initLevel == AllFields) {
+            m_httpStatusText = extractHTTPStatusText(messageRef);
+            m_httpVersion = String(adoptCF(CFHTTPMessageCopyVersion(messageRef)).get()).convertToASCIIUppercase();
         }
-
     }
 
     m_initLevel = initLevel;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to