Title: [247673] trunk
Revision
247673
Author
[email protected]
Date
2019-07-20 20:53:31 -0700 (Sat, 20 Jul 2019)

Log Message

Speed up HashTable decoding by reserving capacity and avoiding rehashing
https://bugs.webkit.org/show_bug.cgi?id=199982

Reviewed by Saam Barati.

Source/WebKit:

Use HashMap::reserveInitialCapacity() in the HashMap IPC decoder for
performance. I measured a ~35% improvement when decoding a very large
HashMap of Strings (~160k entries) in the context of the
StorageManager::GetValues IPC.

* Platform/IPC/ArgumentCoders.h:
* Shared/API/c/WKDictionary.cpp:
(WKDictionaryCreate):

Source/WTF:

Introduce reserveInitialCapacity() on HashMap to reserve capacity on a
HashMap and cut down on rehashing cost when possible.

* wtf/HashMap.h:
* wtf/HashTable.h:
(WTF::HashTable::reserveInitialCapacity):

* wtf/persistence/PersistentCoders.h:
Use HashMap::reserveInitialCapacity() in the HashMap persistent decoder for
performance.

Tools:

Add API test coverage.

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

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (247672 => 247673)


--- trunk/Source/WTF/ChangeLog	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WTF/ChangeLog	2019-07-21 03:53:31 UTC (rev 247673)
@@ -1,3 +1,21 @@
+2019-07-20  Chris Dumez  <[email protected]>
+
+        Speed up HashTable decoding by reserving capacity and avoiding rehashing
+        https://bugs.webkit.org/show_bug.cgi?id=199982
+
+        Reviewed by Saam Barati.
+
+        Introduce reserveInitialCapacity() on HashMap to reserve capacity on a
+        HashMap and cut down on rehashing cost when possible.
+
+        * wtf/HashMap.h:
+        * wtf/HashTable.h:
+        (WTF::HashTable::reserveInitialCapacity):
+
+        * wtf/persistence/PersistentCoders.h:
+        Use HashMap::reserveInitialCapacity() in the HashMap persistent decoder for
+        performance.
+
 2019-07-20  Alexander Mikhaylenko  <[email protected]>
 
         REGRESSION(r246033/r246496): [GTK] Kinetic scrolling doesn't work

Modified: trunk/Source/WTF/wtf/HashMap.h (247672 => 247673)


--- trunk/Source/WTF/wtf/HashMap.h	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WTF/wtf/HashMap.h	2019-07-21 03:53:31 UTC (rev 247673)
@@ -92,6 +92,8 @@
     unsigned capacity() const;
     bool isEmpty() const;
 
+    void reserveInitialCapacity(unsigned keyCount) { m_impl.reserveInitialCapacity(keyCount); }
+
     // iterators iterate over pairs of keys and values
     iterator begin();
     iterator end();

Modified: trunk/Source/WTF/wtf/HashTable.h (247672 => 247673)


--- trunk/Source/WTF/wtf/HashTable.h	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WTF/wtf/HashTable.h	2019-07-21 03:53:31 UTC (rev 247673)
@@ -398,6 +398,20 @@
         unsigned capacity() const { return m_tableSize; }
         bool isEmpty() const { return !m_keyCount; }
 
+        void reserveInitialCapacity(unsigned keyCount)
+        {
+            ASSERT(!m_table);
+            ASSERT(!m_tableSize);
+            ASSERT(!m_deletedCount);
+
+            unsigned minimumTableSize = KeyTraits::minimumTableSize;
+            unsigned newTableSize = std::max(minimumTableSize, computeBestTableSize(keyCount));
+
+            m_tableSize = newTableSize;
+            m_tableSizeMask = newTableSize - 1;
+            m_table = allocateTable(newTableSize);
+        }
+
         AddResult add(const ValueType& value) { return add<IdentityTranslatorType>(Extractor::extract(value), value); }
         AddResult add(ValueType&& value) { return add<IdentityTranslatorType>(Extractor::extract(value), WTFMove(value)); }
 

Modified: trunk/Source/WTF/wtf/persistence/PersistentCoders.h (247672 => 247673)


--- trunk/Source/WTF/wtf/persistence/PersistentCoders.h	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WTF/wtf/persistence/PersistentCoders.h	2019-07-21 03:53:31 UTC (rev 247673)
@@ -196,6 +196,7 @@
             return false;
 
         HashMapType tempHashMap;
