Title: [202121] trunk/Source
Revision
202121
Author
cdu...@apple.com
Date
2016-06-15 19:59:54 -0700 (Wed, 15 Jun 2016)

Log Message

[Cocoa] Clean up / optimize ResourceResponse::platformLazyInit(InitLevel)
https://bugs.webkit.org/show_bug.cgi?id=158809

Reviewed by Darin Adler.

Source/WebCore:

Clean up / optimize ResourceResponse::platformLazyInit(InitLevel).

* platform/network/HTTPParsers.cpp:
(WebCore::extractReasonPhraseFromHTTPStatusLine):
* platform/network/HTTPParsers.h:
Have extractReasonPhraseFromHTTPStatusLine() return an AtomicString as the
Reason is stored as an AtomicString on ResourceResponse. Have the
implementation use StringView::subString()::toAtomicString().

* platform/network/cocoa/ResourceResponseCocoa.mm:
(WebCore::stripLeadingAndTrailingDoubleQuote):
Move the stripLeadingAndTrailingDoubleQuote logic from platformLazyInit()
to its own function. Have it use StringView::subString()::toAtomicString()
to avoid unnecessarily atomizing the textEncodingName that has surrounding
double-quotes.

(WebCore::initializeHTTPHeaders):
Move HTTP headers initialization to its own function for clarity.

(WebCore::extractHTTPStatusText):
Move HTTP status Text extraction to its own function for clarity.

(WebCore::ResourceResponse::platformLazyInit):
- The function is streamlined a bit because most of the logic was moved
  into separate functions.
- Drop unnecessary (initLevel >= CommonFieldsOnly) check in the first
  if case and replace with an assertion. This function is always called
  with CommonFieldsOnly or above (AllFields).
- Drop unnecessary (m_initLevel < AllFields) check in the second if
  case as this is always true. If not, we would have returned early
  at the beginning of the function when checking
  m_initLevel >= initLevel.
- Use AutodrainedPool instead of NSAutoreleasePool for convenience and have
  only 1 pool instead of 2.
- Drop unnecessary copyNSURLResponseStatusLine() function and call directly
  CFHTTPMessageCopyResponseStatusLine() since we already have a
  CFHTTPMessageRef at the call site.

Source/WTF:

Add toAtomicString() method to StringView to avoid having to call toString()
and then atomizing the String at call sites.

* wtf/text/StringView.h:
(WTF::StringView::toAtomicString):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (202120 => 202121)


--- trunk/Source/WTF/ChangeLog	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WTF/ChangeLog	2016-06-16 02:59:54 UTC (rev 202121)
@@ -1,3 +1,16 @@
+2016-06-15  Chris Dumez  <cdu...@apple.com>
+
+        [Cocoa] Clean up / optimize ResourceResponse::platformLazyInit(InitLevel)
+        https://bugs.webkit.org/show_bug.cgi?id=158809
+
+        Reviewed by Darin Adler.
+
+        Add toAtomicString() method to StringView to avoid having to call toString()
+        and then atomizing the String at call sites.
+
+        * wtf/text/StringView.h:
+        (WTF::StringView::toAtomicString):
+
 2016-06-15  Sam Weinig  <s...@webkit.org>
 
         Improve HashMap and HashSet support for Ref

Modified: trunk/Source/WTF/wtf/text/StringView.h (202120 => 202121)


--- trunk/Source/WTF/wtf/text/StringView.h	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WTF/wtf/text/StringView.h	2016-06-16 02:59:54 UTC (rev 202121)
@@ -86,6 +86,7 @@
 
     String toString() const;
     String toStringWithoutCopying() const;
+    AtomicString toAtomicString() const;
 
 #if USE(CF)
     // This function converts null strings to empty strings.
@@ -451,6 +452,13 @@
     return String(characters16(), length());
 }
 
