Title: [175186] trunk/Source/WebCore
Revision
175186
Author
[email protected]
Date
2014-10-24 15:24:30 -0700 (Fri, 24 Oct 2014)

Log Message

[Mac] Use modern loops in ResourceRequestCocoa.mm
https://bugs.webkit.org/show_bug.cgi?id=138052

Reviewed by Darin Adler.

Use modern loops in ResourceRequestCocoa.mm and leverage Objective-C's
fast enumeration.

No new tests, no behavior change.

* platform/network/cocoa/ResourceRequestCocoa.mm:
(WebCore::ResourceRequest::doUpdateResourceRequest):
- Use NSDictionary's block-based enumeration as we enumerate both keys
  and values and this is faster than using fast enumeration of the keys
  then calling [NSDictionary objectForKey:]
- Use Objective C's fast enumeration for
  contentDispositionEncodingFallbackArray as this is faster and shorter
  than index based access. Also use reserveCapacity() /
  uncheckedAppend() for m_responseContentDispositionEncodingFallbackArray
  as we know how many elements were are going to append in the common
  case.

(WebCore::ResourceRequest::doUpdatePlatformRequest):
- Use fast enumeration for [nsRequest allHTTPHeaderFields] dictionary
  keys instead of calling [NSDictionary allKeys] and then doing
  index-based iteration, to avoid copying the keys to a new array.
  Also do a forward enumeration instead of a reverse one. I don't see
  why a backward enumeration would be necessary here. We are not
  modifying the NSDictionary we are iterating over as
  [nsRequest allHTTPHeaderFields] makes a copy.
- Use a modern C++ loop for
  m_responseContentDispositionEncodingFallbackArray.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (175185 => 175186)


--- trunk/Source/WebCore/ChangeLog	2014-10-24 22:01:00 UTC (rev 175185)
+++ trunk/Source/WebCore/ChangeLog	2014-10-24 22:24:30 UTC (rev 175186)
@@ -1,3 +1,38 @@
+2014-10-24  Chris Dumez  <[email protected]>
+
+        [Mac] Use modern loops in ResourceRequestCocoa.mm
+        https://bugs.webkit.org/show_bug.cgi?id=138052
+
+        Reviewed by Darin Adler.
+
+        Use modern loops in ResourceRequestCocoa.mm and leverage Objective-C's
+        fast enumeration.
+
+        No new tests, no behavior change.
+
+        * platform/network/cocoa/ResourceRequestCocoa.mm:
+        (WebCore::ResourceRequest::doUpdateResourceRequest):
+        - Use NSDictionary's block-based enumeration as we enumerate both keys
+          and values and this is faster than using fast enumeration of the keys
+          then calling [NSDictionary objectForKey:]
+        - Use Objective C's fast enumeration for
+          contentDispositionEncodingFallbackArray as this is faster and shorter
+          than index based access. Also use reserveCapacity() /
+          uncheckedAppend() for m_responseContentDispositionEncodingFallbackArray
+          as we know how many elements were are going to append in the common
+          case.
+
+        (WebCore::ResourceRequest::doUpdatePlatformRequest):
+        - Use fast enumeration for [nsRequest allHTTPHeaderFields] dictionary
+          keys instead of calling [NSDictionary allKeys] and then doing
+          index-based iteration, to avoid copying the keys to a new array.
+          Also do a forward enumeration instead of a reverse one. I don't see
+          why a backward enumeration would be necessary here. We are not
+          modifying the NSDictionary we are iterating over as
+          [nsRequest allHTTPHeaderFields] makes a copy.
+        - Use a modern C++ loop for
+          m_responseContentDispositionEncodingFallbackArray.
+
 2014-10-24  Zalan Bujtas  <[email protected]>
 
         Replace INT_MIN/MAX / kFixedPointDenominator with intMin/MaxForLayoutUnit.

Modified: trunk/Source/WebCore/platform/network/cocoa/ResourceRequestCocoa.mm (175185 => 175186)


--- trunk/Source/WebCore/platform/network/cocoa/ResourceRequestCocoa.mm	2014-10-24 22:01:00 UTC (rev 175185)
+++ trunk/Source/WebCore/platform/network/cocoa/ResourceRequestCocoa.mm	2014-10-24 22:24:30 UTC (rev 175186)
@@ -84,20 +84,18 @@
             m_priority = priority;
     }
 
-    NSDictionary *headers = [m_nsRequest.get() allHTTPHeaderFields];
-    NSEnumerator *e = [headers keyEnumerator];
-    NSString *name;
     m_httpHeaderFields.clear();
-    while ((name = [e nextObject]))
-        m_httpHeaderFields.set(String(name), [headers objectForKey:name]);
+    [[m_nsRequest allHTTPHeaderFields] enumerateKeysAndObjectsUsingBlock: ^(NSString *name, NSString *value, BOOL *) {
+        m_httpHeaderFields.set(name, value);
+    }];
 
     m_responseContentDispositionEncodingFallbackArray.clear();
     NSArray *encodingFallbacks = [m_nsRequest.get() contentDispositionEncodingFallbackArray];
-    NSUInteger count = [encodingFallbacks count];
-    for (NSUInteger i = 0; i < count; ++i) {
-        CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding([(NSNumber *)[encodingFallbacks objectAtIndex:i] unsignedLongValue]);
+    m_responseContentDispositionEncodingFallbackArray.reserveCapacity([encodingFallbacks count]);
+    for (NSNumber *encodingFallback in [m_nsRequest contentDispositionEncodingFallbackArray]) {
+        CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding([encodingFallback unsignedLongValue]);
         if (encoding != kCFStringEncodingInvalidId)
-            m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding));
+            m_responseContentDispositionEncodingFallbackArray.uncheckedAppend(CFStringConvertEncodingToIANACharSetName(encoding));
     }
 
 #if ENABLE(CACHE_PARTITIONING)
@@ -163,18 +161,14 @@
     [nsRequest setHTTPShouldHandleCookies:allowCookies()];
 
     // Cannot just use setAllHTTPHeaderFields here, because it does not remove headers.
-    NSArray *oldHeaderFieldNames = [[nsRequest allHTTPHeaderFields] allKeys];
-    for (unsigned i = [oldHeaderFieldNames count]; i != 0; --i)
-        [nsRequest setValue:nil forHTTPHeaderField:[oldHeaderFieldNames objectAtIndex:i - 1]];
+    for (NSString *oldHeaderName in [nsRequest allHTTPHeaderFields])
+        [nsRequest setValue:nil forHTTPHeaderField:oldHeaderName];
     for (const auto& header : httpHeaderFields())
         [nsRequest setValue:header.value forHTTPHeaderField:header.key];
 
     NSMutableArray *encodingFallbacks = [NSMutableArray array];
-    unsigned count = m_responseContentDispositionEncodingFallbackArray.size();
-    for (unsigned i = 0; i != count; ++i) {
-        RetainPtr<CFStringRef> encodingName = m_responseContentDispositionEncodingFallbackArray[i].createCFString();
-        unsigned long nsEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName.get()));
-
+    for (const auto& encodingName : m_responseContentDispositionEncodingFallbackArray) {
+        CFStringEncoding nsEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName.createCFString().get()));
         if (nsEncoding != kCFStringEncodingInvalidId)
             [encodingFallbacks addObject:[NSNumber numberWithUnsignedLong:nsEncoding]];
     }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to