Title: [293579] trunk
Revision
293579
Author
[email protected]
Date
2022-04-28 10:46:58 -0700 (Thu, 28 Apr 2022)

Log Message

[WTF] Use quadratic-probing instead of double-hashing
https://bugs.webkit.org/show_bug.cgi?id=239813

Reviewed by Geoffrey Garen.

If table size is always power-of-two, we can safely use quadratic-probing[1] even if the load exceeds 50%.
Quadratic-probing and double-hashing have different tradeoff between clustering possibility and CPU cache
friendliness. Double-hashing can avoid clustering more, but it is not CPU cache friendly. Quadratic-probing
can cause clustering more but better for CPU cache. And while linear-probing caused a lot of trouble and not
practical, quadratic-probing with 75% load factor is practical configuration which is used in production-ready
hashtables e.g. Google's sparsetable / densetable[2].

This patch replaces WTF::HashTable's probing algorithm from double-hashing to quadratic-probing to make it more
cache friendly while keeping reasonable clustering characteristics (compared to linear-probing etc.). Due to
this cache friendliness, we get large performance improvements.

1. 0.52% improvement in Speedometer2 with 95% probability.
2. 0.72% improvement in JetStream2 with 98% probability.

[1]: https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/
[2]: http://goog-sparsehash.sourceforge.net/doc/implementation.html

Canonical link: https://commits.webkit.org/250093@main

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (293578 => 293579)


--- trunk/LayoutTests/ChangeLog	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/ChangeLog	2022-04-28 17:46:58 UTC (rev 293579)
@@ -1,3 +1,19 @@
+2022-04-28  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Use quadratic-probing instead of double-hashing
+        https://bugs.webkit.org/show_bug.cgi?id=239813
+
+        Reviewed by Geoffrey Garen.
+
+        The following tests are relying on HashTable's ordering which is not guaranteed.
+
+        * media/media-session/actionHandlerInternalMappings-expected.txt:
+        * platform/glib/accessibility/content-editable-as-textarea-expected.txt:
+        * platform/gtk/accessibility/native-text-control-attributed-string-expected.txt:
+        * storage/indexeddb/getdatabases-expected.txt:
+        * storage/indexeddb/getdatabases-private-expected.txt:
+        * storage/indexeddb/resources/getdatabases.js:
+
 2022-04-28  Antti Koivisto  <[email protected]>
 
         [ iOS ] tables/mozilla/bugs/bug26178.html is a constant failure

Modified: trunk/LayoutTests/media/media-session/actionHandlerInternalMappings-expected.txt (293578 => 293579)


--- trunk/LayoutTests/media/media-session/actionHandlerInternalMappings-expected.txt	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/media/media-session/actionHandlerInternalMappings-expected.txt	2022-04-28 17:46:58 UTC (rev 293579)
@@ -22,12 +22,12 @@
 Command: SeekToPlaybackPositionCommand
 Iterate over all possible actions
 Command: PauseCommand
+Command: PlayCommand
 Command: SkipBackwardCommand
 Command: SkipForwardCommand
+Command: SeekToPlaybackPositionCommand
+Command: PreviousTrackCommand
 Command: NextTrackCommand