+inline AtomicString StringView::toAtomicString() const
+{
+    if (is8Bit())
+        return AtomicString(characters8(), m_length);
+    return AtomicString(characters16(), length());
+}
+
 inline float StringView::toFloat(bool& isValid) const
 {
     if (is8Bit())

Modified: trunk/Source/WebCore/ChangeLog (202120 => 202121)


--- trunk/Source/WebCore/ChangeLog	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WebCore/ChangeLog	2016-06-16 02:59:54 UTC (rev 202121)
@@ -1,3 +1,48 @@
+2016-06-15  Chris Dumez  <cdu...@apple.com>
+
+        [Cocoa] Clean up / optimize ResourceResponse::platformLazyInit(InitLevel)
+        https://bugs.webkit.org/show_bug.cgi?id=158809
+
+        Reviewed by Darin Adler.
+
+        Clean up / optimize ResourceResponse::platformLazyInit(InitLevel).
+
+        * platform/network/HTTPParsers.cpp:
+        (WebCore::extractReasonPhraseFromHTTPStatusLine):
+        * platform/network/HTTPParsers.h:
+        Have extractReasonPhraseFromHTTPStatusLine() return an AtomicString as the
+        Reason is stored as an AtomicString on ResourceResponse. Have the
+        implementation use StringView::subString()::toAtomicString().
+
+        * platform/network/cocoa/ResourceResponseCocoa.mm:
+        (WebCore::stripLeadingAndTrailingDoubleQuote):
+        Move the stripLeadingAndTrailingDoubleQuote logic from platformLazyInit()
+        to its own function. Have it use StringView::subString()::toAtomicString()
+        to avoid unnecessarily atomizing the textEncodingName that has surrounding
+        double-quotes.
+
+        (WebCore::initializeHTTPHeaders):
+        Move HTTP headers initialization to its own function for clarity.
+
+        (WebCore::extractHTTPStatusText):
+        Move HTTP status Text extraction to its own function for clarity.
+
+        (WebCore::ResourceResponse::platformLazyInit):
+        - The function is streamlined a bit because most of the logic was moved
+          into separate functions.
+        - Drop unnecessary (initLevel >= CommonFieldsOnly) check in the first
+          if case and replace with an assertion. This function is always called
+          with CommonFieldsOnly or above (AllFields).
+        - Drop unnecessary (m_initLevel < AllFields) check in the second if
+          case as this is always true. If not, we would have returned early
+          at the beginning of the function when checking
+          m_initLevel >= initLevel.
+        - Use AutodrainedPool instead of NSAutoreleasePool for convenience and have
+          only 1 pool instead of 2.
+        - Drop unnecessary copyNSURLResponseStatusLine() function and call directly
+          CFHTTPMessageCopyResponseStatusLine() since we already have a
+          CFHTTPMessageRef at the call site.
+
 2016-06-15  Tim Horton  <timothy_hor...@apple.com>
 
         <attachment> elements jump around a lot around when subtitle text changes slightly

Modified: trunk/Source/WebCore/platform/network/HTTPParsers.cpp (202120 => 202121)


--- trunk/Source/WebCore/platform/network/HTTPParsers.cpp	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WebCore/platform/network/HTTPParsers.cpp	2016-06-16 02:59:54 UTC (rev 202121)
@@ -461,12 +461,13 @@
 }
 #endif
 
-String extractReasonPhraseFromHTTPStatusLine(const String& statusLine)
+AtomicString extractReasonPhraseFromHTTPStatusLine(const String& statusLine)
 {
-    size_t spacePos = statusLine.find(' ');
+    StringView view = statusLine;
+    size_t spacePos = view.find(' ');
     // Remove status code from the status line.
-    spacePos = statusLine.find(' ', spacePos + 1);
-    return statusLine.substring(spacePos + 1);
+    spacePos = view.find(' ', spacePos + 1);
+    return view.substring(spacePos + 1).toAtomicString();
 }
 
 XFrameOptionsDisposition parseXFrameOptionsHeader(const String& header)

Modified: trunk/Source/WebCore/platform/network/HTTPParsers.h (202120 => 202121)


--- trunk/Source/WebCore/platform/network/HTTPParsers.h	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WebCore/platform/network/HTTPParsers.h	2016-06-16 02:59:54 UTC (rev 202121)
@@ -78,7 +78,7 @@
 String extractCharsetFromMediaType(const String&); 
 void findCharsetInMediaType(const String& mediaType, unsigned int& charsetPos, unsigned int& charsetLen, unsigned int start = 0);
 XSSProtectionDisposition parseXSSProtectionHeader(const String& header, String& failureReason, unsigned& failurePosition, String& reportURL);
-String extractReasonPhraseFromHTTPStatusLine(const String&);
+AtomicString extractReasonPhraseFromHTTPStatusLine(const String&);
 XFrameOptionsDisposition parseXFrameOptionsHeader(const String&);
 
 // -1 could be set to one of the return parameters to indicate the value is not specified.

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


--- trunk/Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm	2016-06-16 02:20:04 UTC (rev 202120)
+++ trunk/Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm	2016-06-16 02:59:54 UTC (rev 202121)
@@ -33,7 +33,10 @@
 #import "WebCoreURLResponse.h"
 #import <Foundation/Foundation.h>
 #import <limits>
+#import <wtf/AutodrainedPool.h>
+#import <wtf/NeverDestroyed.h>
 #import <wtf/StdLibExtras.h>
+#import <wtf/text/StringView.h>
 
 namespace WebCore {
 
@@ -145,89 +148,80 @@
         initNSURLResponse();
     return m_nsResponse.get();
 }
-    
-static NSString *copyNSURLResponseStatusLine(NSURLResponse *response)
+
+static void addToHTTPHeaderMap(const void* key, const void* value, void* context)
 {
-    CFURLResponseRef cfResponse = [response _CFURLResponse];
-    if (!cfResponse)
-        return nil;
+    HTTPHeaderMap* httpHeaderMap = (HTTPHeaderMap*)context;
+    httpHeaderMap->set((CFStringRef)key, (CFStringRef)value);
+}
 
