Skip to content

Commit 53bedf6

Browse files
committed
Store one status byte per bucket in WTF::HashTable
https://bugs.webkit.org/show_bug.cgi?id=330025 rdar://191722665 Reviewed by Yusuke Suzuki. The status byte stores empty, deleted, or 7 bits of the key's hash. (And we use additional hash bits, scaled with table size, to index into the status byte array.) This eliminates the need for special empty / deleted keys per type, which have been a common source of bugs. (RobinHoodHashTable still uses special empty / deleted keys, so it needs a follow-up before we can remove them entirely.) This is also a speedup: 0.94%, 1.36% Speedometer 3 0.47% MotionMark 0.39%, 0.79% JetStream 3.0 Likely sources of speedup: * Misses and collisions are faster because, on average, ~95% of lookups terminate in a single cache line without any extra key comparisons. * Iteration during rehash and destruction are faster because we touch less memory, especially for sparse tables. Note: I also benchmarked popular "Swiss" / SIMD hash tables. They were slower. The likely explanation is fixed costs from extra bit twiddling and SIMD latency, especially in small tables, which are common in WebKit. (SIMD benchmarked faster for very large tables with very high load factors. So SIMD might be a path to memory savings in the future.) I had to fix hash translators that assigned their keys instead of constructing them. Assigning was always sketchy in theory (since the assigned-to location was the sentinel empty / deleted key, which is not valid to access in any way), but it is now much more likely to encounter truly uninitialized bits in practice. (Perhaps a follow-up should pass void* or span<char> to translators, to prevent this mistake at compile time.) Tests: Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp Tools/TestWebKitAPI/Tests/WTF/HashTable.cpp * Source/JavaScriptCore/dfg/DFGCSEPhase.cpp: * Source/JavaScriptCore/dfg/DFGLazyJSValue.cpp: (JSC::DFG::CrossThreadStringTranslator::translate): * Source/JavaScriptCore/jit/JITThunks.cpp: (JSC::JITThunks::NativeExecutableTranslator::translate): * Source/WTF/wtf/Compiler.h: Add ALWAYS_INLINE_CALLEE. * Source/WTF/wtf/HashMap.h: (WTF::HashMapTranslator::translate): (WTF::HashMapTranslatorAdapter::translate): * Source/WTF/wtf/HashSet.h: (WTF::HashSetTranslator::translate): (WTF::HashSetTranslatorAdapter::translate): (WTF::HashSetEnsureTranslatorAdaptor::translate): (WTF::shouldValidateKey>::take): Fix hash translators that assigned instead of constructing. * Source/WTF/wtf/HashTable.h: New implementation, including status bytes. * Source/WTF/wtf/ListHashSet.h: (WTF::ListHashSetTranslator::translate): More hash translator fixes. * Source/WTF/wtf/RobinHoodHashTable.h: Copied the old iterators into RobinHoodHashTable since HashTable iterates differently now. * Source/WTF/wtf/SmallMap.h: Remove isValidKey() assertions because all keys are valid now. * Source/WTF/wtf/text/AtomStringImpl.cpp: (WTF::UTF16BufferTranslator::translate): (WTF::SubstringTranslator::translate): (WTF::Latin1BufferTranslator::translate): (WTF::BufferFromStaticDataTranslator::translate): (WTF::StaticStringAtomTranslator::translate): * Source/WTF/wtf/text/StringHash.h: (WTF::StringViewHashTranslator::translate): (WTF::ASCIICaseInsensitiveStringViewHashTranslator::translate): (WTF::HashTranslatorASCIILiteral::translate): * Source/WebCore/contentextensions/CombinedFiltersAlphabet.cpp: (WebCore::ContentExtensions::TermCreatorTranslator::translate): * Source/WebCore/contentextensions/NFAToDFA.cpp: (WebCore::ContentExtensions::NodeIdSetToUniqueNodeIdSetTranslator::translate): * Source/WebCore/dom/DocumentSharedObjectPool.cpp: (WebCore::AttributeSpanTranslator::translate): * Source/WebCore/dom/QualifiedNameCache.cpp: (WebCore::QNameComponentsTranslator::translate): * Source/WebCore/platform/graphics/cg/CGSubimageCacheWithTimer.cpp: (WebCore::CGSubimageCacheAdder::translate): * Source/WebCore/rendering/TextAutoSizing.cpp: (WebCore::TextAutoSizingHashTranslator::translate): More hash translator fixes. * Tools/Scripts/webkitpy/api_tests/allowlist.txt: Removed tests we don't need anymore. * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Added HashTable.cpp. * Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp: * Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp: Removed empty / deleted tests because that bug can't happen anymore. * Tools/TestWebKitAPI/Tests/WTF/HashTable.cpp: Asked claude to go nuts on regression tests for new edge cases. Canonical link: https://commits.webkit.org/325806@main
1 parent f79d4db commit 53bedf6

