Title: [247740] branches/safari-608-branch
Revision
247740
Author
[email protected]
Date
2019-07-23 13:41:02 -0700 (Tue, 23 Jul 2019)

Log Message

Cherry-pick r247673. rdar://problem/53449723

    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):

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

Modified Paths

Diff

Modified: branches/safari-608-branch/Source/WTF/ChangeLog (247739 => 247740)


--- branches/safari-608-branch/Source/WTF/ChangeLog	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WTF/ChangeLog	2019-07-23 20:41:02 UTC (rev 247740)
@@ -1,3 +1,64 @@
+2019-07-23  Alan Coon  <[email protected]>
+
+        Cherry-pick r247673. rdar://problem/53449723
+
+    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):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247673 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    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-18  Alan Coon  <[email protected]>
 
         Cherry-pick r247566. rdar://problem/53279081

Modified: branches/safari-608-branch/Source/WTF/wtf/HashMap.h (247739 => 247740)


--- branches/safari-608-branch/Source/WTF/wtf/HashMap.h	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WTF/wtf/HashMap.h	2019-07-23 20:41:02 UTC (rev 247740)
@@ -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: branches/safari-608-branch/Source/WTF/wtf/HashTable.h (247739 => 247740)


--- branches/safari-608-branch/Source/WTF/wtf/HashTable.h	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WTF/wtf/HashTable.h	2019-07-23 20:41:02 UTC (rev 247740)
@@ -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: branches/safari-608-branch/Source/WTF/wtf/persistence/PersistentCoders.h (247739 => 247740)


--- branches/safari-608-branch/Source/WTF/wtf/persistence/PersistentCoders.h	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WTF/wtf/persistence/PersistentCoders.h	2019-07-23 20:41:02 UTC (rev 247740)
@@ -196,6 +196,7 @@
             return false;
 
         HashMapType tempHashMap;
+        tempHashMap.reserveInitialCapacity(hashMapSize);
         for (uint64_t i = 0; i < hashMapSize; ++i) {
             KeyArg key;
             MappedArg value;

Modified: branches/safari-608-branch/Source/WebKit/ChangeLog (247739 => 247740)


--- branches/safari-608-branch/Source/WebKit/ChangeLog	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WebKit/ChangeLog	2019-07-23 20:41:02 UTC (rev 247740)
@@ -1,5 +1,64 @@
 2019-07-23  Alan Coon  <[email protected]>
 
+        Cherry-pick r247673. rdar://problem/53449723
+
+    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):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247673 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    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-23  Alan Coon  <[email protected]>
+
         Cherry-pick r247672. rdar://problem/53449739
 
     Micro-optimize HashMap & String IPC decoding

Modified: branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h (247739 => 247740)


--- branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WebKit/Platform/IPC/ArgumentCoders.h	2019-07-23 20:41:02 UTC (rev 247740)
@@ -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: branches/safari-608-branch/Source/WebKit/Shared/API/c/WKDictionary.cpp (247739 => 247740)


--- branches/safari-608-branch/Source/WebKit/Shared/API/c/WKDictionary.cpp	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Source/WebKit/Shared/API/c/WKDictionary.cpp	2019-07-23 20:41:02 UTC (rev 247740)
@@ -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: branches/safari-608-branch/Tools/ChangeLog (247739 => 247740)


--- branches/safari-608-branch/Tools/ChangeLog	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Tools/ChangeLog	2019-07-23 20:41:02 UTC (rev 247740)
@@ -1,3 +1,58 @@
+2019-07-23  Alan Coon  <[email protected]>
+
+        Cherry-pick r247673. rdar://problem/53449723
+
+    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):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247673 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    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-18  Alan Coon  <[email protected]>
 
         Cherry-pick r247566. rdar://problem/53279081

Modified: branches/safari-608-branch/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp (247739 => 247740)


--- branches/safari-608-branch/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2019-07-23 20:40:58 UTC (rev 247739)
+++ branches/safari-608-branch/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2019-07-23 20:41:02 UTC (rev 247740)
@@ -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