-    CFHTTPMessageRef cfHTTPMessage = CFURLResponseGetHTTPResponse(cfResponse);
-    if (!cfHTTPMessage)
-        return nil;
+static inline AtomicString stripLeadingAndTrailingDoubleQuote(const String& value)
+{
+    unsigned length = value.length();
+    if (length < 2 || value[0u] != '"' || value[length - 1] != '"')
+        return value;
 
-    return (NSString *)CFHTTPMessageCopyResponseStatusLine(cfHTTPMessage);
+    return StringView(value).substring(1, length - 2).toAtomicString();
 }
 
-static void addToHTTPHeaderMap(const void* key, const void* value, void* context)
+enum class OnlyCommonHeaders { No, Yes };
+static inline void initializeHTTPHeaders(OnlyCommonHeaders onlyCommonHeaders, NSHTTPURLResponse *httpResponse, HTTPHeaderMap& headersMap)
 {
-    HTTPHeaderMap* httpHeaderMap = (HTTPHeaderMap*)context;
-    httpHeaderMap->set((CFStringRef)key, (CFStringRef)value);
+    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;
+    }
+    CFDictionaryApplyFunction(headers.get(), addToHTTPHeaderMap, &headersMap);
 }
 
+static inline AtomicString extractHTTPStatusText(CFHTTPMessageRef messageRef)
+{
+    if (auto httpStatusLine = adoptCF(CFHTTPMessageCopyResponseStatusLine(messageRef)))
+        return extractReasonPhraseFromHTTPStatusLine(httpStatusLine.get());
+
+    static NeverDestroyed<AtomicString> defaultStatusText("OK", AtomicString::ConstructFromLiteral);
+    return defaultStatusText;
+}
+
 void ResourceResponse::platformLazyInit(InitLevel initLevel)
 {
+    ASSERT(initLevel >= CommonFieldsOnly);
+
     if (m_initLevel >= initLevel)
         return;
 
     if (m_isNull || !m_nsResponse)
         return;
     
-    if (m_initLevel < CommonFieldsOnly && initLevel >= CommonFieldsOnly) {
-        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+    AutodrainedPool pool;
 
-        m_httpHeaderFields.clear();
+    NSHTTPURLResponse *httpResponse = [m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]] ? (NSHTTPURLResponse *)m_nsResponse.get() : nullptr;
+
+    if (m_initLevel < CommonFieldsOnly) {
         m_url = [m_nsResponse.get() URL];
         m_mimeType = [m_nsResponse.get() MIMEType];
         m_expectedContentLength = [m_nsResponse.get() expectedContentLength];
-        m_textEncodingName = [m_nsResponse.get() textEncodingName];
-
-        // Workaround for <rdar://problem/8757088>, can be removed once that is fixed.
-        unsigned textEncodingNameLength = m_textEncodingName.length();
-        if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"')
-            m_textEncodingName = m_textEncodingName.string().substring(1, textEncodingNameLength - 2);
-
-        if ([m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]]) {
-            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)m_nsResponse.get();
-
-            m_httpStatusCode = [httpResponse statusCode];
-            
-            if (initLevel < AllFields) {
-                CFHTTPMessageRef messageRef = CFURLResponseGetHTTPResponse([httpResponse _CFURLResponse]);
-
-                // Avoid calling [NSURLResponse allHeaderFields] to minimize copying (<rdar://problem/26778863>).
-                RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(messageRef));
-                for (auto& commonHeader : commonHeaderFields) {
-                    CFStringRef value;
-                    if (CFDictionaryGetValueIfPresent(headers.get(), commonHeader, (const void **)&value))
-                        m_httpHeaderFields.set(commonHeader, value);
-                }
-            }
-        } else
-            m_httpStatusCode = 0;
-        
-        [pool drain];
+        // 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;
     }
-
-    if (m_initLevel < AllFields && initLevel == AllFields) {
-        if ([m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]]) {
-            NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
-
-            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)m_nsResponse.get();
-            if (RetainPtr<NSString> httpStatusLine = adoptNS(copyNSURLResponseStatusLine(httpResponse)))
-                m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(httpStatusLine.get());
-            else
-                m_httpStatusText = AtomicString("OK", AtomicString::ConstructFromLiteral);
-
-            CFHTTPMessageRef messageRef = CFURLResponseGetHTTPResponse([httpResponse _CFURLResponse]);
+    if (httpResponse) {
+        if (initLevel == AllFields) {
+            auto messageRef = CFURLResponseGetHTTPResponse([httpResponse _CFURLResponse]);
+            m_httpStatusText = extractHTTPStatusText(messageRef);
             m_httpVersion = String(adoptCF(CFHTTPMessageCopyVersion(messageRef)).get()).convertToASCIIUppercase();
-
-            // Avoid calling [NSURLResponse allHeaderFields] to minimize copying (<rdar://problem/26778863>).
-            RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(messageRef));
-            CFDictionaryApplyFunction(headers.get(), addToHTTPHeaderMap, &m_httpHeaderFields);
-            
-            [pool drain];
-        }
+            initializeHTTPHeaders(OnlyCommonHeaders::No, httpResponse, m_httpHeaderFields);
+        } else
+            initializeHTTPHeaders(OnlyCommonHeaders::Yes, httpResponse, m_httpHeaderFields);
     }
 
     m_initLevel = initLevel;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to