25 files changed

Lines changed: 1728 additions & 594 deletions

‎Source/JavaScriptCore/dfg/DFGCSEPhase.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ struct ImpureDataTranslator {
9898
return slot->key == key;
9999
}
100100

101-
static void translate(std::unique_ptr<ImpureDataSlot>& slot, const HeapLocation& key, unsigned hashCode)
101+
static void translate(std::unique_ptr<ImpureDataSlot>* slot, const HeapLocation& key, unsigned hashCode)
102102
{
103-
new (NotNull, std::addressof(slot)) std::unique_ptr<ImpureDataSlot>(new ImpureDataSlot {key, LazyNode(), hashCode});
103+
new (NotNull, slot) std::unique_ptr<ImpureDataSlot>(new ImpureDataSlot { key, LazyNode(), hashCode });
104104
}
105105
};
106106

‎Source/JavaScriptCore/dfg/DFGLazyJSValue.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ struct CrossThreadStringTranslator {
118118
return WTF::equal(string.impl(), impl);
119119
}
120120

121-
static void translate(String& location, const StringImpl* impl, unsigned)
121+
static void translate(String* location, const StringImpl* impl, unsigned)
122122
{
123-
location = impl->isolatedCopy();
123+
new (NotNull, location) String(impl->isolatedCopy());
124124
}
125125
};
126126

‎Source/JavaScriptCore/jit/JITThunks.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ struct JITThunks::HostKeySearcher {
288288
struct JITThunks::NativeExecutableTranslator {
289289
static unsigned LEGACY_NODELETE hash(const NativeExecutable* key) { return WeakNativeExecutableHash::hash(key); }
290290
static bool LEGACY_NODELETE equal(const Weak<NativeExecutable>& a, const NativeExecutable* b) { return WeakNativeExecutableHash::equal(a, b); }
291-
static void translate(Weak<NativeExecutable>& location, NativeExecutable* executable, unsigned)
291+
static void translate(Weak<NativeExecutable>* location, NativeExecutable* executable, unsigned)
292292
{
293-
location = Weak<NativeExecutable>(executable, executable->vm().jitStubs.get());
293+
new (NotNull, location) Weak<NativeExecutable>(executable, executable->vm().jitStubs.get());
294294
}
295295
};
296296

‎Source/WTF/wtf/Compiler.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@
213213
#define ALWAYS_INLINE_LAMBDA
214214
#endif
215215

216+
/* ALWAYS_INLINE_CALLEE */
217+
218+
/* Inlines the calls in the statement that follows. Only Clang supports always_inline on a statement. */
219+
#if !defined(ALWAYS_INLINE_CALLEE) && defined(NDEBUG) && COMPILER(CLANG)
220+
#define ALWAYS_INLINE_CALLEE [[clang::always_inline]] // NOLINT: check-webkit-style does not understand annotations.
221+
#endif
222+
223+
#if !defined(ALWAYS_INLINE_CALLEE)
224+
#define ALWAYS_INLINE_CALLEE
225+
#endif
226+
216227
/* COLD */
217228

218229
#if !defined(COLD)

‎Source/WTF/wtf/HashMap.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class HashMap final {
213213
// in the table. HashTranslator must have the following function members:
214214
// static unsigned hash(const T&);
215215
// static bool equal(const ValueType&, const T&);
216-
// static translate(ValueType&, const T&, unsigned hashCode);
216+
// static void translate(KeyType* uninitializedLocation, const T&, unsigned hashCode);
217217
template<typename HashTranslator, typename K, typename V> AddResult add(K&&, V&&) LIFETIME_BOUND;
218218
template<typename HashTranslator> AddResult ensure(auto&& key, NOESCAPE const Invocable<MappedType()> auto&) LIFETIME_BOUND;
219219

@@ -269,21 +269,21 @@ template<typename ValueTraits, typename HashFunctions>
269269
struct HashMapTranslator {
270270
static unsigned hash(const auto& key) { return HashFunctions::hash(key); }
271271
static bool equal(const auto& a, const auto& b) { return HashFunctions::equal(a, b); }
272-
template<typename U> static void translate(auto& location, U&& key, NOESCAPE const Invocable<typename ValueTraits::ValueTraits::TraitType()> auto& functor)
272+
template<typename U> static void translate(typename ValueTraits::TraitType* location, U&& key, NOESCAPE const Invocable<typename ValueTraits::ValueTraits::TraitType()> auto& functor)
273273
{
274-
ValueTraits::KeyTraits::assignToEmpty(location.key, std::forward<U>(key));
275-
ValueTraits::ValueTraits::assignToEmpty(location.value, functor());
274+
std::construct_at(std::addressof(location->key), std::forward<U>(key));
275+
std::construct_at(std::addressof(location->value), functor());
276276
}
277277
};
278278

279279
template<typename ValueTraits, typename Translator>
280280
struct HashMapTranslatorAdapter {
281281
static unsigned hash(const auto& key) { return Translator::hash(key); }
282282
static bool equal(const auto& a, const auto& b) { return Translator::equal(a, b); }
283-
static void translate(auto& location, auto&& key, NOESCAPE const Invocable<typename ValueTraits::ValueTraits::TraitType()> auto& functor, unsigned hashCode)
283+
static void translate(typename ValueTraits::TraitType* location, auto&& key, NOESCAPE const Invocable<typename ValueTraits::ValueTraits::TraitType()> auto& functor, unsigned hashCode)
284284
{
285-
Translator::translate(location.key, key, hashCode);
286-
location.value = functor();
285+
Translator::translate(std::addressof(location->key), key, hashCode);
286+
std::construct_at(std::addressof(location->value), functor());
287287
}
288288
};
289289

‎Source/WTF/wtf/HashSet.h‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class HashSet final {
126126
// in the table. HashTranslator must have the following function members:
127127
// static unsigned hash(const T&);
128128
// static bool equal(const ValueType&, const T&);
129-
// static translate(ValueType&, const T&, unsigned hashCode);
129+
// static void translate(ValueType* uninitializedLocation, const T&, unsigned hashCode);
130130
template<typename HashTranslator, typename T> AddResult add(const T&) LIFETIME_BOUND;
131131

132132
// An alternate version of translated add(), ensure() will still do translation
@@ -236,17 +236,17 @@ template<typename ValueTraits, typename HashFunctions>
236236
struct HashSetTranslator {
237237
static unsigned hash(const auto& key) { return HashFunctions::hash(key); }
238238
static bool equal(const auto& a, const auto& b) { return HashFunctions::equal(a, b); }
239-
static void translate(auto& location, auto&&, NOESCAPE const Invocable<typename ValueTraits::TraitType()> auto& functor)
240-
{
241-
ValueTraits::assignToEmpty(location, functor());
239+
static void translate(typename ValueTraits::TraitType* location, auto&&, NOESCAPE const Invocable<typename ValueTraits::TraitType()> auto& functor)
240+
{
241+
std::construct_at(location, functor());
242242
}
243243
};
244244

245245
template<typename Translator>
246246
struct HashSetTranslatorAdapter {
247247
static unsigned hash(const auto& key) { return Translator::hash(key); }
248248
static bool equal(const auto& a, const auto& b) { return Translator::equal(a, b); }
249-
static void translate(auto& location, const auto& key, const auto&, unsigned hashCode)
249+
static void translate(auto* location, const auto& key, const auto&, unsigned hashCode)
250250
{
251251
Translator::translate(location, key, hashCode);
252252
}
@@ -256,9 +256,9 @@ template<typename ValueTraits, typename Translator>
256256
struct HashSetEnsureTranslatorAdaptor {
257257
static unsigned hash(const auto& key) { return Translator::hash(key); }
258258
static bool equal(const auto& a, const auto& b) { return Translator::equal(a, b); }
259-
static void translate(auto& location, auto&&, NOESCAPE const Invocable<typename ValueTraits::TraitType()> auto& functor)
259+
static void translate(typename ValueTraits::TraitType* location, auto&&, NOESCAPE const Invocable<typename ValueTraits::TraitType()> auto& functor)
260260
{
261-
ValueTraits::assignToEmpty(location, functor());
261+
std::construct_at(location, functor());
262262
}
263263
};
264264

@@ -440,8 +440,9 @@ inline auto HashSet<T, U, V, W, shouldValidateKey>::take(iterator it) -> TakeTyp
440440
if (it == end())
441441
return ValueTraits::take(ValueTraits::emptyValue());
442442

443+
m_impl.internalCheckTableConsistency();
443444
auto result = ValueTraits::take(WTF::move(const_cast<ValueType&>(*it)));
444-
remove(it);
445+
m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
445446
return result;
446447
}
447448

0 commit comments

Comments
 (0)