-Command: PreviousTrackCommand
-Command: SeekToPlaybackPositionCommand
-Command: PlayCommand
 Command: StopCommand
 Iterate over possible actions after video element src is cleared
 RUN(video.src = ""

Modified: trunk/LayoutTests/platform/glib/accessibility/content-editable-as-textarea-expected.txt (293578 => 293579)


--- trunk/LayoutTests/platform/glib/accessibility/content-editable-as-textarea-expected.txt	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/platform/glib/accessibility/content-editable-as-textarea-expected.txt	2022-04-28 17:46:58 UTC (rev 293579)
@@ -15,13 +15,13 @@
 		family-name:-webkit-standard
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
 		fg-color:0,0,0
+		weight:400
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for 'ello<\n>':
 	Range attributes for 'worl':
 		weight:700

Modified: trunk/LayoutTests/platform/gtk/accessibility/native-text-control-attributed-string-expected.txt (293578 => 293579)


--- trunk/LayoutTests/platform/gtk/accessibility/native-text-control-attributed-string-expected.txt	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/platform/gtk/accessibility/native-text-control-attributed-string-expected.txt	2022-04-28 17:46:58 UTC (rev 293579)
@@ -10,13 +10,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for 'some word':
 Attributed string for range (0, 3):
 	Default text attributes:
@@ -25,13 +25,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for 'som':
 Attributed string for range (5, 4):
 	Default text attributes:
@@ -40,13 +40,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for 'word':
 Attributed string for range (5, 10):
 Attributed string for range (10, 1):
@@ -57,13 +57,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for 's':
 
 Attributed string for range (0, 3):
@@ -73,13 +73,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for '•••':
 Attributed string for range (0, 2):
 	Default text attributes:
@@ -88,13 +88,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for '••':
 Attributed string for range (1, 2):
 	Default text attributes:
@@ -103,13 +103,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for '••':
 Attributed string for range (1, 10):
 Attributed string for range (10, 1):
@@ -120,13 +120,13 @@
 		bg-color:255,255,255
 		editable:true
 		strikethrough:false
+		direction:ltr
 		style:normal
+		weight:400
 		fg-color:0,0,0
 		size:12pt
-		weight:400
 		invisible:false
 		underline:none
-		direction:ltr
 	Range attributes for '•':
 
 PASS successfullyParsed is true

Modified: trunk/LayoutTests/storage/indexeddb/getdatabases-expected.txt (293578 => 293579)


--- trunk/LayoutTests/storage/indexeddb/getdatabases-expected.txt	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/storage/indexeddb/getdatabases-expected.txt	2022-04-28 17:46:58 UTC (rev 293579)
@@ -10,8 +10,8 @@
 PASS databases[0].version is 1
 PASS databases[0].name is "getdatabase-first"
 PASS databases.length is 2
-PASS databases[1].version is 2
-PASS databases[1].name is "getdatabase-second"
+PASS databases[0].version is 2
+PASS databases[0].name is "getdatabase-second"
 PASS successfullyParsed is true
 
 TEST COMPLETE

Modified: trunk/LayoutTests/storage/indexeddb/getdatabases-private-expected.txt (293578 => 293579)


--- trunk/LayoutTests/storage/indexeddb/getdatabases-private-expected.txt	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/storage/indexeddb/getdatabases-private-expected.txt	2022-04-28 17:46:58 UTC (rev 293579)
@@ -10,8 +10,8 @@
 PASS databases[0].version is 1
 PASS databases[0].name is "getdatabase-first"
 PASS databases.length is 2
-PASS databases[1].version is 2
-PASS databases[1].name is "getdatabase-second"
+PASS databases[0].version is 2
+PASS databases[0].name is "getdatabase-second"
 PASS successfullyParsed is true
 
 TEST COMPLETE

Modified: trunk/LayoutTests/storage/indexeddb/resources/getdatabases.js (293578 => 293579)


--- trunk/LayoutTests/storage/indexeddb/resources/getdatabases.js	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/LayoutTests/storage/indexeddb/resources/getdatabases.js	2022-04-28 17:46:58 UTC (rev 293579)
@@ -35,8 +35,8 @@
         indexedDB.databases().then((result) => {
             databases = result;
             shouldBeEqualToNumber("databases.length", 2);
-            shouldBeEqualToNumber("databases[1].version", 2);
-            shouldBeEqualToString("databases[1].name", "getdatabase-second");
+            shouldBeEqualToNumber("databases[0].version", 2);
+            shouldBeEqualToString("databases[0].name", "getdatabase-second");
             finishJSTest();
         });
     }

Modified: trunk/Source/WTF/ChangeLog (293578 => 293579)


