Title: [260202] trunk/Source/WTF
Revision
260202
Author
dba...@webkit.org
Date
2020-04-16 11:30:30 -0700 (Thu, 16 Apr 2020)

Log Message

Clean up VectorCocoa createNSArray overloads and add documentation for createNSArray taking a map function
https://bugs.webkit.org/show_bug.cgi?id=210610

Reviewed by Darin Adler.

Remove unnecessary local variable. Rename "map" type in createNSArray function for clarity
and add documentation for createNSArray function that takes a map function.

I was tempted, but decided not to write the createNSArray function that does not take a
map function in terms of the one that did. With the "right" optimization settings the
compiler should emit the same code, but I didn't verify this. So, I didn't do it.

While I am here, update  makeVector() to call shrinkToFit() to reduce the memory footprint
of the returned vector should it end of having less entries, due to filtering, than its
initial capacity.

* wtf/cocoa/VectorCocoa.h:
(WTF::createNSArray): See above description.
(WTF::makeVector): Shrink to fit before returning the vector. See above for more details.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (260201 => 260202)


--- trunk/Source/WTF/ChangeLog	2020-04-16 18:11:26 UTC (rev 260201)
+++ trunk/Source/WTF/ChangeLog	2020-04-16 18:30:30 UTC (rev 260202)
@@ -1,5 +1,27 @@
 2020-04-16  Daniel Bates  <daba...@apple.com>
 
+        Clean up VectorCocoa createNSArray overloads and add documentation for createNSArray taking a map function
+        https://bugs.webkit.org/show_bug.cgi?id=210610
+
+        Reviewed by Darin Adler.
+
+        Remove unnecessary local variable. Rename "map" type in createNSArray function for clarity
+        and add documentation for createNSArray function that takes a map function.
+
+        I was tempted, but decided not to write the createNSArray function that does not take a
+        map function in terms of the one that did. With the "right" optimization settings the
+        compiler should emit the same code, but I didn't verify this. So, I didn't do it.
+
+        While I am here, update  makeVector() to call shrinkToFit() to reduce the memory footprint
+        of the returned vector should it end of having less entries, due to filtering, than its
+        initial capacity.
+
+        * wtf/cocoa/VectorCocoa.h:
+        (WTF::createNSArray): See above description.
+        (WTF::makeVector): Shrink to fit before returning the vector. See above for more details.
+
+2020-04-16  Daniel Bates  <daba...@apple.com>
+
         Move -_requestTextInputContextsInRect to WKContentView to simplify implementation
         https://bugs.webkit.org/show_bug.cgi?id=210398
         <rdar://problem/61656931>

Modified: trunk/Source/WTF/wtf/cocoa/VectorCocoa.h (260201 => 260202)


--- trunk/Source/WTF/wtf/cocoa/VectorCocoa.h	2020-04-16 18:11:26 UTC (rev 260201)
+++ trunk/Source/WTF/wtf/cocoa/VectorCocoa.h	2020-04-16 18:30:30 UTC (rev 260202)
@@ -47,26 +47,28 @@
 //    Optional<VectorElementType> makeVectorElement(const VectorElementType*, id arrayElement);
 
 template<typename VectorType> RetainPtr<NSArray> createNSArray(const VectorType&);
-template<typename VectorType, typename MapFunction> RetainPtr<NSArray> createNSArray(const VectorType&, MapFunction);
 template<typename VectorElementType> Vector<VectorElementType> makeVector(NSArray *);
 
+// This overload of createNSArray takes a function to map each vector element to an Objective-C object.
+// The map function has the same interface as the makeNSArrayElement function above, but can be any
+// function including a lambda, a function-like object, or Function<>.
+template<typename VectorType, typename MapFunctionType> RetainPtr<NSArray> createNSArray(const VectorType&, const MapFunctionType&);
+
 // Implementation details of the function templates above.
 
 template<typename VectorType> RetainPtr<NSArray> createNSArray(const VectorType& vector)
 {
-    auto size = vector.size();
-    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:size]);
+    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:vector.size()]);
     for (auto& element : vector)
         [array addObject:getPtr(makeNSArrayElement(element))];
     return array;
 }
 
-template<typename VectorType, typename MapFunction> RetainPtr<NSArray> createNSArray(const VectorType& vector, MapFunction mapFunction)
+template<typename VectorType, typename MapFunctionType> RetainPtr<NSArray> createNSArray(const VectorType& vector, const MapFunctionType& function)
 {
-    auto size = vector.size();
-    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:size]);
+    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:vector.size()]);
     for (auto& element : vector)
-        [array addObject:getPtr(mapFunction(element))];
+        [array addObject:getPtr(function(element))];
     return array;
 }
 
@@ -79,6 +81,7 @@
         if (auto vectorElement = makeVectorElement(typedNull, element))
             vector.uncheckedAppend(WTFMove(*vectorElement));
     }
+    vector.shrinkToFit();
     return vector;
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to