Commit 55be0419 authored by Victor Gomes's avatar Victor Gomes Committed by V8 LUCI CQ

[runtime] Adds a hashtable object (name => index)

In preparation to use the hash table in the scope_info, we
setup a hashtable from name to indices.

Bug: v8:12315
Change-Id: I77f1eb40191c2fb2d40127e1e84dbc41ca2e4b70
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3386804Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Victor Gomes <victorgomes@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78646}
parent 44a8a7d6
......@@ -209,6 +209,7 @@ extern class HashTable extends FixedArray generates 'TNode<FixedArray>';
extern class OrderedHashMap extends HashTable;
extern class OrderedHashSet extends HashTable;
extern class OrderedNameDictionary extends HashTable;
extern class NameToIndexHashTable extends HashTable;
extern class NameDictionary extends HashTable;
extern class GlobalDictionary extends HashTable;
extern class SimpleNumberDictionary extends HashTable;
......
......@@ -202,6 +202,7 @@ void HeapObject::HeapObjectVerify(Isolate* isolate) {
case ORDERED_HASH_MAP_TYPE:
case ORDERED_HASH_SET_TYPE:
case ORDERED_NAME_DICTIONARY_TYPE:
case NAME_TO_INDEX_HASH_TABLE_TYPE:
case NAME_DICTIONARY_TYPE:
case GLOBAL_DICTIONARY_TYPE:
case NUMBER_DICTIONARY_TYPE:
......
......@@ -138,6 +138,9 @@ void HeapObject::HeapObjectPrint(std::ostream& os) {
case HASH_TABLE_TYPE:
ObjectHashTable::cast(*this).ObjectHashTablePrint(os);
break;
case NAME_TO_INDEX_HASH_TABLE_TYPE:
NameToIndexHashTable::cast(*this).NameToIndexHashTablePrint(os);
break;
case ORDERED_HASH_MAP_TYPE:
OrderedHashMap::cast(*this).OrderedHashMapPrint(os);
break;
......@@ -961,6 +964,11 @@ void ObjectHashTable::ObjectHashTablePrint(std::ostream& os) {
PrintHashMapContentsFull(os, *this);
}
void NameToIndexHashTable::NameToIndexHashTablePrint(std::ostream& os) {
PrintHashTableHeader(os, *this, "NameToIndexHashTable");
PrintHashMapContentsFull(os, *this);
}
void NumberDictionary::NumberDictionaryPrint(std::ostream& os) {
PrintHashTableHeader(os, *this, "NumberDictionary");
PrintDictionaryContentsFull(os, *this);
......
......@@ -476,6 +476,8 @@ bool Heap::CreateInitialMaps() {
ALLOCATE_VARSIZE_MAP(NUMBER_DICTIONARY_TYPE, number_dictionary)
ALLOCATE_VARSIZE_MAP(SIMPLE_NUMBER_DICTIONARY_TYPE,
simple_number_dictionary)
ALLOCATE_VARSIZE_MAP(NAME_TO_INDEX_HASH_TABLE_TYPE,
name_to_index_hash_table)
ALLOCATE_VARSIZE_MAP(EMBEDDER_DATA_ARRAY_TYPE, embedder_data_array)
ALLOCATE_VARSIZE_MAP(EPHEMERON_HASH_TABLE_TYPE, ephemeron_hash_table)
......
......@@ -45,9 +45,15 @@ ObjectHashSet::ObjectHashSet(Address ptr)
SLOW_DCHECK(IsObjectHashSet());
}
NameToIndexHashTable::NameToIndexHashTable(Address ptr)
: HashTable<NameToIndexHashTable, NameToIndexShape>(ptr) {
SLOW_DCHECK(IsNameToIndexHashTable());
}
CAST_ACCESSOR(ObjectHashTable)
CAST_ACCESSOR(EphemeronHashTable)
CAST_ACCESSOR(ObjectHashSet)
CAST_ACCESSOR(NameToIndexHashTable)
void EphemeronHashTable::set_key(int index, Object value) {
DCHECK_NE(GetReadOnlyRoots().fixed_cow_array_map(), map());
......@@ -124,6 +130,11 @@ Handle<Map> HashTable<Derived, Shape>::GetMap(ReadOnlyRoots roots) {
return roots.hash_table_map_handle();
}
// static
Handle<Map> NameToIndexHashTable::GetMap(ReadOnlyRoots roots) {
return roots.name_to_index_hash_table_map_handle();
}
// static
Handle<Map> EphemeronHashTable::GetMap(ReadOnlyRoots roots) {
return roots.ephemeron_hash_table_map_handle();
......@@ -247,6 +258,18 @@ bool ObjectHashTableShape::IsMatch(Handle<Object> key, Object other) {
return key->SameValue(other);
}
bool NameToIndexShape::IsMatch(Handle<Name> key, Object other) {
return *key == other;
}
uint32_t NameToIndexShape::HashForObject(ReadOnlyRoots roots, Object other) {
return Name::cast(other).hash();
}
uint32_t NameToIndexShape::Hash(ReadOnlyRoots roots, Handle<Name> key) {
return key->hash();
}
uint32_t ObjectHashTableShape::Hash(ReadOnlyRoots roots, Handle<Object> key) {
return Smi::ToInt(key->GetHash());
}
......
......@@ -437,6 +437,42 @@ class V8_EXPORT_PRIVATE ObjectHashSet
HashTable<ObjectHashSet, ObjectHashSetShape>);
};
class NameToIndexShape : public BaseShape<Handle<Name>> {
public:
static inline bool IsMatch(Handle<Name> key, Object other);
static inline uint32_t Hash(ReadOnlyRoots roots, Handle<Name> key);
static inline uint32_t HashForObject(ReadOnlyRoots roots, Object object);
static inline Handle<Object> AsHandle(Handle<Name> key);
static const int kPrefixSize = 0;
static const int kEntryValueIndex = 1;
static const int kEntrySize = 2;
static const bool kMatchNeedsHoleCheck = false;
};
class V8_EXPORT_PRIVATE NameToIndexHashTable
: public HashTable<NameToIndexHashTable, NameToIndexShape> {
public:
inline static Handle<Map> GetMap(ReadOnlyRoots roots);
int32_t Lookup(Handle<Name> key);
// Returns the value at entry.
Object ValueAt(InternalIndex entry);
V8_WARN_UNUSED_RESULT static Handle<NameToIndexHashTable> Add(
Isolate* isolate, Handle<NameToIndexHashTable> table, Handle<Name> key,
int32_t value);
DECL_CAST(NameToIndexHashTable)
DECL_PRINTER(NameToIndexHashTable)
OBJECT_CONSTRUCTORS(NameToIndexHashTable,
HashTable<NameToIndexHashTable, NameToIndexShape>);
private:
static inline int EntryToValueIndex(InternalIndex entry) {
return EntryToIndex(entry) + NameToIndexShape::kEntryValueIndex;
}
};
} // namespace internal
} // namespace v8
......
......@@ -129,6 +129,7 @@ VisitorId Map::GetVisitorId(Map map) {
return kVisitEmbedderDataArray;
case OBJECT_BOILERPLATE_DESCRIPTION_TYPE:
case NAME_TO_INDEX_HASH_TABLE_TYPE:
case CLOSURE_FEEDBACK_CELL_ARRAY_TYPE:
case HASH_TABLE_TYPE:
case ORDERED_HASH_MAP_TYPE:
......
......@@ -183,6 +183,7 @@ class ZoneForwardList;
V(Microtask) \
V(Name) \
V(NameDictionary) \
V(NameToIndexHashTable) \
V(NativeContext) \
V(NormalizedMapCache) \
V(NumberDictionary) \
......
......@@ -1036,6 +1036,7 @@ auto BodyDescriptorApply(InstanceType type, Args&&... args) {
case GLOBAL_DICTIONARY_TYPE:
case NUMBER_DICTIONARY_TYPE:
case SIMPLE_NUMBER_DICTIONARY_TYPE:
case NAME_TO_INDEX_HASH_TABLE_TYPE:
case SCRIPT_CONTEXT_TABLE_TYPE:
return CALL_APPLY(FixedArray);
case EPHEMERON_HASH_TABLE_TYPE:
......
......@@ -2326,6 +2326,7 @@ bool HeapObject::NeedsRehashing(InstanceType instance_type) const {
case ORDERED_HASH_SET_TYPE:
return false; // We'll rehash from the JSMap or JSSet referencing them.
case NAME_DICTIONARY_TYPE:
case NAME_TO_INDEX_HASH_TABLE_TYPE:
case GLOBAL_DICTIONARY_TYPE:
case NUMBER_DICTIONARY_TYPE:
case SIMPLE_NUMBER_DICTIONARY_TYPE:
......@@ -2354,6 +2355,7 @@ bool HeapObject::CanBeRehashed(PtrComprCageBase cage_base) const {
case ORDERED_NAME_DICTIONARY_TYPE:
return false;
case NAME_DICTIONARY_TYPE:
case NAME_TO_INDEX_HASH_TABLE_TYPE:
case GLOBAL_DICTIONARY_TYPE:
case NUMBER_DICTIONARY_TYPE:
case SIMPLE_NUMBER_DICTIONARY_TYPE:
......@@ -2383,6 +2385,9 @@ void HeapObject::RehashBasedOnMap(IsolateT* isolate) {
case NAME_DICTIONARY_TYPE:
NameDictionary::cast(*this).Rehash(isolate);
break;
case NAME_TO_INDEX_HASH_TABLE_TYPE:
NameToIndexHashTable::cast(*this).Rehash(isolate);
break;
case SWISS_NAME_DICTIONARY_TYPE:
SwissNameDictionary::cast(*this).Rehash(isolate);
break;
......@@ -6235,6 +6240,33 @@ Object ObjectHashTableBase<Derived, Shape>::Lookup(PtrComprCageBase cage_base,
return this->get(Derived::EntryToIndex(entry) + 1);
}
int32_t NameToIndexHashTable::Lookup(Handle<Name> key) {
DisallowGarbageCollection no_gc;
PtrComprCageBase cage_base = GetPtrComprCageBase(*this);
ReadOnlyRoots roots = this->GetReadOnlyRoots(cage_base);
InternalIndex entry = this->FindEntry(cage_base, roots, key, key->hash());
if (entry.is_not_found()) return -1;
return Smi::cast(this->get(EntryToValueIndex(entry))).value();
}
Handle<NameToIndexHashTable> NameToIndexHashTable::Add(
Isolate* isolate, Handle<NameToIndexHashTable> table, Handle<Name> key,
int32_t index) {
DCHECK_GE(index, 0);
// Validate that the key is absent.
SLOW_DCHECK(table->FindEntry(isolate, key).is_not_found());
// Check whether the dictionary should be extended.
table = EnsureCapacity(isolate, table);
// Compute the key object.
InternalIndex entry = table->FindInsertionEntry(isolate, key->hash());
table->set(EntryToIndex(entry), *key);
table->set(EntryToValueIndex(entry), Smi::FromInt(index));
table->ElementAdded();
return table;
}
template <typename Derived, typename Shape>
Object ObjectHashTableBase<Derived, Shape>::Lookup(Handle<Object> key) {
DisallowGarbageCollection no_gc;
......@@ -6262,6 +6294,10 @@ Object ObjectHashTableBase<Derived, Shape>::ValueAt(InternalIndex entry) {
return this->get(EntryToValueIndex(entry));
}
Object NameToIndexHashTable::ValueAt(InternalIndex entry) {
return this->get(EntryToValueIndex(entry));
}
template <typename Derived, typename Shape>
Handle<Derived> ObjectHashTableBase<Derived, Shape>::Put(Handle<Derived> table,
Handle<Object> key,
......@@ -6836,6 +6872,7 @@ Address Smi::LexicographicCompare(Isolate* isolate, Smi x, Smi y) {
EXTERN_DEFINE_HASH_TABLE(StringSet, StringSetShape)
EXTERN_DEFINE_HASH_TABLE(CompilationCacheTable, CompilationCacheShape)
EXTERN_DEFINE_HASH_TABLE(ObjectHashSet, ObjectHashSetShape)
EXTERN_DEFINE_HASH_TABLE(NameToIndexHashTable, NameToIndexShape)
EXTERN_DEFINE_OBJECT_BASE_HASH_TABLE(ObjectHashTable, ObjectHashTableShape)
EXTERN_DEFINE_OBJECT_BASE_HASH_TABLE(EphemeronHashTable, ObjectHashTableShape)
......
......@@ -96,6 +96,7 @@ class Symbol;
V(Map, one_closure_cell_map, OneClosureCellMap) \
V(Map, ordered_hash_map_map, OrderedHashMapMap) \
V(Map, ordered_hash_set_map, OrderedHashSetMap) \
V(Map, name_to_index_hash_table_map, NameToIndexHashTableMap) \
V(Map, ordered_name_dictionary_map, OrderedNameDictionaryMap) \
V(Map, preparse_data_map, PreparseDataMap) \
V(Map, property_array_map, PropertyArrayMap) \
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment