Title: [275658] trunk
Revision
275658
Author
[email protected]
Date
2021-04-08 09:14:00 -0700 (Thu, 08 Apr 2021)

Log Message

Copy-constructed Vectors should not have excess capacity
https://bugs.webkit.org/show_bug.cgi?id=224313

Reviewed by Darin Adler.

Source/WTF:

When copy-constructing a Vector<>, set the capacity to the size of the input.
This avoids wasted vector capacity at call sites that dynamically build vectors
and then copy them into read-only caches.

* wtf/Vector.h:
(WTF::Malloc>::Vector):

Tools:

New API tests for testing capacity on copy-construct, with tests for copy-constructing
with various configs of inline capacity.

* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (275657 => 275658)


--- trunk/Source/WTF/ChangeLog	2021-04-08 13:16:19 UTC (rev 275657)
+++ trunk/Source/WTF/ChangeLog	2021-04-08 16:14:00 UTC (rev 275658)
@@ -1,3 +1,17 @@
+2021-04-08  Simon Fraser  <[email protected]>
+
+        Copy-constructed Vectors should not have excess capacity
+        https://bugs.webkit.org/show_bug.cgi?id=224313
+
+        Reviewed by Darin Adler.
+
+        When copy-constructing a Vector<>, set the capacity to the size of the input.
+        This avoids wasted vector capacity at call sites that dynamically build vectors
+        and then copy them into read-only caches.
+
+        * wtf/Vector.h:
+        (WTF::Malloc>::Vector):
+
 2021-04-02  Darin Adler  <[email protected]>
 
         Use Hasher more, remove IntegerHasher, fix hashing-related mistakes

Modified: trunk/Source/WTF/wtf/Vector.h (275657 => 275658)


--- trunk/Source/WTF/wtf/Vector.h	2021-04-08 13:16:19 UTC (rev 275657)
+++ trunk/Source/WTF/wtf/Vector.h	2021-04-08 16:14:00 UTC (rev 275658)
@@ -891,7 +891,7 @@
 
 template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
 Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::Vector(const Vector& other)
-    : Base(other.capacity(), other.size())
+    : Base(other.size(), other.size())
 {
     asanSetInitialBufferSizeTo(other.size());
 
@@ -902,7 +902,7 @@
 template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
 template<size_t otherCapacity, typename otherOverflowBehaviour, size_t otherMinimumCapacity, typename OtherMalloc>
 Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::Vector(const Vector<T, otherCapacity, otherOverflowBehaviour, otherMinimumCapacity, OtherMalloc>& other)
-    : Base(other.capacity(), other.size())
+    : Base(other.size(), other.size())
 {
     asanSetInitialBufferSizeTo(other.size());
 

Modified: trunk/Tools/ChangeLog (275657 => 275658)


--- trunk/Tools/ChangeLog	2021-04-08 13:16:19 UTC (rev 275657)
+++ trunk/Tools/ChangeLog	2021-04-08 16:14:00 UTC (rev 275658)
@@ -1,3 +1,16 @@
+2021-04-08  Simon Fraser  <[email protected]>
+
+        Copy-constructed Vectors should not have excess capacity
+        https://bugs.webkit.org/show_bug.cgi?id=224313
+
+        Reviewed by Darin Adler.
+
+        New API tests for testing capacity on copy-construct, with tests for copy-constructing
+        with various configs of inline capacity.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::TEST):
+
 2021-04-08  Youenn Fablet  <[email protected]>
 
         Update SFrame implementation to latest version

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (275657 => 275658)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2021-04-08 13:16:19 UTC (rev 275657)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2021-04-08 16:14:00 UTC (rev 275658)
@@ -177,6 +177,15 @@
 
 TEST(WTF_Vector, InitializeFromOtherInitialCapacity)
 {
+    Vector<int> vector = { 1, 3, 2, 4, 5 };
+    Vector<int> vectorCopy(vector);
+    EXPECT_EQ(5U, vector.size());
+    EXPECT_EQ(5U, vectorCopy.size());
+    EXPECT_EQ(5U, vectorCopy.capacity());
+}
+
+TEST(WTF_Vector, InitializeFromOtherInlineInitialCapacity)
+{
     Vector<int, 3> vector = { 1, 3, 2, 4 };
     Vector<int, 5> vectorCopy(vector);
     EXPECT_EQ(4U, vector.size());
@@ -192,6 +201,24 @@
     EXPECT_FALSE(vector != vectorCopy);
 }
 
+TEST(WTF_Vector, InitializeFromOtherInlineBigToSmallInitialCapacity)
+{
+    Vector<int, 5> vector = { 1, 3, 2, 4 };
+    Vector<int, 3> vectorCopy(vector);
+    EXPECT_EQ(4U, vector.size());
+    EXPECT_EQ(5U, vector.capacity());
+    EXPECT_EQ(4U, vectorCopy.size());
+    EXPECT_EQ(4U, vectorCopy.capacity());
+
+    EXPECT_EQ(1, vectorCopy[0]);
+    EXPECT_EQ(3, vectorCopy[1]);
+    EXPECT_EQ(2, vectorCopy[2]);
+    EXPECT_EQ(4, vectorCopy[3]);
+
+    EXPECT_TRUE(vector == vectorCopy);
+    EXPECT_FALSE(vector != vectorCopy);
+}
+
 TEST(WTF_Vector, CopyFromOtherInitialCapacity)
 {
     Vector<int, 3> vector = { 1, 3, 2, 4 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to