Commit 8f54d18b authored by Marja Hölttä's avatar Marja Hölttä Committed by Commit Bot

[objects.h splitting] Move out HashTable and related classes.

BUG=v8:5402
R=mstarzinger@chromium.org

Change-Id: I8ce43504fee83dcb6859418a526b2c7aea52e778
Reviewed-on: https://chromium-review.googlesource.com/468968
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44436}
parent 0344b73e
......@@ -1700,10 +1700,15 @@ v8_source_set("v8_base") {
"src/objects-printer.cc",
"src/objects.cc",
"src/objects.h",
"src/objects/code-cache-inl.h",
"src/objects/code-cache.h",
"src/objects/compilation-cache-inl.h",
"src/objects/compilation-cache.h",
"src/objects/descriptor-array.h",
"src/objects/dictionary.h",
"src/objects/frame-array-inl.h",
"src/objects/frame-array.h",
"src/objects/hash-table.h",
"src/objects/literal-objects.cc",
"src/objects/literal-objects.h",
"src/objects/module-info.h",
......@@ -1712,6 +1717,7 @@ v8_source_set("v8_base") {
"src/objects/regexp-match-info.h",
"src/objects/scope-info.cc",
"src/objects/scope-info.h",
"src/objects/string-table.h",
"src/ostreams.cc",
"src/ostreams.h",
"src/parsing/duplicate-finder.h",
......
......@@ -8,6 +8,7 @@
#include "src/factory.h"
#include "src/globals.h"
#include "src/objects-inl.h"
#include "src/objects/compilation-cache-inl.h"
namespace v8 {
namespace internal {
......
......@@ -6,7 +6,7 @@
#define V8_COMPILATION_CACHE_H_
#include "src/allocation.h"
#include "src/objects.h"
#include "src/objects/compilation-cache.h"
namespace v8 {
namespace internal {
......
......@@ -19,6 +19,8 @@
#include "src/heap-symbols.h"
#include "src/list.h"
#include "src/objects.h"
#include "src/objects/hash-table.h"
#include "src/objects/string-table.h"
namespace v8 {
namespace internal {
......
......@@ -10,6 +10,8 @@
#include "src/heap/heap-inl.h"
#include "src/isolate.h"
#include "src/macro-assembler.h"
#include "src/objects/code-cache-inl.h"
#include "src/objects/compilation-cache-inl.h"
#include "src/utils.h"
namespace v8 {
......
......@@ -616,8 +616,6 @@ CAST_ACCESSOR(ByteArray)
CAST_ACCESSOR(BytecodeArray)
CAST_ACCESSOR(Cell)
CAST_ACCESSOR(Code)
CAST_ACCESSOR(CodeCacheHashTable)
CAST_ACCESSOR(CompilationCacheTable)
CAST_ACCESSOR(ConsString)
CAST_ACCESSOR(DeoptimizationInputData)
CAST_ACCESSOR(DeoptimizationOutputData)
......@@ -923,16 +921,6 @@ Handle<Object> StringTableShape::AsHandle(Isolate* isolate, HashTableKey* key) {
return key->AsHandle(isolate);
}
Handle<Object> CompilationCacheShape::AsHandle(Isolate* isolate,
HashTableKey* key) {
return key->AsHandle(isolate);
}
Handle<Object> CodeCacheHashTableShape::AsHandle(Isolate* isolate,
HashTableKey* key) {
return key->AsHandle(isolate);
}
template <typename Char>
class SequentialStringKey : public HashTableKey {
public:
......
......@@ -55,6 +55,8 @@
#include "src/map-updater.h"
#include "src/messages.h"
#include "src/objects-body-descriptors-inl.h"
#include "src/objects/code-cache-inl.h"
#include "src/objects/compilation-cache-inl.h"
#include "src/objects/frame-array-inl.h"
#include "src/property-descriptor.h"
#include "src/prototype.h"
......
......@@ -3045,668 +3045,6 @@ inline int Search(T* array, Name* name, int valid_entries = 0,
int* out_insertion_index = NULL);
// HashTable is a subclass of FixedArray that implements a hash table
// that uses open addressing and quadratic probing.
//
// In order for the quadratic probing to work, elements that have not
// yet been used and elements that have been deleted are
// distinguished. Probing continues when deleted elements are
// encountered and stops when unused elements are encountered.
//
// - Elements with key == undefined have not been used yet.
// - Elements with key == the_hole have been deleted.
//
// The hash table class is parameterized with a Shape and a Key.
// Shape must be a class with the following interface:
// class ExampleShape {
// public:
// // Tells whether key matches other.
// static bool IsMatch(Key key, Object* other);
// // Returns the hash value for key.
// static uint32_t Hash(Key key);
// // Returns the hash value for object.
// static uint32_t HashForObject(Key key, Object* object);
// // Convert key to an object.
// static inline Handle<Object> AsHandle(Isolate* isolate, Key key);
// // The prefix size indicates number of elements in the beginning
// // of the backing storage.
// static const int kPrefixSize = ..;
// // The Element size indicates number of elements per entry.
// static const int kEntrySize = ..;
// };
// The prefix size indicates an amount of memory in the
// beginning of the backing storage that can be used for non-element
// information by subclasses.
template<typename Key>
class BaseShape {
public:
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(Key key, Object* object) { return 0; }
static uint32_t SeededHashForObject(Key key, uint32_t seed, Object* object) {
DCHECK(UsesSeed);
return HashForObject(key, object);
}
static inline Map* GetMap(Isolate* isolate);
};
class HashTableBase : public FixedArray {
public:
// Returns the number of elements in the hash table.
inline int NumberOfElements();
// Returns the number of deleted elements in the hash table.
inline int NumberOfDeletedElements();
// Returns the capacity of the hash table.
inline int Capacity();
// ElementAdded should be called whenever an element is added to a
// hash table.
inline void ElementAdded();
// ElementRemoved should be called whenever an element is removed from
// a hash table.
inline void ElementRemoved();
inline void ElementsRemoved(int n);
// Computes the required capacity for a table holding the given
// number of elements. May be more than HashTable::kMaxCapacity.
static inline int ComputeCapacity(int at_least_space_for);
// 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);
// Compute the probe offset (quadratic probing).
INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
return (n + n * n) >> 1;
}
static const int kNumberOfElementsIndex = 0;
static const int kNumberOfDeletedElementsIndex = 1;
static const int kCapacityIndex = 2;
static const int kPrefixStartIndex = 3;
// Constant used for denoting a absent entry.
static const int kNotFound = -1;
// Minimum capacity for newly created hash tables.
static const int kMinCapacity = 4;
protected:
// Update the number of elements in the hash table.
inline void SetNumberOfElements(int nof);
// Update the number of deleted elements in the hash table.
inline void SetNumberOfDeletedElements(int nod);
// Returns probe entry.
static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
DCHECK(base::bits::IsPowerOfTwo32(size));
return (hash + GetProbeOffset(number)) & (size - 1);
}
inline static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
return hash & (size - 1);
}
inline static uint32_t NextProbe(
uint32_t last, uint32_t number, uint32_t size) {
return (last + number) & (size - 1);
}
};
template <typename Derived, typename Shape, typename Key>
class HashTable : public HashTableBase {
public:
typedef Shape ShapeT;
// Wrapper methods
inline uint32_t Hash(Key key) {
if (Shape::UsesSeed) {
return Shape::SeededHash(key, GetHeap()->HashSeed());
} else {
return Shape::Hash(key);
}
}
inline uint32_t HashForObject(Key key, Object* object) {
if (Shape::UsesSeed) {
return Shape::SeededHashForObject(key, GetHeap()->HashSeed(), object);
} else {
return Shape::HashForObject(key, object);
}
}
// Returns a new HashTable object.
MUST_USE_RESULT static Handle<Derived> New(
Isolate* isolate, int at_least_space_for,
MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY,
PretenureFlag pretenure = NOT_TENURED);
DECLARE_CAST(HashTable)
// Garbage collection support.
void IteratePrefix(ObjectVisitor* visitor);
void IterateElements(ObjectVisitor* visitor);
// Find entry for key otherwise return kNotFound.
inline int FindEntry(Key key);
inline int FindEntry(Isolate* isolate, Key key, int32_t hash);
int FindEntry(Isolate* isolate, Key key);
inline bool Has(Isolate* isolate, Key key);
inline bool Has(Key key);
// Rehashes the table in-place.
void Rehash(Key key);
// Returns the key at entry.
Object* KeyAt(int entry) { return get(EntryToIndex(entry) + kEntryKeyIndex); }
static const int kElementsStartIndex = kPrefixStartIndex + Shape::kPrefixSize;
static const int kEntrySize = Shape::kEntrySize;
STATIC_ASSERT(kEntrySize > 0);
static const int kEntryKeyIndex = 0;
static const int kElementsStartOffset =
kHeaderSize + kElementsStartIndex * kPointerSize;
// Maximal capacity of HashTable. Based on maximal length of underlying
// FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
// cannot overflow.
static const int kMaxCapacity =
(FixedArray::kMaxLength - kElementsStartIndex) / kEntrySize;
// Returns the index for an entry (of the key)
static inline int EntryToIndex(int entry) {
return (entry * kEntrySize) + kElementsStartIndex;
}
protected:
friend class ObjectHashTable;
MUST_USE_RESULT static Handle<Derived> New(Isolate* isolate, int capacity,
PretenureFlag pretenure);
// Find the entry at which to insert element with the given key that
// has the given hash value.
uint32_t FindInsertionEntry(uint32_t hash);
// Attempt to shrink hash table after removal of key.
MUST_USE_RESULT static Handle<Derived> Shrink(Handle<Derived> table, Key key);
// Ensure enough space for n additional elements.
MUST_USE_RESULT static Handle<Derived> EnsureCapacity(
Handle<Derived> table,
int n,
Key key,
PretenureFlag pretenure = NOT_TENURED);
// Returns true if this table has sufficient capacity for adding n elements.
bool HasSufficientCapacityToAdd(int number_of_additional_elements);
// Sets the capacity of the hash table.
void SetCapacity(int capacity) {
// To scale a computed hash code to fit within the hash table, we
// use bit-wise AND with a mask, so the capacity must be positive
// and non-zero.
DCHECK(capacity > 0);
DCHECK(capacity <= kMaxCapacity);
set(kCapacityIndex, Smi::FromInt(capacity));
}
private:
// Returns _expected_ if one of entries given by the first _probe_ probes is
// equal to _expected_. Otherwise, returns the entry given by the probe
// number _probe_.
uint32_t EntryForProbe(Key key, Object* k, int probe, uint32_t expected);
void Swap(uint32_t entry1, uint32_t entry2, WriteBarrierMode mode);
// Rehashes this hash-table into the new table.
void Rehash(Handle<Derived> new_table, Key key);
};
// HashTableKey is an abstract superclass for virtual key behavior.
class HashTableKey {
public:
// Returns whether the other object matches this key.
virtual bool IsMatch(Object* other) = 0;
// Returns the hash value for this key.
virtual uint32_t Hash() = 0;
// Returns the hash value for object.
virtual uint32_t HashForObject(Object* key) = 0;
// Returns the key object for storing into the hash table.
MUST_USE_RESULT virtual Handle<Object> AsHandle(Isolate* isolate) = 0;
// Required.
virtual ~HashTableKey() {}
};
class StringTableShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) {
return key->Hash();
}
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class SeqOneByteString;
// StringTable.
//
// No special elements in the prefix and the element size is 1
// because only the string itself (the key) needs to be stored.
class StringTable: public HashTable<StringTable,
StringTableShape,
HashTableKey*> {
public:
// Find string in the string table. If it is not there yet, it is
// added. The return value is the string found.
V8_EXPORT_PRIVATE static Handle<String> LookupString(Isolate* isolate,
Handle<String> key);
static Handle<String> LookupKey(Isolate* isolate, HashTableKey* key);
static String* LookupKeyIfExists(Isolate* isolate, HashTableKey* key);
// Tries to internalize given string and returns string handle on success
// or an empty handle otherwise.
MUST_USE_RESULT static MaybeHandle<String> InternalizeStringIfExists(
Isolate* isolate,
Handle<String> string);
// Looks up a string that is equal to the given string and returns
// string handle if it is found, or an empty handle otherwise.
MUST_USE_RESULT static MaybeHandle<String> LookupStringIfExists(
Isolate* isolate,
Handle<String> str);
MUST_USE_RESULT static MaybeHandle<String> LookupTwoCharsStringIfExists(
Isolate* isolate,
uint16_t c1,
uint16_t c2);
static void EnsureCapacityForDeserialization(Isolate* isolate, int expected);
DECLARE_CAST(StringTable)
private:
template <bool seq_one_byte>
friend class JsonParser;
DISALLOW_IMPLICIT_CONSTRUCTORS(StringTable);
};
class StringSetShape : public BaseShape<String*> {
public:
static inline bool IsMatch(String* key, Object* value);
static inline uint32_t Hash(String* key);
static inline uint32_t HashForObject(String* key, Object* object);
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class StringSet : public HashTable<StringSet, StringSetShape, String*> {
public:
static Handle<StringSet> New(Isolate* isolate);
static Handle<StringSet> Add(Handle<StringSet> blacklist,
Handle<String> name);
bool Has(Handle<String> name);
DECLARE_CAST(StringSet)
};
class ObjectHashTableShape : public BaseShape<Handle<Object> > {
public:
static inline bool IsMatch(Handle<Object> key, Object* other);
static inline uint32_t Hash(Handle<Object> key);
static inline uint32_t HashForObject(Handle<Object> key, Object* object);
static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
static const int kPrefixSize = 0;
static const int kEntrySize = 2;
};
// ObjectHashTable maps keys that are arbitrary objects to object values by
// using the identity hash of the key for hashing purposes.
class ObjectHashTable: public HashTable<ObjectHashTable,
ObjectHashTableShape,
Handle<Object> > {
typedef HashTable<
ObjectHashTable, ObjectHashTableShape, Handle<Object> > DerivedHashTable;
public:
DECLARE_CAST(ObjectHashTable)
// Attempt to shrink hash table after removal of key.
MUST_USE_RESULT static inline Handle<ObjectHashTable> Shrink(
Handle<ObjectHashTable> table,
Handle<Object> key);
// Looks up the value associated with the given key. The hole value is
// returned in case the key is not present.
Object* Lookup(Handle<Object> key);
Object* Lookup(Handle<Object> key, int32_t hash);
Object* Lookup(Isolate* isolate, Handle<Object> key, int32_t hash);
// Returns the value at entry.
Object* ValueAt(int entry);
// Adds (or overwrites) the value associated with the given key.
static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
Handle<Object> key,
Handle<Object> value);
static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
Handle<Object> key, Handle<Object> value,
int32_t hash);
// Returns an ObjectHashTable (possibly |table|) where |key| has been removed.
static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
Handle<Object> key,
bool* was_present);
static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
Handle<Object> key, bool* was_present,
int32_t hash);
protected:
friend class MarkCompactCollector;
void AddEntry(int entry, Object* key, Object* value);
void RemoveEntry(int entry);
// Returns the index to the value of an entry.
static inline int EntryToValueIndex(int entry) {
return EntryToIndex(entry) + 1;
}
};
class ObjectHashSetShape : public ObjectHashTableShape {
public:
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class ObjectHashSet
: public HashTable<ObjectHashSet, ObjectHashSetShape, Handle<Object>> {
public:
static Handle<ObjectHashSet> Add(Handle<ObjectHashSet> set,
Handle<Object> key);
inline bool Has(Isolate* isolate, Handle<Object> key, int32_t hash);
inline bool Has(Isolate* isolate, Handle<Object> key);
DECLARE_CAST(ObjectHashSet)
};
// OrderedHashTable is a HashTable with Object keys that preserves
// insertion order. There are Map and Set interfaces (OrderedHashMap
// and OrderedHashTable, below). It is meant to be used by JSMap/JSSet.
//
// Only Object* keys are supported, with Object::SameValueZero() used as the
// equality operator and Object::GetHash() for the hash function.
//
// Based on the "Deterministic Hash Table" as described by Jason Orendorff at
// https://wiki.mozilla.org/User:Jorend/Deterministic_hash_tables
// Originally attributed to Tyler Close.
//
// Memory layout:
// [0]: bucket count
// [1]: element count
// [2]: deleted element count
// [3..(3 + NumberOfBuckets() - 1)]: "hash table", where each item is an
// offset into the data table (see below) where the
// first item in this bucket is stored.
// [3 + NumberOfBuckets()..length]: "data table", an array of length
// Capacity() * kEntrySize, where the first entrysize
// items are handled by the derived class and the
// item at kChainOffset is another entry into the
// data table indicating the next entry in this hash
// bucket.
//
// When we transition the table to a new version we obsolete it and reuse parts
// of the memory to store information how to transition an iterator to the new
// table:
//
// Memory layout for obsolete table:
// [0]: bucket count
// [1]: Next newer table
// [2]: Number of removed holes or -1 when the table was cleared.
// [3..(3 + NumberOfRemovedHoles() - 1)]: The indexes of the removed holes.
// [3 + NumberOfRemovedHoles()..length]: Not used
//
template <class Derived, int entrysize>
class OrderedHashTable : public FixedArray {
public:
// Returns an OrderedHashTable with a capacity of at least |capacity|.
static Handle<Derived> Allocate(
Isolate* isolate, int capacity, PretenureFlag pretenure = NOT_TENURED);
// Returns an OrderedHashTable (possibly |table|) with enough space
// to add at least one new element.
static Handle<Derived> EnsureGrowable(Handle<Derived> table);
// Returns an OrderedHashTable (possibly |table|) that's shrunken
// if possible.
static Handle<Derived> Shrink(Handle<Derived> table);
// Returns a new empty OrderedHashTable and records the clearing so that
// existing iterators can be updated.
static Handle<Derived> Clear(Handle<Derived> table);
// Returns a true if the OrderedHashTable contains the key
static bool HasKey(Handle<Derived> table, Handle<Object> key);
int NumberOfElements() {
return Smi::cast(get(kNumberOfElementsIndex))->value();
}
int NumberOfDeletedElements() {
return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
}
// Returns the number of contiguous entries in the data table, starting at 0,
// that either are real entries or have been deleted.
int UsedCapacity() { return NumberOfElements() + NumberOfDeletedElements(); }
int NumberOfBuckets() {
return Smi::cast(get(kNumberOfBucketsIndex))->value();
}
// Returns an index into |this| for the given entry.
int EntryToIndex(int entry) {
return kHashTableStartIndex + NumberOfBuckets() + (entry * kEntrySize);
}
int HashToBucket(int hash) { return hash & (NumberOfBuckets() - 1); }
int HashToEntry(int hash) {
int bucket = HashToBucket(hash);
Object* entry = this->get(kHashTableStartIndex + bucket);
return Smi::cast(entry)->value();
}
int KeyToFirstEntry(Isolate* isolate, Object* key) {
Object* hash = key->GetHash();
// If the object does not have an identity hash, it was never used as a key
if (hash->IsUndefined(isolate)) return kNotFound;
return HashToEntry(Smi::cast(hash)->value());
}
int NextChainEntry(int entry) {
Object* next_entry = get(EntryToIndex(entry) + kChainOffset);
return Smi::cast(next_entry)->value();
}
// use KeyAt(i)->IsTheHole(isolate) to determine if this is a deleted entry.
Object* KeyAt(int entry) {
DCHECK_LT(entry, this->UsedCapacity());
return get(EntryToIndex(entry));
}
bool IsObsolete() {
return !get(kNextTableIndex)->IsSmi();
}
// The next newer table. This is only valid if the table is obsolete.
Derived* NextTable() {
return Derived::cast(get(kNextTableIndex));
}
// When the table is obsolete we store the indexes of the removed holes.
int RemovedIndexAt(int index) {
return Smi::cast(get(kRemovedHolesIndex + index))->value();
}
static const int kNotFound = -1;
static const int kMinCapacity = 4;
static const int kNumberOfElementsIndex = 0;
// The next table is stored at the same index as the nof elements.
static const int kNextTableIndex = kNumberOfElementsIndex;
static const int kNumberOfDeletedElementsIndex = kNumberOfElementsIndex + 1;
static const int kNumberOfBucketsIndex = kNumberOfDeletedElementsIndex + 1;
static const int kHashTableStartIndex = kNumberOfBucketsIndex + 1;
static constexpr const int kNumberOfElementsOffset =
FixedArray::OffsetOfElementAt(kNumberOfElementsIndex);
static constexpr const int kNextTableOffset =
FixedArray::OffsetOfElementAt(kNextTableIndex);
static constexpr const int kNumberOfDeletedElementsOffset =
FixedArray::OffsetOfElementAt(kNumberOfDeletedElementsIndex);
static constexpr const int kNumberOfBucketsOffset =
FixedArray::OffsetOfElementAt(kNumberOfBucketsIndex);
static constexpr const int kHashTableStartOffset =
FixedArray::OffsetOfElementAt(kHashTableStartIndex);
static const int kEntrySize = entrysize + 1;
static const int kChainOffset = entrysize;
static const int kLoadFactor = 2;
// NumberOfDeletedElements is set to kClearedTableSentinel when
// the table is cleared, which allows iterator transitions to
// optimize that case.
static const int kClearedTableSentinel = -1;
protected:
static Handle<Derived> Rehash(Handle<Derived> table, int new_capacity);
void SetNumberOfBuckets(int num) {
set(kNumberOfBucketsIndex, Smi::FromInt(num));
}
void SetNumberOfElements(int num) {
set(kNumberOfElementsIndex, Smi::FromInt(num));
}
void SetNumberOfDeletedElements(int num) {
set(kNumberOfDeletedElementsIndex, Smi::FromInt(num));
}
// Returns the number elements that can fit into the allocated buffer.
int Capacity() {
return NumberOfBuckets() * kLoadFactor;
}
void SetNextTable(Derived* next_table) {
set(kNextTableIndex, next_table);
}
void SetRemovedIndexAt(int index, int removed_index) {
return set(kRemovedHolesIndex + index, Smi::FromInt(removed_index));
}
static const int kRemovedHolesIndex = kHashTableStartIndex;
static const int kMaxCapacity =
(FixedArray::kMaxLength - kHashTableStartIndex)
/ (1 + (kEntrySize * kLoadFactor));
};
class OrderedHashSet : public OrderedHashTable<OrderedHashSet, 1> {
public:
DECLARE_CAST(OrderedHashSet)
static Handle<OrderedHashSet> Add(Handle<OrderedHashSet> table,
Handle<Object> value);
static Handle<FixedArray> ConvertToKeysArray(Handle<OrderedHashSet> table,
GetKeysConversion convert);
};
class OrderedHashMap : public OrderedHashTable<OrderedHashMap, 2> {
public:
DECLARE_CAST(OrderedHashMap)
inline Object* ValueAt(int entry);
static const int kValueOffset = 1;
};
template <int entrysize>
class WeakHashTableShape : public BaseShape<Handle<Object> > {
public:
static inline bool IsMatch(Handle<Object> key, Object* other);
static inline uint32_t Hash(Handle<Object> key);
static inline uint32_t HashForObject(Handle<Object> key, Object* object);
static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
static const int kPrefixSize = 0;
static const int kEntrySize = entrysize;
};
// WeakHashTable maps keys that are arbitrary heap objects to heap object
// values. The table wraps the keys in weak cells and store values directly.
// Thus it references keys weakly and values strongly.
class WeakHashTable: public HashTable<WeakHashTable,
WeakHashTableShape<2>,
Handle<Object> > {
typedef HashTable<
WeakHashTable, WeakHashTableShape<2>, Handle<Object> > DerivedHashTable;
public:
DECLARE_CAST(WeakHashTable)
// Looks up the value associated with the given key. The hole value is
// returned in case the key is not present.
Object* Lookup(Handle<HeapObject> key);
// Adds (or overwrites) the value associated with the given key. Mapping a
// key to the hole value causes removal of the whole entry.
MUST_USE_RESULT static Handle<WeakHashTable> Put(Handle<WeakHashTable> table,
Handle<HeapObject> key,
Handle<HeapObject> value);
static Handle<FixedArray> GetValues(Handle<WeakHashTable> table);
private:
friend class MarkCompactCollector;
void AddEntry(int entry, Handle<WeakCell> key, Handle<HeapObject> value);
// Returns the index to the value of an entry.
static inline int EntryToValueIndex(int entry) {
return EntryToIndex(entry) + 1;
}
};
// The cache for maps used by normalized (dictionary mode) objects.
// Such maps do not have property descriptors, so a typical program
// needs very limited number of distinct normalized maps.
......@@ -8144,137 +7482,6 @@ class JSRegExp: public JSObject {
DEFINE_OPERATORS_FOR_FLAGS(JSRegExp::Flags)
class CompilationCacheShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) {
return key->Hash();
}
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
static const int kEntrySize = 3;
};
class InfoVectorPair {
public:
InfoVectorPair() : shared_(nullptr), vector_cell_(nullptr) {}
InfoVectorPair(SharedFunctionInfo* shared, Cell* vector_cell)
: shared_(shared), vector_cell_(vector_cell) {}
SharedFunctionInfo* shared() const { return shared_; }
Cell* vector() const { return vector_cell_; }
bool has_shared() const { return shared_ != nullptr; }
bool has_vector() const { return vector_cell_ != nullptr; }
private:
SharedFunctionInfo* shared_;
Cell* vector_cell_;
};
// This cache is used in two different variants. For regexp caching, it simply
// maps identifying info of the regexp to the cached regexp object. Scripts and
// eval code only gets cached after a second probe for the code object. To do
// so, on first "put" only a hash identifying the source is entered into the
// cache, mapping it to a lifetime count of the hash. On each call to Age all
// such lifetimes get reduced, and removed once they reach zero. If a second put
// is called while such a hash is live in the cache, the hash gets replaced by
// an actual cache entry. Age also removes stale live entries from the cache.
// Such entries are identified by SharedFunctionInfos pointing to either the
// recompilation stub, or to "old" code. This avoids memory leaks due to
// premature caching of scripts and eval strings that are never needed later.
class CompilationCacheTable: public HashTable<CompilationCacheTable,
CompilationCacheShape,
HashTableKey*> {
public:
// Find cached value for a string key, otherwise return null.
Handle<Object> Lookup(
Handle<String> src, Handle<Context> context, LanguageMode language_mode);
InfoVectorPair LookupScript(Handle<String> src, Handle<Context> context,
LanguageMode language_mode);
InfoVectorPair LookupEval(Handle<String> src,
Handle<SharedFunctionInfo> shared,
Handle<Context> native_context,
LanguageMode language_mode, int position);
Handle<Object> LookupRegExp(Handle<String> source, JSRegExp::Flags flags);
static Handle<CompilationCacheTable> Put(
Handle<CompilationCacheTable> cache, Handle<String> src,
Handle<Context> context, LanguageMode language_mode,
Handle<Object> value);
static Handle<CompilationCacheTable> PutScript(
Handle<CompilationCacheTable> cache, Handle<String> src,
Handle<Context> context, LanguageMode language_mode,
Handle<SharedFunctionInfo> value, Handle<Cell> literals);
static Handle<CompilationCacheTable> PutEval(
Handle<CompilationCacheTable> cache, Handle<String> src,
Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
Handle<Context> native_context, Handle<Cell> literals, int position);
static Handle<CompilationCacheTable> PutRegExp(
Handle<CompilationCacheTable> cache, Handle<String> src,
JSRegExp::Flags flags, Handle<FixedArray> value);
void Remove(Object* value);
void Age();
static const int kHashGenerations = 10;
DECLARE_CAST(CompilationCacheTable)
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
};
class CodeCacheHashTableShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) {
return key->Hash();
}
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
// The both the key (name + flags) and value (code object) can be derived from
// the fixed array that stores both the name and code.
// TODO(verwaest): Don't allocate a fixed array but inline name and code.
// Rewrite IsMatch to get table + index as input rather than just the raw key.
static const int kEntrySize = 1;
};
class CodeCacheHashTable: public HashTable<CodeCacheHashTable,
CodeCacheHashTableShape,
HashTableKey*> {
public:
static Handle<CodeCacheHashTable> Put(
Handle<CodeCacheHashTable> table,
Handle<Name> name,
Handle<Code> code);
Code* Lookup(Name* name, Code::Flags flags);
DECLARE_CAST(CodeCacheHashTable)
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
};
class TypeFeedbackInfo: public Struct {
public:
inline int ic_total_count();
......@@ -9924,6 +9131,8 @@ class JSCollection : public JSObject {
// The JSSet describes EcmaScript Harmony sets
// TODO(marja): When moving JSSet out of objects.h, move JSSetIterator (from
// objects/hash-table.h) into the same file.
class JSSet : public JSCollection {
public:
DECLARE_CAST(JSSet)
......@@ -9941,6 +9150,8 @@ class JSSet : public JSCollection {
// The JSMap describes EcmaScript Harmony maps
// TODO(marja): When moving JSMap out of objects.h, move JSMapIterator (from
// objects/hash-table.h) into the same file.
class JSMap : public JSCollection {
public:
DECLARE_CAST(JSMap)
......@@ -10035,112 +9246,6 @@ class JSStringIterator : public JSObject {
DISALLOW_IMPLICIT_CONSTRUCTORS(JSStringIterator);
};
// OrderedHashTableIterator is an iterator that iterates over the keys and
// values of an OrderedHashTable.
//
// The iterator has a reference to the underlying OrderedHashTable data,
// [table], as well as the current [index] the iterator is at.
//
// When the OrderedHashTable is rehashed it adds a reference from the old table
// to the new table as well as storing enough data about the changes so that the
// iterator [index] can be adjusted accordingly.
//
// When the [Next] result from the iterator is requested, the iterator checks if
// there is a newer table that it needs to transition to.
template<class Derived, class TableType>
class OrderedHashTableIterator: public JSObject {
public:
// [table]: the backing hash table mapping keys to values.
DECL_ACCESSORS(table, Object)
// [index]: The index into the data table.
DECL_ACCESSORS(index, Object)
// [kind]: The kind of iteration this is. One of the [Kind] enum values.
DECL_ACCESSORS(kind, Object)
#ifdef OBJECT_PRINT
void OrderedHashTableIteratorPrint(std::ostream& os); // NOLINT
#endif
static const int kTableOffset = JSObject::kHeaderSize;
static const int kIndexOffset = kTableOffset + kPointerSize;
static const int kKindOffset = kIndexOffset + kPointerSize;
static const int kSize = kKindOffset + kPointerSize;
enum Kind {
kKindKeys = 1,
kKindValues = 2,
kKindEntries = 3
};
// Whether the iterator has more elements. This needs to be called before
// calling |CurrentKey| and/or |CurrentValue|.
bool HasMore();
// Move the index forward one.
void MoveNext() {
set_index(Smi::FromInt(Smi::cast(index())->value() + 1));
}
// Populates the array with the next key and value and then moves the iterator
// forward.
// This returns the |kind| or 0 if the iterator is already at the end.
Smi* Next(JSArray* value_array);
// Returns the current key of the iterator. This should only be called when
// |HasMore| returns true.
inline Object* CurrentKey();
private:
// Transitions the iterator to the non obsolete backing store. This is a NOP
// if the [table] is not obsolete.
void Transition();
DISALLOW_IMPLICIT_CONSTRUCTORS(OrderedHashTableIterator);
};
class JSSetIterator: public OrderedHashTableIterator<JSSetIterator,
OrderedHashSet> {
public:
// Dispatched behavior.
DECLARE_PRINTER(JSSetIterator)
DECLARE_VERIFIER(JSSetIterator)
DECLARE_CAST(JSSetIterator)
// Called by |Next| to populate the array. This allows the subclasses to
// populate the array differently.
inline void PopulateValueArray(FixedArray* array);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(JSSetIterator);
};
class JSMapIterator: public OrderedHashTableIterator<JSMapIterator,
OrderedHashMap> {
public:
// Dispatched behavior.
DECLARE_PRINTER(JSMapIterator)
DECLARE_VERIFIER(JSMapIterator)
DECLARE_CAST(JSMapIterator)
// Called by |Next| to populate the array. This allows the subclasses to
// populate the array differently.
inline void PopulateValueArray(FixedArray* array);
private:
// Returns the current value of the iterator. This should only be called when
// |HasMore| returns true.
inline Object* CurrentValue();
DISALLOW_IMPLICIT_CONSTRUCTORS(JSMapIterator);
};
// Base class for both JSWeakMap and JSWeakSet
class JSWeakCollection: public JSObject {
public:
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_CODE_CACHE_INL_H_
#define V8_OBJECTS_CODE_CACHE_INL_H_
#include "src/objects/code-cache.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
CAST_ACCESSOR(CodeCacheHashTable)
Handle<Object> CodeCacheHashTableShape::AsHandle(Isolate* isolate,
HashTableKey* key) {
return key->AsHandle(isolate);
}
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_CODE_CACHE_INL_H_
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_CODE_CACHE_H_
#define V8_OBJECTS_CODE_CACHE_H_
#include "src/objects/hash-table.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
class CodeCacheHashTableShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) { return key->Hash(); }
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
// The both the key (name + flags) and value (code object) can be derived from
// the fixed array that stores both the name and code.
// TODO(verwaest): Don't allocate a fixed array but inline name and code.
// Rewrite IsMatch to get table + index as input rather than just the raw key.
static const int kEntrySize = 1;
};
class CodeCacheHashTable
: public HashTable<CodeCacheHashTable, CodeCacheHashTableShape,
HashTableKey*> {
public:
static Handle<CodeCacheHashTable> Put(Handle<CodeCacheHashTable> table,
Handle<Name> name, Handle<Code> code);
Code* Lookup(Name* name, Code::Flags flags);
DECLARE_CAST(CodeCacheHashTable)
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
};
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_CODE_CACHE_H_
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_COMPILATION_CACHE_INL_H_
#define V8_OBJECTS_COMPILATION_CACHE_INL_H_
#include "src/objects/compilation-cache.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
CAST_ACCESSOR(CompilationCacheTable)
Handle<Object> CompilationCacheShape::AsHandle(Isolate* isolate,
HashTableKey* key) {
return key->AsHandle(isolate);
}
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_COMPILATION_CACHE_INL_H_
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_COMPILATION_CACHE_H_
#define V8_OBJECTS_COMPILATION_CACHE_H_
#include "src/objects/hash-table.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
class CompilationCacheShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) { return key->Hash(); }
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
static const int kEntrySize = 3;
};
class InfoVectorPair {
public:
InfoVectorPair() : shared_(nullptr), vector_cell_(nullptr) {}
InfoVectorPair(SharedFunctionInfo* shared, Cell* vector_cell)
: shared_(shared), vector_cell_(vector_cell) {}
SharedFunctionInfo* shared() const { return shared_; }
Cell* vector() const { return vector_cell_; }
bool has_shared() const { return shared_ != nullptr; }
bool has_vector() const { return vector_cell_ != nullptr; }
private:
SharedFunctionInfo* shared_;
Cell* vector_cell_;
};
// This cache is used in two different variants. For regexp caching, it simply
// maps identifying info of the regexp to the cached regexp object. Scripts and
// eval code only gets cached after a second probe for the code object. To do
// so, on first "put" only a hash identifying the source is entered into the
// cache, mapping it to a lifetime count of the hash. On each call to Age all
// such lifetimes get reduced, and removed once they reach zero. If a second put
// is called while such a hash is live in the cache, the hash gets replaced by
// an actual cache entry. Age also removes stale live entries from the cache.
// Such entries are identified by SharedFunctionInfos pointing to either the
// recompilation stub, or to "old" code. This avoids memory leaks due to
// premature caching of scripts and eval strings that are never needed later.
class CompilationCacheTable
: public HashTable<CompilationCacheTable, CompilationCacheShape,
HashTableKey*> {
public:
// Find cached value for a string key, otherwise return null.
Handle<Object> Lookup(Handle<String> src, Handle<Context> context,
LanguageMode language_mode);
InfoVectorPair LookupScript(Handle<String> src, Handle<Context> context,
LanguageMode language_mode);
InfoVectorPair LookupEval(Handle<String> src,
Handle<SharedFunctionInfo> shared,
Handle<Context> native_context,
LanguageMode language_mode, int position);
Handle<Object> LookupRegExp(Handle<String> source, JSRegExp::Flags flags);
static Handle<CompilationCacheTable> Put(Handle<CompilationCacheTable> cache,
Handle<String> src,
Handle<Context> context,
LanguageMode language_mode,
Handle<Object> value);
static Handle<CompilationCacheTable> PutScript(
Handle<CompilationCacheTable> cache, Handle<String> src,
Handle<Context> context, LanguageMode language_mode,
Handle<SharedFunctionInfo> value, Handle<Cell> literals);
static Handle<CompilationCacheTable> PutEval(
Handle<CompilationCacheTable> cache, Handle<String> src,
Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
Handle<Context> native_context, Handle<Cell> literals, int position);
static Handle<CompilationCacheTable> PutRegExp(
Handle<CompilationCacheTable> cache, Handle<String> src,
JSRegExp::Flags flags, Handle<FixedArray> value);
void Remove(Object* value);
void Age();
static const int kHashGenerations = 10;
DECLARE_CAST(CompilationCacheTable)
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
};
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_COMPILATION_CACHE_H_
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_HASH_TABLE_H_
#define V8_OBJECTS_HASH_TABLE_H_
#include "src/objects.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
// HashTable is a subclass of FixedArray that implements a hash table
// that uses open addressing and quadratic probing.
//
// In order for the quadratic probing to work, elements that have not
// yet been used and elements that have been deleted are
// distinguished. Probing continues when deleted elements are
// encountered and stops when unused elements are encountered.
//
// - Elements with key == undefined have not been used yet.
// - Elements with key == the_hole have been deleted.
//
// The hash table class is parameterized with a Shape and a Key.
// Shape must be a class with the following interface:
// class ExampleShape {
// public:
// // Tells whether key matches other.
// static bool IsMatch(Key key, Object* other);
// // Returns the hash value for key.
// static uint32_t Hash(Key key);
// // Returns the hash value for object.
// static uint32_t HashForObject(Key key, Object* object);
// // Convert key to an object.
// static inline Handle<Object> AsHandle(Isolate* isolate, Key key);
// // The prefix size indicates number of elements in the beginning
// // of the backing storage.
// static const int kPrefixSize = ..;
// // The Element size indicates number of elements per entry.
// static const int kEntrySize = ..;
// };
// The prefix size indicates an amount of memory in the
// beginning of the backing storage that can be used for non-element
// information by subclasses.
template <typename Key>
class BaseShape {
public:
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(Key key, Object* object) { return 0; }
static uint32_t SeededHashForObject(Key key, uint32_t seed, Object* object) {
DCHECK(UsesSeed);
return HashForObject(key, object);
}
static inline Map* GetMap(Isolate* isolate);
};
class HashTableBase : public FixedArray {
public:
// Returns the number of elements in the hash table.
inline int NumberOfElements();
// Returns the number of deleted elements in the hash table.
inline int NumberOfDeletedElements();
// Returns the capacity of the hash table.
inline int Capacity();
// ElementAdded should be called whenever an element is added to a
// hash table.
inline void ElementAdded();
// ElementRemoved should be called whenever an element is removed from
// a hash table.
inline void ElementRemoved();
inline void ElementsRemoved(int n);
// Computes the required capacity for a table holding the given
// number of elements. May be more than HashTable::kMaxCapacity.
static inline int ComputeCapacity(int at_least_space_for);
// 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);
// Compute the probe offset (quadratic probing).
INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
return (n + n * n) >> 1;
}
static const int kNumberOfElementsIndex = 0;
static const int kNumberOfDeletedElementsIndex = 1;
static const int kCapacityIndex = 2;
static const int kPrefixStartIndex = 3;
// Constant used for denoting a absent entry.
static const int kNotFound = -1;
// Minimum capacity for newly created hash tables.
static const int kMinCapacity = 4;
protected:
// Update the number of elements in the hash table.
inline void SetNumberOfElements(int nof);
// Update the number of deleted elements in the hash table.
inline void SetNumberOfDeletedElements(int nod);
// Returns probe entry.
static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
DCHECK(base::bits::IsPowerOfTwo32(size));
return (hash + GetProbeOffset(number)) & (size - 1);
}
inline static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
return hash & (size - 1);
}
inline static uint32_t NextProbe(uint32_t last, uint32_t number,
uint32_t size) {
return (last + number) & (size - 1);
}
};
template <typename Derived, typename Shape, typename Key>
class HashTable : public HashTableBase {
public:
typedef Shape ShapeT;
// Wrapper methods
inline uint32_t Hash(Key key) {
if (Shape::UsesSeed) {
return Shape::SeededHash(key, GetHeap()->HashSeed());
} else {
return Shape::Hash(key);
}
}
inline uint32_t HashForObject(Key key, Object* object) {
if (Shape::UsesSeed) {
return Shape::SeededHashForObject(key, GetHeap()->HashSeed(), object);
} else {
return Shape::HashForObject(key, object);
}
}
// Returns a new HashTable object.
MUST_USE_RESULT static Handle<Derived> New(
Isolate* isolate, int at_least_space_for,
MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY,
PretenureFlag pretenure = NOT_TENURED);
DECLARE_CAST(HashTable)
// Garbage collection support.
void IteratePrefix(ObjectVisitor* visitor);
void IterateElements(ObjectVisitor* visitor);
// Find entry for key otherwise return kNotFound.
inline int FindEntry(Key key);
inline int FindEntry(Isolate* isolate, Key key, int32_t hash);
int FindEntry(Isolate* isolate, Key key);
inline bool Has(Isolate* isolate, Key key);
inline bool Has(Key key);
// Rehashes the table in-place.
void Rehash(Key key);
// Returns the key at entry.
Object* KeyAt(int entry) { return get(EntryToIndex(entry) + kEntryKeyIndex); }
static const int kElementsStartIndex = kPrefixStartIndex + Shape::kPrefixSize;
static const int kEntrySize = Shape::kEntrySize;
STATIC_ASSERT(kEntrySize > 0);
static const int kEntryKeyIndex = 0;
static const int kElementsStartOffset =
kHeaderSize + kElementsStartIndex * kPointerSize;
// Maximal capacity of HashTable. Based on maximal length of underlying
// FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
// cannot overflow.
static const int kMaxCapacity =
(FixedArray::kMaxLength - kElementsStartIndex) / kEntrySize;
// Returns the index for an entry (of the key)
static inline int EntryToIndex(int entry) {
return (entry * kEntrySize) + kElementsStartIndex;
}
protected:
friend class ObjectHashTable;
MUST_USE_RESULT static Handle<Derived> New(Isolate* isolate, int capacity,
PretenureFlag pretenure);
// Find the entry at which to insert element with the given key that
// has the given hash value.
uint32_t FindInsertionEntry(uint32_t hash);
// Attempt to shrink hash table after removal of key.
MUST_USE_RESULT static Handle<Derived> Shrink(Handle<Derived> table, Key key);
// Ensure enough space for n additional elements.
MUST_USE_RESULT static Handle<Derived> EnsureCapacity(
Handle<Derived> table, int n, Key key,
PretenureFlag pretenure = NOT_TENURED);
// Returns true if this table has sufficient capacity for adding n elements.
bool HasSufficientCapacityToAdd(int number_of_additional_elements);
// Sets the capacity of the hash table.
void SetCapacity(int capacity) {
// To scale a computed hash code to fit within the hash table, we
// use bit-wise AND with a mask, so the capacity must be positive
// and non-zero.
DCHECK(capacity > 0);
DCHECK(capacity <= kMaxCapacity);
set(kCapacityIndex, Smi::FromInt(capacity));
}
private:
// Returns _expected_ if one of entries given by the first _probe_ probes is
// equal to _expected_. Otherwise, returns the entry given by the probe
// number _probe_.
uint32_t EntryForProbe(Key key, Object* k, int probe, uint32_t expected);
void Swap(uint32_t entry1, uint32_t entry2, WriteBarrierMode mode);
// Rehashes this hash-table into the new table.
void Rehash(Handle<Derived> new_table, Key key);
};
// HashTableKey is an abstract superclass for virtual key behavior.
class HashTableKey {
public:
// Returns whether the other object matches this key.
virtual bool IsMatch(Object* other) = 0;
// Returns the hash value for this key.
virtual uint32_t Hash() = 0;
// Returns the hash value for object.
virtual uint32_t HashForObject(Object* key) = 0;
// Returns the key object for storing into the hash table.
MUST_USE_RESULT virtual Handle<Object> AsHandle(Isolate* isolate) = 0;
// Required.
virtual ~HashTableKey() {}
};
class ObjectHashTableShape : public BaseShape<Handle<Object>> {
public:
static inline bool IsMatch(Handle<Object> key, Object* other);
static inline uint32_t Hash(Handle<Object> key);
static inline uint32_t HashForObject(Handle<Object> key, Object* object);
static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
static const int kPrefixSize = 0;
static const int kEntrySize = 2;
};
// ObjectHashTable maps keys that are arbitrary objects to object values by
// using the identity hash of the key for hashing purposes.
class ObjectHashTable
: public HashTable<ObjectHashTable, ObjectHashTableShape, Handle<Object>> {
typedef HashTable<ObjectHashTable, ObjectHashTableShape, Handle<Object>>
DerivedHashTable;
public:
DECLARE_CAST(ObjectHashTable)
// Attempt to shrink hash table after removal of key.
MUST_USE_RESULT static inline Handle<ObjectHashTable> Shrink(
Handle<ObjectHashTable> table, Handle<Object> key);
// Looks up the value associated with the given key. The hole value is
// returned in case the key is not present.
Object* Lookup(Handle<Object> key);
Object* Lookup(Handle<Object> key, int32_t hash);
Object* Lookup(Isolate* isolate, Handle<Object> key, int32_t hash);
// Returns the value at entry.
Object* ValueAt(int entry);
// Adds (or overwrites) the value associated with the given key.
static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
Handle<Object> key, Handle<Object> value);
static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
Handle<Object> key, Handle<Object> value,
int32_t hash);
// Returns an ObjectHashTable (possibly |table|) where |key| has been removed.
static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
Handle<Object> key, bool* was_present);
static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
Handle<Object> key, bool* was_present,
int32_t hash);
protected:
friend class MarkCompactCollector;
void AddEntry(int entry, Object* key, Object* value);
void RemoveEntry(int entry);
// Returns the index to the value of an entry.
static inline int EntryToValueIndex(int entry) {
return EntryToIndex(entry) + 1;
}
};
class ObjectHashSetShape : public ObjectHashTableShape {
public:
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class ObjectHashSet
: public HashTable<ObjectHashSet, ObjectHashSetShape, Handle<Object>> {
public:
static Handle<ObjectHashSet> Add(Handle<ObjectHashSet> set,
Handle<Object> key);
inline bool Has(Isolate* isolate, Handle<Object> key, int32_t hash);
inline bool Has(Isolate* isolate, Handle<Object> key);
DECLARE_CAST(ObjectHashSet)
};
// OrderedHashTable is a HashTable with Object keys that preserves
// insertion order. There are Map and Set interfaces (OrderedHashMap
// and OrderedHashTable, below). It is meant to be used by JSMap/JSSet.
//
// Only Object* keys are supported, with Object::SameValueZero() used as the
// equality operator and Object::GetHash() for the hash function.
//
// Based on the "Deterministic Hash Table" as described by Jason Orendorff at
// https://wiki.mozilla.org/User:Jorend/Deterministic_hash_tables
// Originally attributed to Tyler Close.
//
// Memory layout:
// [0]: bucket count
// [1]: element count
// [2]: deleted element count
// [3..(3 + NumberOfBuckets() - 1)]: "hash table", where each item is an
// offset into the data table (see below) where the
// first item in this bucket is stored.
// [3 + NumberOfBuckets()..length]: "data table", an array of length
// Capacity() * kEntrySize, where the first entrysize
// items are handled by the derived class and the
// item at kChainOffset is another entry into the
// data table indicating the next entry in this hash
// bucket.
//
// When we transition the table to a new version we obsolete it and reuse parts
// of the memory to store information how to transition an iterator to the new
// table:
//
// Memory layout for obsolete table:
// [0]: bucket count
// [1]: Next newer table
// [2]: Number of removed holes or -1 when the table was cleared.
// [3..(3 + NumberOfRemovedHoles() - 1)]: The indexes of the removed holes.
// [3 + NumberOfRemovedHoles()..length]: Not used
//
template <class Derived, int entrysize>
class OrderedHashTable : public FixedArray {
public:
// Returns an OrderedHashTable with a capacity of at least |capacity|.
static Handle<Derived> Allocate(Isolate* isolate, int capacity,
PretenureFlag pretenure = NOT_TENURED);
// Returns an OrderedHashTable (possibly |table|) with enough space
// to add at least one new element.
static Handle<Derived> EnsureGrowable(Handle<Derived> table);
// Returns an OrderedHashTable (possibly |table|) that's shrunken
// if possible.
static Handle<Derived> Shrink(Handle<Derived> table);
// Returns a new empty OrderedHashTable and records the clearing so that
// existing iterators can be updated.
static Handle<Derived> Clear(Handle<Derived> table);
// Returns a true if the OrderedHashTable contains the key
static bool HasKey(Handle<Derived> table, Handle<Object> key);
int NumberOfElements() {
return Smi::cast(get(kNumberOfElementsIndex))->value();
}
int NumberOfDeletedElements() {
return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
}
// Returns the number of contiguous entries in the data table, starting at 0,
// that either are real entries or have been deleted.
int UsedCapacity() { return NumberOfElements() + NumberOfDeletedElements(); }
int NumberOfBuckets() {
return Smi::cast(get(kNumberOfBucketsIndex))->value();
}
// Returns an index into |this| for the given entry.
int EntryToIndex(int entry) {
return kHashTableStartIndex + NumberOfBuckets() + (entry * kEntrySize);
}
int HashToBucket(int hash) { return hash & (NumberOfBuckets() - 1); }
int HashToEntry(int hash) {
int bucket = HashToBucket(hash);
Object* entry = this->get(kHashTableStartIndex + bucket);
return Smi::cast(entry)->value();
}
int KeyToFirstEntry(Isolate* isolate, Object* key) {
Object* hash = key->GetHash();
// If the object does not have an identity hash, it was never used as a key
if (hash->IsUndefined(isolate)) return kNotFound;
return HashToEntry(Smi::cast(hash)->value());
}
int NextChainEntry(int entry) {
Object* next_entry = get(EntryToIndex(entry) + kChainOffset);
return Smi::cast(next_entry)->value();
}
// use KeyAt(i)->IsTheHole(isolate) to determine if this is a deleted entry.
Object* KeyAt(int entry) {
DCHECK_LT(entry, this->UsedCapacity());
return get(EntryToIndex(entry));
}
bool IsObsolete() { return !get(kNextTableIndex)->IsSmi(); }
// The next newer table. This is only valid if the table is obsolete.
Derived* NextTable() { return Derived::cast(get(kNextTableIndex)); }
// When the table is obsolete we store the indexes of the removed holes.
int RemovedIndexAt(int index) {
return Smi::cast(get(kRemovedHolesIndex + index))->value();
}
static const int kNotFound = -1;
static const int kMinCapacity = 4;
static const int kNumberOfElementsIndex = 0;
// The next table is stored at the same index as the nof elements.
static const int kNextTableIndex = kNumberOfElementsIndex;
static const int kNumberOfDeletedElementsIndex = kNumberOfElementsIndex + 1;
static const int kNumberOfBucketsIndex = kNumberOfDeletedElementsIndex + 1;
static const int kHashTableStartIndex = kNumberOfBucketsIndex + 1;
static constexpr const int kNumberOfElementsOffset =
FixedArray::OffsetOfElementAt(kNumberOfElementsIndex);
static constexpr const int kNextTableOffset =
FixedArray::OffsetOfElementAt(kNextTableIndex);
static constexpr const int kNumberOfDeletedElementsOffset =
FixedArray::OffsetOfElementAt(kNumberOfDeletedElementsIndex);
static constexpr const int kNumberOfBucketsOffset =
FixedArray::OffsetOfElementAt(kNumberOfBucketsIndex);
static constexpr const int kHashTableStartOffset =
FixedArray::OffsetOfElementAt(kHashTableStartIndex);
static const int kEntrySize = entrysize + 1;
static const int kChainOffset = entrysize;
static const int kLoadFactor = 2;
// NumberOfDeletedElements is set to kClearedTableSentinel when
// the table is cleared, which allows iterator transitions to
// optimize that case.
static const int kClearedTableSentinel = -1;
protected:
static Handle<Derived> Rehash(Handle<Derived> table, int new_capacity);
void SetNumberOfBuckets(int num) {
set(kNumberOfBucketsIndex, Smi::FromInt(num));
}
void SetNumberOfElements(int num) {
set(kNumberOfElementsIndex, Smi::FromInt(num));
}
void SetNumberOfDeletedElements(int num) {
set(kNumberOfDeletedElementsIndex, Smi::FromInt(num));
}
// Returns the number elements that can fit into the allocated buffer.
int Capacity() { return NumberOfBuckets() * kLoadFactor; }
void SetNextTable(Derived* next_table) { set(kNextTableIndex, next_table); }
void SetRemovedIndexAt(int index, int removed_index) {
return set(kRemovedHolesIndex + index, Smi::FromInt(removed_index));
}
static const int kRemovedHolesIndex = kHashTableStartIndex;
static const int kMaxCapacity =
(FixedArray::kMaxLength - kHashTableStartIndex) /
(1 + (kEntrySize * kLoadFactor));
};
class OrderedHashSet : public OrderedHashTable<OrderedHashSet, 1> {
public:
DECLARE_CAST(OrderedHashSet)
static Handle<OrderedHashSet> Add(Handle<OrderedHashSet> table,
Handle<Object> value);
static Handle<FixedArray> ConvertToKeysArray(Handle<OrderedHashSet> table,
GetKeysConversion convert);
};
class OrderedHashMap : public OrderedHashTable<OrderedHashMap, 2> {
public:
DECLARE_CAST(OrderedHashMap)
inline Object* ValueAt(int entry);
static const int kValueOffset = 1;
};
template <int entrysize>
class WeakHashTableShape : public BaseShape<Handle<Object>> {
public:
static inline bool IsMatch(Handle<Object> key, Object* other);
static inline uint32_t Hash(Handle<Object> key);
static inline uint32_t HashForObject(Handle<Object> key, Object* object);
static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
static const int kPrefixSize = 0;
static const int kEntrySize = entrysize;
};
// WeakHashTable maps keys that are arbitrary heap objects to heap object
// values. The table wraps the keys in weak cells and store values directly.
// Thus it references keys weakly and values strongly.
class WeakHashTable
: public HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object>> {
typedef HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object>>
DerivedHashTable;
public:
DECLARE_CAST(WeakHashTable)
// Looks up the value associated with the given key. The hole value is
// returned in case the key is not present.
Object* Lookup(Handle<HeapObject> key);
// Adds (or overwrites) the value associated with the given key. Mapping a
// key to the hole value causes removal of the whole entry.
MUST_USE_RESULT static Handle<WeakHashTable> Put(Handle<WeakHashTable> table,
Handle<HeapObject> key,
Handle<HeapObject> value);
static Handle<FixedArray> GetValues(Handle<WeakHashTable> table);
private:
friend class MarkCompactCollector;
void AddEntry(int entry, Handle<WeakCell> key, Handle<HeapObject> value);
// Returns the index to the value of an entry.
static inline int EntryToValueIndex(int entry) {
return EntryToIndex(entry) + 1;
}
};
// OrderedHashTableIterator is an iterator that iterates over the keys and
// values of an OrderedHashTable.
//
// The iterator has a reference to the underlying OrderedHashTable data,
// [table], as well as the current [index] the iterator is at.
//
// When the OrderedHashTable is rehashed it adds a reference from the old table
// to the new table as well as storing enough data about the changes so that the
// iterator [index] can be adjusted accordingly.
//
// When the [Next] result from the iterator is requested, the iterator checks if
// there is a newer table that it needs to transition to.
template <class Derived, class TableType>
class OrderedHashTableIterator : public JSObject {
public:
// [table]: the backing hash table mapping keys to values.
DECL_ACCESSORS(table, Object)
// [index]: The index into the data table.
DECL_ACCESSORS(index, Object)
// [kind]: The kind of iteration this is. One of the [Kind] enum values.
DECL_ACCESSORS(kind, Object)
#ifdef OBJECT_PRINT
void OrderedHashTableIteratorPrint(std::ostream& os); // NOLINT
#endif
static const int kTableOffset = JSObject::kHeaderSize;
static const int kIndexOffset = kTableOffset + kPointerSize;
static const int kKindOffset = kIndexOffset + kPointerSize;
static const int kSize = kKindOffset + kPointerSize;
enum Kind { kKindKeys = 1, kKindValues = 2, kKindEntries = 3 };
// Whether the iterator has more elements. This needs to be called before
// calling |CurrentKey| and/or |CurrentValue|.
bool HasMore();
// Move the index forward one.
void MoveNext() { set_index(Smi::FromInt(Smi::cast(index())->value() + 1)); }
// Populates the array with the next key and value and then moves the iterator
// forward.
// This returns the |kind| or 0 if the iterator is already at the end.
Smi* Next(JSArray* value_array);
// Returns the current key of the iterator. This should only be called when
// |HasMore| returns true.
inline Object* CurrentKey();
private:
// Transitions the iterator to the non obsolete backing store. This is a NOP
// if the [table] is not obsolete.
void Transition();
DISALLOW_IMPLICIT_CONSTRUCTORS(OrderedHashTableIterator);
};
class JSSetIterator
: public OrderedHashTableIterator<JSSetIterator, OrderedHashSet> {
public:
// Dispatched behavior.
DECLARE_PRINTER(JSSetIterator)
DECLARE_VERIFIER(JSSetIterator)
DECLARE_CAST(JSSetIterator)
// Called by |Next| to populate the array. This allows the subclasses to
// populate the array differently.
inline void PopulateValueArray(FixedArray* array);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(JSSetIterator);
};
class JSMapIterator
: public OrderedHashTableIterator<JSMapIterator, OrderedHashMap> {
public:
// Dispatched behavior.
DECLARE_PRINTER(JSMapIterator)
DECLARE_VERIFIER(JSMapIterator)
DECLARE_CAST(JSMapIterator)
// Called by |Next| to populate the array. This allows the subclasses to
// populate the array differently.
inline void PopulateValueArray(FixedArray* array);
private:
// Returns the current value of the iterator. This should only be called when
// |HasMore| returns true.
inline Object* CurrentValue();
DISALLOW_IMPLICIT_CONSTRUCTORS(JSMapIterator);
};
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_HASH_TABLE_H_
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_STRING_TABLE_H_
#define V8_OBJECTS_STRING_TABLE_H_
#include "src/objects/hash-table.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
class StringTableShape : public BaseShape<HashTableKey*> {
public:
static inline bool IsMatch(HashTableKey* key, Object* value) {
return key->IsMatch(value);
}
static inline uint32_t Hash(HashTableKey* key) { return key->Hash(); }
static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
return key->HashForObject(object);
}
static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class SeqOneByteString;
// StringTable.
//
// No special elements in the prefix and the element size is 1
// because only the string itself (the key) needs to be stored.
class StringTable
: public HashTable<StringTable, StringTableShape, HashTableKey*> {
public:
// Find string in the string table. If it is not there yet, it is
// added. The return value is the string found.
V8_EXPORT_PRIVATE static Handle<String> LookupString(Isolate* isolate,
Handle<String> key);
static Handle<String> LookupKey(Isolate* isolate, HashTableKey* key);
static String* LookupKeyIfExists(Isolate* isolate, HashTableKey* key);
// Tries to internalize given string and returns string handle on success
// or an empty handle otherwise.
MUST_USE_RESULT static MaybeHandle<String> InternalizeStringIfExists(
Isolate* isolate, Handle<String> string);
// Looks up a string that is equal to the given string and returns
// string handle if it is found, or an empty handle otherwise.
MUST_USE_RESULT static MaybeHandle<String> LookupStringIfExists(
Isolate* isolate, Handle<String> str);
MUST_USE_RESULT static MaybeHandle<String> LookupTwoCharsStringIfExists(
Isolate* isolate, uint16_t c1, uint16_t c2);
static void EnsureCapacityForDeserialization(Isolate* isolate, int expected);
DECLARE_CAST(StringTable)
private:
template <bool seq_one_byte>
friend class JsonParser;
DISALLOW_IMPLICIT_CONSTRUCTORS(StringTable);
};
class StringSetShape : public BaseShape<String*> {
public:
static inline bool IsMatch(String* key, Object* value);
static inline uint32_t Hash(String* key);
static inline uint32_t HashForObject(String* key, Object* object);
static const int kPrefixSize = 0;
static const int kEntrySize = 1;
};
class StringSet : public HashTable<StringSet, StringSetShape, String*> {
public:
static Handle<StringSet> New(Isolate* isolate);
static Handle<StringSet> Add(Handle<StringSet> blacklist,
Handle<String> name);
bool Has(Handle<String> name);
DECLARE_CAST(StringSet)
};
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_STRING_TABLE_H_
......@@ -1118,10 +1118,15 @@
'objects-printer.cc',
'objects.cc',
'objects.h',
'objects/code-cache.h',
'objects/code-cache-inl.h',
'objects/compilation-cache.h',
'objects/compilation-cache-inl.h',
'objects/descriptor-array.h',
'objects/dictionary.h',
'objects/frame-array.h',
'objects/frame-array-inl.h',
'objects/hash-table.h',
'objects/literal-objects.cc',
'objects/literal-objects.h',
'objects/module-info.h',
......@@ -1130,6 +1135,7 @@
'objects/regexp-match-info.h',
'objects/scope-info.cc',
'objects/scope-info.h',
'objects/string-table.h',
'ostreams.cc',
'ostreams.h',
'parsing/duplicate-finder.h',
......
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