From 4d05b0484578cf33c5764c296e44c6f71079bebb Mon Sep 17 00:00:00 2001 From: Toon Verwaest Date: Thu, 22 Jun 2017 16:00:02 +0200 Subject: [PATCH] [runtime] Remove HashTable::UsesSeed. Specialize Hash(ForObject) instead. Bug: Change-Id: I52bd9573735ac7c28a03e070064fe89b38d479ef Reviewed-on: https://chromium-review.googlesource.com/544957 Reviewed-by: Camillo Bruni Commit-Queue: Toon Verwaest Cr-Commit-Position: refs/heads/master@{#46141} --- src/code-stub-assembler.cc | 9 ++---- src/objects-inl.h | 44 +++++++++++++++-------------- src/objects.cc | 17 ++++++----- src/objects/code-cache.h | 4 +-- src/objects/compilation-cache-inl.h | 3 +- src/objects/compilation-cache.h | 6 ++-- src/objects/dictionary.h | 13 ++++----- src/objects/hash-table-inl.h | 18 ------------ src/objects/hash-table.h | 30 +++++--------------- src/objects/string-table.h | 8 +++--- 10 files changed, 59 insertions(+), 93 deletions(-) diff --git a/src/code-stub-assembler.cc b/src/code-stub-assembler.cc index f436d6a29b..bcb1ed065e 100644 --- a/src/code-stub-assembler.cc +++ b/src/code-stub-assembler.cc @@ -5057,12 +5057,9 @@ void CodeStubAssembler::NumberDictionaryLookup(Node* dictionary, Node* capacity = SmiUntag(GetCapacity(dictionary)); Node* mask = IntPtrSub(capacity, IntPtrConstant(1)); - Node* int32_seed; - if (Dictionary::ShapeT::UsesSeed) { - int32_seed = HashSeed(); - } else { - int32_seed = Int32Constant(kZeroHashSeed); - } + Node* int32_seed = std::is_same::value + ? HashSeed() + : Int32Constant(kZeroHashSeed); Node* hash = ChangeUint32ToWord(ComputeIntegerHash(intptr_index, int32_seed)); Node* key_as_float64 = RoundIntPtrToFloat64(intptr_index); diff --git a/src/objects-inl.h b/src/objects-inl.h index 5067d18883..d64872a442 100644 --- a/src/objects-inl.h +++ b/src/objects-inl.h @@ -2603,7 +2603,7 @@ int HashTable::FindEntry(Key key) { template int HashTable::FindEntry(Isolate* isolate, Key key) { - return FindEntry(isolate, key, HashTable::Hash(key)); + return FindEntry(isolate, key, Shape::Hash(isolate, key)); } // Find entry for key otherwise return kNotFound. @@ -2655,9 +2655,11 @@ bool StringSetShape::IsMatch(String* key, Object* value) { return key->Equals(String::cast(value)); } -uint32_t StringSetShape::Hash(String* key) { return key->Hash(); } +uint32_t StringSetShape::Hash(Isolate* isolate, String* key) { + return key->Hash(); +} -uint32_t StringSetShape::HashForObject(Object* object) { +uint32_t StringSetShape::HashForObject(Isolate* isolate, Object* object) { return String::cast(object)->Hash(); } @@ -2674,7 +2676,7 @@ Handle StringTableShape::AsHandle(Isolate* isolate, return key->AsHandle(isolate); } -uint32_t StringTableShape::HashForObject(Object* object) { +uint32_t StringTableShape::HashForObject(Isolate* isolate, Object* object) { return String::cast(object)->Hash(); } @@ -6094,12 +6096,12 @@ bool NumberDictionaryShape::IsMatch(uint32_t key, Object* other) { return key == static_cast(other->Number()); } - -uint32_t UnseededNumberDictionaryShape::Hash(uint32_t key) { +uint32_t UnseededNumberDictionaryShape::Hash(Isolate* isolate, uint32_t key) { return ComputeIntegerHash(key, 0); } -uint32_t UnseededNumberDictionaryShape::HashForObject(Object* other) { +uint32_t UnseededNumberDictionaryShape::HashForObject(Isolate* isolate, + Object* other) { DCHECK(other->IsNumber()); return ComputeIntegerHash(static_cast(other->Number()), 0); } @@ -6108,14 +6110,15 @@ Map* UnseededNumberDictionaryShape::GetMap(Isolate* isolate) { return isolate->heap()->unseeded_number_dictionary_map(); } -uint32_t SeededNumberDictionaryShape::SeededHash(uint32_t key, uint32_t seed) { - return ComputeIntegerHash(key, seed); +uint32_t SeededNumberDictionaryShape::Hash(Isolate* isolate, uint32_t key) { + return ComputeIntegerHash(key, isolate->heap()->HashSeed()); } -uint32_t SeededNumberDictionaryShape::SeededHashForObject(uint32_t seed, - Object* other) { +uint32_t SeededNumberDictionaryShape::HashForObject(Isolate* isolate, + Object* other) { DCHECK(other->IsNumber()); - return ComputeIntegerHash(static_cast(other->Number()), seed); + return ComputeIntegerHash(static_cast(other->Number()), + isolate->heap()->HashSeed()); } @@ -6131,12 +6134,11 @@ bool NameDictionaryShape::IsMatch(Handle key, Object* other) { return *key == other; } - -uint32_t NameDictionaryShape::Hash(Handle key) { +uint32_t NameDictionaryShape::Hash(Isolate* isolate, Handle key) { return key->Hash(); } -uint32_t NameDictionaryShape::HashForObject(Object* other) { +uint32_t NameDictionaryShape::HashForObject(Isolate* isolate, Object* other) { return Name::cast(other)->Hash(); } @@ -6185,12 +6187,11 @@ bool ObjectHashTableShape::IsMatch(Handle key, Object* other) { return key->SameValue(other); } - -uint32_t ObjectHashTableShape::Hash(Handle key) { +uint32_t ObjectHashTableShape::Hash(Isolate* isolate, Handle key) { return Smi::cast(key->GetHash())->value(); } -uint32_t ObjectHashTableShape::HashForObject(Object* other) { +uint32_t ObjectHashTableShape::HashForObject(Isolate* isolate, Object* other) { return Smi::cast(other->GetHash())->value(); } @@ -6211,9 +6212,9 @@ bool WeakHashTableShape::IsMatch(Handle key, Object* other) { : *key == other; } - template -uint32_t WeakHashTableShape::Hash(Handle key) { +uint32_t WeakHashTableShape::Hash(Isolate* isolate, + Handle key) { intptr_t hash = key->IsWeakCell() ? reinterpret_cast(WeakCell::cast(*key)->value()) @@ -6222,7 +6223,8 @@ uint32_t WeakHashTableShape::Hash(Handle key) { } template -uint32_t WeakHashTableShape::HashForObject(Object* other) { +uint32_t WeakHashTableShape::HashForObject(Isolate* isolate, + Object* other) { if (other->IsWeakCell()) other = WeakCell::cast(other)->value(); intptr_t hash = reinterpret_cast(other); return (uint32_t)(hash & 0xFFFFFFFF); diff --git a/src/objects.cc b/src/objects.cc index f7edbb9a1f..d8ebe796e1 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -16060,14 +16060,12 @@ void HashTable::Rehash(Derived* new_table) { // Rehash the elements. int capacity = this->Capacity(); - Heap* heap = new_table->GetHeap(); - Object* the_hole = heap->the_hole_value(); - Object* undefined = heap->undefined_value(); + Isolate* isolate = new_table->GetIsolate(); for (int i = 0; i < capacity; i++) { uint32_t from_index = EntryToIndex(i); Object* k = this->get(from_index); - if (k != the_hole && k != undefined) { - uint32_t hash = this->HashForObject(k); + if (IsKey(isolate, k)) { + uint32_t hash = Shape::HashForObject(isolate, k); uint32_t insertion_index = EntryToIndex(new_table->FindInsertionEntry(hash)); for (int j = 0; j < Shape::kEntrySize; j++) { @@ -16082,7 +16080,7 @@ void HashTable::Rehash(Derived* new_table) { template uint32_t HashTable::EntryForProbe(Object* k, int probe, uint32_t expected) { - uint32_t hash = this->HashForObject(k); + uint32_t hash = Shape::HashForObject(GetIsolate(), k); uint32_t capacity = this->Capacity(); uint32_t entry = FirstProbe(hash, capacity); for (int i = 1; i < probe; i++) { @@ -17198,7 +17196,7 @@ Handle StringSet::Add(Handle stringset, Handle name) { if (!stringset->Has(name)) { stringset = EnsureCapacity(stringset, 1); - uint32_t hash = StringSetShape::Hash(*name); + uint32_t hash = ShapeT::Hash(name->GetIsolate(), *name); int entry = stringset->FindInsertionEntry(hash); stringset->set(EntryToIndex(entry), *name); stringset->ElementAdded(); @@ -17619,7 +17617,7 @@ Handle Dictionary::Add(Handle dictionary, PropertyDetails details, int* entry_out) { Isolate* isolate = dictionary->GetIsolate(); - uint32_t hash = dictionary->Hash(key); + uint32_t hash = Shape::Hash(isolate, key); // Valdate key is absent. SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound)); // Check whether the dictionary should be extended. @@ -18066,7 +18064,8 @@ Handle WeakHashTable::Put(Handle table, // Check whether the hash table should be extended. table = EnsureCapacity(table, 1, TENURED); - table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value); + uint32_t hash = ShapeT::Hash(isolate, key); + table->AddEntry(table->FindInsertionEntry(hash), key_cell, value); return table; } diff --git a/src/objects/code-cache.h b/src/objects/code-cache.h index 1ced250b49..8cf2e68e5d 100644 --- a/src/objects/code-cache.h +++ b/src/objects/code-cache.h @@ -66,11 +66,11 @@ class CodeCacheHashTableShape : public BaseShape { return key->IsMatch(value); } - static inline uint32_t Hash(CodeCacheHashTableKey* key) { + static inline uint32_t Hash(Isolate* isolate, CodeCacheHashTableKey* key) { return key->Hash(); } - static inline uint32_t HashForObject(Object* object) { + static inline uint32_t HashForObject(Isolate* isolate, Object* object) { FixedArray* pair = FixedArray::cast(object); Name* name = Name::cast(pair->get(0)); Code* code = Code::cast(pair->get(1)); diff --git a/src/objects/compilation-cache-inl.h b/src/objects/compilation-cache-inl.h index 7704286332..acf6e76510 100644 --- a/src/objects/compilation-cache-inl.h +++ b/src/objects/compilation-cache-inl.h @@ -39,7 +39,8 @@ uint32_t CompilationCacheShape::StringSharedHash(String* source, return hash; } -uint32_t CompilationCacheShape::HashForObject(Object* object) { +uint32_t CompilationCacheShape::HashForObject(Isolate* isolate, + Object* object) { if (object->IsNumber()) return static_cast(object->Number()); FixedArray* val = FixedArray::cast(object); diff --git a/src/objects/compilation-cache.h b/src/objects/compilation-cache.h index 29fb6a12ea..2e39d9438b 100644 --- a/src/objects/compilation-cache.h +++ b/src/objects/compilation-cache.h @@ -19,7 +19,9 @@ class CompilationCacheShape : public BaseShape { return key->IsMatch(value); } - static inline uint32_t Hash(HashTableKey* key) { return key->Hash(); } + static inline uint32_t Hash(Isolate* isolate, HashTableKey* key) { + return key->Hash(); + } static inline uint32_t RegExpHash(String* string, Smi* flags); @@ -28,7 +30,7 @@ class CompilationCacheShape : public BaseShape { LanguageMode language_mode, int position); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static const int kPrefixSize = 0; static const int kEntrySize = 3; diff --git a/src/objects/dictionary.h b/src/objects/dictionary.h index 5f3211d11d..69df0c4312 100644 --- a/src/objects/dictionary.h +++ b/src/objects/dictionary.h @@ -122,8 +122,8 @@ class BaseDictionaryShape : public BaseShape { class NameDictionaryShape : public BaseDictionaryShape> { public: static inline bool IsMatch(Handle key, Object* other); - static inline uint32_t Hash(Handle key); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t Hash(Isolate* isolate, Handle key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static inline Handle AsHandle(Isolate* isolate, Handle key); static const int kPrefixSize = 1; static const int kEntrySize = 3; @@ -222,12 +222,11 @@ class NumberDictionaryShape : public BaseDictionaryShape { class SeededNumberDictionaryShape : public NumberDictionaryShape { public: - static const bool UsesSeed = true; static const int kPrefixSize = 1; static const int kEntrySize = 3; - static inline uint32_t SeededHash(uint32_t key, uint32_t seed); - static inline uint32_t SeededHashForObject(uint32_t seed, Object* object); + static inline uint32_t Hash(Isolate* isolate, uint32_t key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); }; class UnseededNumberDictionaryShape : public NumberDictionaryShape { @@ -235,8 +234,8 @@ class UnseededNumberDictionaryShape : public NumberDictionaryShape { static const int kPrefixSize = 0; static const int kEntrySize = 2; - static inline uint32_t Hash(uint32_t key); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t Hash(Isolate* isolate, uint32_t key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); template static inline PropertyDetails DetailsAt(Dictionary* dict, int entry) { diff --git a/src/objects/hash-table-inl.h b/src/objects/hash-table-inl.h index d52f003d36..5fb84a6717 100644 --- a/src/objects/hash-table-inl.h +++ b/src/objects/hash-table-inl.h @@ -10,24 +10,6 @@ namespace v8 { namespace internal { -template -uint32_t HashTable::Hash(Key key) { - if (Shape::UsesSeed) { - return Shape::SeededHash(key, GetHeap()->HashSeed()); - } else { - return Shape::Hash(key); - } -} - -template -uint32_t HashTable::HashForObject(Object* object) { - if (Shape::UsesSeed) { - return Shape::SeededHashForObject(GetHeap()->HashSeed(), object); - } else { - return Shape::HashForObject(object); - } -} - } // namespace internal } // namespace v8 diff --git a/src/objects/hash-table.h b/src/objects/hash-table.h index ad1bcd135c..b0180711d7 100644 --- a/src/objects/hash-table.h +++ b/src/objects/hash-table.h @@ -34,9 +34,9 @@ namespace internal { // // Tells whether key matches other. // static bool IsMatch(Key key, Object* other); // // Returns the hash value for key. -// static uint32_t Hash(Key key); +// static uint32_t Hash(Isolate* isolate, Key key); // // Returns the hash value for object. -// static uint32_t HashForObject(Object* object); +// static uint32_t HashForObject(Isolate* isolate, Object* object); // // Convert key to an object. // static inline Handle AsHandle(Isolate* isolate, Key key); // // The prefix size indicates number of elements in the beginning @@ -56,17 +56,6 @@ template class BaseShape { public: typedef KeyT Key; - static const bool UsesSeed = false; - static uint32_t Hash(Key key) { return 0; } - static uint32_t SeededHash(Key key, uint32_t seed) { - DCHECK(UsesSeed); - return Hash(key); - } - static uint32_t HashForObject(Object* object) { return 0; } - static uint32_t SeededHashForObject(uint32_t seed, Object* object) { - DCHECK(UsesSeed); - return HashForObject(object); - } static inline Map* GetMap(Isolate* isolate); static const bool kNeedsHoleCheck = true; }; @@ -97,7 +86,7 @@ class V8_EXPORT_PRIVATE HashTableBase : public NON_EXPORTED_BASE(FixedArray) { // Tells whether k is a real key. The hole and undefined are not allowed // as keys and can be used to indicate missing or deleted elements. - inline bool IsKey(Isolate* isolate, Object* k); + static inline bool IsKey(Isolate* isolate, Object* k); // Compute the probe offset (quadratic probing). INLINE(static uint32_t GetProbeOffset(uint32_t n)) { @@ -144,11 +133,6 @@ class HashTable : public HashTableBase { typedef Shape ShapeT; typedef typename Shape::Key Key; - // Wrapper methods. Defined in src/objects/hash-table-inl.h - // to break a cycle with src/heap/heap.h. - inline uint32_t Hash(Key key); - inline uint32_t HashForObject(Object* object); - // Returns a new HashTable object. MUST_USE_RESULT static Handle New( Isolate* isolate, int at_least_space_for, @@ -271,8 +255,8 @@ class HashTableKey { class ObjectHashTableShape : public BaseShape> { public: static inline bool IsMatch(Handle key, Object* other); - static inline uint32_t Hash(Handle key); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t Hash(Isolate* isolate, Handle key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static inline Handle AsHandle(Isolate* isolate, Handle key); static const int kPrefixSize = 0; static const int kEntrySize = 2; @@ -573,8 +557,8 @@ template class WeakHashTableShape : public BaseShape> { public: static inline bool IsMatch(Handle key, Object* other); - static inline uint32_t Hash(Handle key); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t Hash(Isolate* isolate, Handle key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static inline Handle AsHandle(Isolate* isolate, Handle key); static const int kPrefixSize = 0; static const int kEntrySize = entrysize; diff --git a/src/objects/string-table.h b/src/objects/string-table.h index f150aba099..1fa3c71bc1 100644 --- a/src/objects/string-table.h +++ b/src/objects/string-table.h @@ -36,9 +36,9 @@ class StringTableShape : public BaseShape { return key->IsMatch(value); } - static inline uint32_t Hash(Key key) { return key->Hash(); } + static inline uint32_t Hash(Isolate* isolate, Key key) { return key->Hash(); } - static inline uint32_t HashForObject(Object* object); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static inline Handle AsHandle(Isolate* isolate, Key key); @@ -81,8 +81,8 @@ class StringTable : public HashTable { class StringSetShape : public BaseShape { public: static inline bool IsMatch(String* key, Object* value); - static inline uint32_t Hash(String* key); - static inline uint32_t HashForObject(Object* object); + static inline uint32_t Hash(Isolate* isolate, String* key); + static inline uint32_t HashForObject(Isolate* isolate, Object* object); static const int kPrefixSize = 0; static const int kEntrySize = 1;