--- trunk/Source/WTF/ChangeLog	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/Source/WTF/ChangeLog	2022-04-28 17:46:58 UTC (rev 293579)
@@ -1,3 +1,35 @@
+2022-04-28  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Use quadratic-probing instead of double-hashing
+        https://bugs.webkit.org/show_bug.cgi?id=239813
+
+        Reviewed by Geoffrey Garen.
+
+        If table size is always power-of-two, we can safely use quadratic-probing[1] even if the load exceeds 50%.
+        Quadratic-probing and double-hashing have different tradeoff between clustering possibility and CPU cache
+        friendliness. Double-hashing can avoid clustering more, but it is not CPU cache friendly. Quadratic-probing
+        can cause clustering more but better for CPU cache. And while linear-probing caused a lot of trouble and not
+        practical, quadratic-probing with 75% load factor is practical configuration which is used in production-ready
+        hashtables e.g. Google's sparsetable / densetable[2].
+
+        This patch replaces WTF::HashTable's probing algorithm from double-hashing to quadratic-probing to make it more
+        cache friendly while keeping reasonable clustering characteristics (compared to linear-probing etc.). Due to
+        this cache friendliness, we get large performance improvements.
+
+        1. 0.52% improvement in Speedometer2 with 95% probability.
+        2. 0.72% improvement in JetStream2 with 98% probability.
+
+        [1]: https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/
+        [2]: http://goog-sparsehash.sourceforge.net/doc/implementation.html
+
+        * wtf/HashTable.h:
+        (WTF::KeyTraits>::inlineLookup):
+        (WTF::KeyTraits>::lookupForWriting):
+        (WTF::KeyTraits>::fullLookupForWriting):
+        (WTF::KeyTraits>::addUniqueForInitialization):
+        (WTF::KeyTraits>::add):
+        (WTF::doubleHash): Deleted.
+
 2022-04-28  Per Arne Vollan  <[email protected]>
 
         [iOS] Enable IOKit blocking in GPUP by default

Modified: trunk/Source/WTF/wtf/HashTable.h (293578 => 293579)


--- trunk/Source/WTF/wtf/HashTable.h	2022-04-28 17:40:36 UTC (rev 293578)
+++ trunk/Source/WTF/wtf/HashTable.h	2022-04-28 17:46:58 UTC (rev 293579)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2019 Apple Inc. All rights reserved.
+ * Copyright (C) 2005-2022 Apple Inc. All rights reserved.
  * Copyright (C) 2008 David Levin <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
@@ -37,6 +37,12 @@
 #include <wtf/StdLibExtras.h>
 #include <wtf/ValueCheck.h>
 
+// Configuration of WTF::HashTable.
+//  - 75% load factor for small tables.
+//  - 50% load factor for large tables.
+//  - Use quadratic probing.
+//  - Always use power-of-two hashtable size, which is also important to make quadratic probing work.
+
 #define DUMP_HASHTABLE_STATS 0
 #define DUMP_HASHTABLE_STATS_PER_TABLE 0
 
@@ -634,16 +640,6 @@
     {
     }
 
-    inline unsigned doubleHash(unsigned key)
-    {
-        key = ~key + (key >> 23);
-        key ^= (key << 12);
-        key ^= (key >> 7);
-        key ^= (key << 2);
-        key ^= (key >> 20);
-        return key;
-    }
-
 #if !ASSERT_ENABLED
 
     template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
@@ -684,7 +680,6 @@
         static_assert(sizeof(Value) <= 150, "Your HashTable types are too big to efficiently move when rehashing.  Consider using UniqueRef instead");
         checkKey<HashTranslator>(key);
 
-        unsigned k = 0;
         ValueType* table = m_table;
         if (!table)
             return nullptr;
@@ -692,10 +687,10 @@
         unsigned sizeMask = tableSizeMask();
         unsigned h = HashTranslator::hash(key);
         unsigned i = h & sizeMask;
+        unsigned probeCount = 0;
 
 #if DUMP_HASHTABLE_STATS
         ++HashTableStats::numAccesses;
-        unsigned probeCount = 0;
 #endif
 
 #if DUMP_HASHTABLE_STATS_PER_TABLE
@@ -719,8 +714,10 @@
                 if (!isDeletedBucket(*entry) && HashTranslator::equal(Extractor::extract(*entry), key))
                     return entry;
             }
+
+            ++probeCount;
+
 #if DUMP_HASHTABLE_STATS
-            ++probeCount;
             HashTableStats::recordCollisionAtCount(probeCount);
 #endif
 
@@ -728,9 +725,7 @@
             m_stats->recordCollisionAtCount(probeCount);
 #endif
 
-            if (k == 0)
-                k = 1 | doubleHash(h);
-            i = (i + k) & sizeMask;
+            i = (i + probeCount) & sizeMask;
         }
     }
 
@@ -741,15 +736,14 @@
         ASSERT(m_table);
         checkKey<HashTranslator>(key);
 
-        unsigned k = 0;
         ValueType* table = m_table;
         unsigned sizeMask = tableSizeMask();
         unsigned h = HashTranslator::hash(key);
         unsigned i = h & sizeMask;
+        unsigned probeCount = 0;
 
 #if DUMP_HASHTABLE_STATS
         ++HashTableStats::numAccesses;