+        tempHashMap.reserveInitialCapacity(hashMapSize);
         for (uint64_t i = 0; i < hashMapSize; ++i) {
             KeyArg key;
             MappedArg value;

Modified: trunk/Source/WebKit/ChangeLog (247672 => 247673)


--- trunk/Source/WebKit/ChangeLog	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WebKit/ChangeLog	2019-07-21 03:53:31 UTC (rev 247673)
@@ -1,5 +1,21 @@
 2019-07-20  Chris Dumez  <[email protected]>
 
+        Speed up HashTable decoding by reserving capacity and avoiding rehashing
+        https://bugs.webkit.org/show_bug.cgi?id=199982
+
+        Reviewed by Saam Barati.
+
+        Use HashMap::reserveInitialCapacity() in the HashMap IPC decoder for
+        performance. I measured a ~35% improvement when decoding a very large
+        HashMap of Strings (~160k entries) in the context of the
+        StorageManager::GetValues IPC.
+
+        * Platform/IPC/ArgumentCoders.h:
+        * Shared/API/c/WKDictionary.cpp:
+        (WKDictionaryCreate):
+
+2019-07-20  Chris Dumez  <[email protected]>
+
         Micro-optimize HashMap & String IPC decoding
         https://bugs.webkit.org/show_bug.cgi?id=199967
 

Modified: trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h (247672 => 247673)


--- trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-21 03:53:31 UTC (rev 247673)
@@ -378,6 +378,7 @@
             return WTF::nullopt;
 
         HashMapType hashMap;
+        hashMap.reserveInitialCapacity(hashMapSize);
         for (uint32_t i = 0; i < hashMapSize; ++i) {
             Optional<KeyArg> key;
             decoder >> key;

Modified: trunk/Source/WebKit/Shared/API/c/WKDictionary.cpp (247672 => 247673)


--- trunk/Source/WebKit/Shared/API/c/WKDictionary.cpp	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Source/WebKit/Shared/API/c/WKDictionary.cpp	2019-07-21 03:53:31 UTC (rev 247673)
@@ -38,6 +38,7 @@
 WK_EXPORT WKDictionaryRef WKDictionaryCreate(const WKStringRef* keys, const WKTypeRef* values, size_t numberOfValues)
 {
     API::Dictionary::MapType map;
+    map.reserveInitialCapacity(numberOfValues);
     for (size_t i = 0; i < numberOfValues; ++i)
         map.add(WebKit::toImpl(keys[i])->string(), WebKit::toImpl(values[i]));
 

Modified: trunk/Tools/ChangeLog (247672 => 247673)


--- trunk/Tools/ChangeLog	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Tools/ChangeLog	2019-07-21 03:53:31 UTC (rev 247673)
@@ -1,3 +1,15 @@
+2019-07-20  Chris Dumez  <[email protected]>
+
+        Speed up HashTable decoding by reserving capacity and avoiding rehashing
+        https://bugs.webkit.org/show_bug.cgi?id=199982
+
+        Reviewed by Saam Barati.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WTF/HashMap.cpp:
+        (TestWebKitAPI::TEST):
+
 2019-07-20  Andres Gonzalez  <[email protected]>
 
         Add accessibilityInsertText for text insertion in edit fields.

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp (247672 => 247673)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2019-07-21 01:04:07 UTC (rev 247672)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2019-07-21 03:53:31 UTC (rev 247673)
@@ -34,6 +34,7 @@
 #include <string>
 #include <wtf/HashMap.h>
 #include <wtf/Ref.h>
+#include <wtf/text/StringConcatenateNumbers.h>
 #include <wtf/text/StringHash.h>
 
 namespace TestWebKitAPI {
@@ -1037,6 +1038,39 @@
     ASSERT_LE(ones, 600u);
 }
 
+TEST(WTF_HashMap, ReserveInitialCapacity)
+{
+    HashMap<String, String> map;
+    EXPECT_EQ(0u, map.size());
+    map.reserveInitialCapacity(9999);
+    EXPECT_EQ(0u, map.size());
+    for (int i = 0; i < 9999; ++i)
+        map.add(makeString("foo", i), makeString("bar", i));
+    EXPECT_EQ(9999u, map.size());
+    EXPECT_TRUE(map.contains("foo3"_str));
+    EXPECT_STREQ("bar3", map.get("foo3"_str).utf8().data());
+    for (int i = 0; i < 9999; ++i)
+        map.add(makeString("excess", i), makeString("baz", i));
+    EXPECT_EQ(9999u + 9999u, map.size());
+    for (int i = 0; i < 9999; ++i)
+        EXPECT_TRUE(map.remove(makeString("foo", i)));
+    EXPECT_EQ(9999u, map.size());
+    EXPECT_STREQ("baz3", map.get("excess3"_str).utf8().data());
+    for (int i = 0; i < 9999; ++i)
+        EXPECT_TRUE(map.remove(makeString("excess", i)));
+    EXPECT_EQ(0u, map.size());
+    
+    HashMap<String, String> map2;
+    map2.reserveInitialCapacity(9999);
+    EXPECT_FALSE(map2.remove("foo1"_s));
+    for (int i = 0; i < 2000; ++i)
+        map2.add(makeString("foo", i), makeString("bar", i));
+    EXPECT_EQ(2000u, map2.size());
+    for (int i = 0; i < 2000; ++i)
+        EXPECT_TRUE(map2.remove(makeString("foo", i)));
+    EXPECT_EQ(0u, map2.size());
+}
+
 TEST(WTF_HashMap, Random_IsEvenlyDistributedAfterRemove)
 {
     for (size_t tableSize = 2; tableSize <= 2 * 6; ++tableSize) { // Our hash tables shrink at a load factor of 1 / 6.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to