[Mac] Fix inefficiencies in ResourceResponse::platformLazyInit(InitLevel)
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Oct 2014 00:44:36 +0000 (00:44 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Oct 2014 00:44:36 +0000 (00:44 +0000)
https://bugs.webkit.org/show_bug.cgi?id=137721

Reviewed by Geoffrey Garen.

There were several inefficiencies with the Mac implementation of
ResourceResponse::platformLazyInit(InitLevel):
1. We end up initializing uncommon fields even if called with
   'CommonFieldsOnly' initLevel.
2. If called with 'AllFields' initLevel (and if currently
   uninitialized), we end up populating m_httpHeaderFields twice, once
   with only the common headers, then a second time with ALL the
   headers. We can skip the common-header case in this case to avoid
   wasting CPU time.

This patch addresses both inefficiencies and cleans up the code a
little bit to reduce variable scope and to use fast enumeration of
header names.

As a result, we spend almost twice as little time in platformLazyInit()
when loading msn.com (~30ms -> ~18ms).

No new tests, no behavior change.

* platform/network/mac/ResourceResponseMac.mm:
(WebCore::ResourceResponse::platformLazyInit):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@174717 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/network/mac/ResourceResponseMac.mm

index f7e786a..91d5af7 100644 (file)
@@ -1,3 +1,32 @@
+2014-10-14  Chris Dumez  <cdumez@apple.com>
+
+        [Mac] Fix inefficiencies in ResourceResponse::platformLazyInit(InitLevel)
+        https://bugs.webkit.org/show_bug.cgi?id=137721
+
+        Reviewed by Geoffrey Garen.
+
+        There were several inefficiencies with the Mac implementation of
+        ResourceResponse::platformLazyInit(InitLevel):
+        1. We end up initializing uncommon fields even if called with
+           'CommonFieldsOnly' initLevel.
+        2. If called with 'AllFields' initLevel (and if currently
+           uninitialized), we end up populating m_httpHeaderFields twice, once
+           with only the common headers, then a second time with ALL the
+           headers. We can skip the common-header case in this case to avoid
+           wasting CPU time.
+
+        This patch addresses both inefficiencies and cleans up the code a
+        little bit to reduce variable scope and to use fast enumeration of
+        header names.
+
+        As a result, we spend almost twice as little time in platformLazyInit()
+        when loading msn.com (~30ms -> ~18ms).
+
+        No new tests, no behavior change.
+
+        * platform/network/mac/ResourceResponseMac.mm:
+        (WebCore::ResourceResponse::platformLazyInit):
+
 2014-10-14  Zalan Bujtas  <zalan@apple.com>
 
         REGRESSION (Safari 7.1/8.0): Border-radius and overflow hidden renders incorrectly.
index 53f7f00..4e97a35 100644 (file)
@@ -135,12 +135,13 @@ void ResourceResponse::platformLazyInit(InitLevel initLevel)
             NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)m_nsResponse.get();
 
             m_httpStatusCode = [httpResponse statusCode];
-
-            NSDictionary *headers = [httpResponse allHeaderFields];
             
-            for (unsigned i = 0; i < WTF_ARRAY_LENGTH(commonHeaderFields); ++i) {
-                if (NSString* headerValue = [headers objectForKey:commonHeaderFields[i]])
-                    m_httpHeaderFields.set(String(commonHeaderFields[i]), headerValue);
+            if (initLevel < AllFields) {
+                NSDictionary *headers = [httpResponse allHeaderFields];
+                for (unsigned i = 0; i < WTF_ARRAY_LENGTH(commonHeaderFields); ++i) {
+                    if (NSString* headerValue = [headers objectForKey:commonHeaderFields[i]])
+                        m_httpHeaderFields.set(String(commonHeaderFields[i]), headerValue);
+                }
             }
         } else
             m_httpStatusCode = 0;
@@ -148,21 +149,18 @@ void ResourceResponse::platformLazyInit(InitLevel initLevel)
         [pool drain];
     }
 
-    if (m_initLevel < AllFields) {
+    if (m_initLevel < AllFields && initLevel == AllFields) {
         if ([m_nsResponse.get() isKindOfClass:[NSHTTPURLResponse class]]) {
             NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
 
             NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)m_nsResponse.get();
-
-            RetainPtr<NSString> httpStatusLine = adoptNS(wkCopyNSURLResponseStatusLine(m_nsResponse.get()));
-            if (httpStatusLine)
+            if (RetainPtr<NSString> httpStatusLine = adoptNS(wkCopyNSURLResponseStatusLine(httpResponse)))
                 m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(httpStatusLine.get());
             else
-                m_httpStatusText = "OK";
+                m_httpStatusText = AtomicString("OK", AtomicString::ConstructFromLiteral);
 
             NSDictionary *headers = [httpResponse allHeaderFields];
-            NSEnumerator *e = [headers keyEnumerator];
-            while (NSString *name = [e nextObject])
+            for (NSString *name in headers)
                 m_httpHeaderFields.set(String(name), [headers objectForKey:name]);
             
             [pool drain];