-        unsigned probeCount = 0;
 #endif
 
 #if DUMP_HASHTABLE_STATS_PER_TABLE
@@ -780,8 +774,10 @@
                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
                     return LookupType(entry, true);
             }
+
+            ++probeCount;
+
 #if DUMP_HASHTABLE_STATS
-            ++probeCount;
             HashTableStats::recordCollisionAtCount(probeCount);
 #endif
 
@@ -789,9 +785,7 @@
             m_stats->recordCollisionAtCount(probeCount);
 #endif
 
-            if (k == 0)
-                k = 1 | doubleHash(h);
-            i = (i + k) & sizeMask;
+            i = (i + probeCount) & sizeMask;
         }
     }
 
@@ -802,15 +796,14 @@
         ASSERT(m_table);
         checkKey<HashTranslator>(key);
 
-        unsigned k = 0;
         ValueType* table = m_table;
         unsigned sizeMask = tableSizeMask();
         unsigned h = HashTranslator::hash(key);
         unsigned i = h & sizeMask;
+        unsigned probeCount = 0;
 
 #if DUMP_HASHTABLE_STATS
         ++HashTableStats::numAccesses;
-        unsigned probeCount = 0;
 #endif
 
 #if DUMP_HASHTABLE_STATS_PER_TABLE
@@ -841,8 +834,10 @@
                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
                     return makeLookupResult(entry, true, h);
             }
+
+            ++probeCount;
+
 #if DUMP_HASHTABLE_STATS
-            ++probeCount;
             HashTableStats::recordCollisionAtCount(probeCount);
 #endif
 
@@ -850,9 +845,7 @@
             m_stats->recordCollisionAtCount(probeCount);
 #endif
 
-            if (k == 0)
-                k = 1 | doubleHash(h);
-            i = (i + k) & sizeMask;
+            i = (i + probeCount) & sizeMask;
         }
     }
 
@@ -868,15 +861,14 @@
 
         internalCheckTableConsistency();
 
-        unsigned k = 0;
         ValueType* table = m_table;
         unsigned sizeMask = tableSizeMask();
         unsigned h = HashTranslator::hash(key);
         unsigned i = h & sizeMask;
+        unsigned probeCount = 0;
 
 #if DUMP_HASHTABLE_STATS
         ++HashTableStats::numAccesses;
-        unsigned probeCount = 0;
 #endif
 
 #if DUMP_HASHTABLE_STATS_PER_TABLE
@@ -890,8 +882,9 @@
             if (isEmptyBucket(*entry))
                 break;
 
+            ++probeCount;
+
 #if DUMP_HASHTABLE_STATS
-            ++probeCount;
             HashTableStats::recordCollisionAtCount(probeCount);
 #endif
 
@@ -899,9 +892,7 @@
             m_stats->recordCollisionAtCount(probeCount);
 #endif
 
-            if (k == 0)
-                k = 1 | doubleHash(h);
-            i = (i + k) & sizeMask;
+            i = (i + probeCount) & sizeMask;
         }
 
         HashTranslator::translate(*entry, std::forward<T>(key), std::forward<Extra>(extra));
@@ -949,15 +940,14 @@
 
         ASSERT(m_table);
 
-        unsigned k = 0;
         ValueType* table = m_table;
         unsigned sizeMask = tableSizeMask();
         unsigned h = HashTranslator::hash(key);
         unsigned i = h & sizeMask;
+        unsigned probeCount = 0;
 
 #if DUMP_HASHTABLE_STATS
         ++HashTableStats::numAccesses;
-        unsigned probeCount = 0;
 #endif
 
 #if DUMP_HASHTABLE_STATS_PER_TABLE
@@ -988,8 +978,10 @@
                 else if (HashTranslator::equal(Extractor::extract(*entry), key))
                     return AddResult(makeKnownGoodIterator(entry), false);
             }
+
+            ++probeCount;
+
 #if DUMP_HASHTABLE_STATS
-            ++probeCount;
             HashTableStats::recordCollisionAtCount(probeCount);
 #endif
 
@@ -997,9 +989,7 @@
             m_stats->recordCollisionAtCount(probeCount);
 #endif
 
-            if (k == 0)
-                k = 1 | doubleHash(h);
-            i = (i + k) & sizeMask;
+            i = (i + probeCount) & sizeMask;
         }
 
         if (deletedEntry) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to