Commit e2de1b86 authored by Ross McIlroy's avatar Ross McIlroy Committed by Commit Bot

Add support to IdentityMap for deletion, iteration and AllocationPolicy.

In order to use the IdentityMap in the CompilerDispatcher the following
support is added:
 - Support for deleting entries
 - Support for iterating through the entries.
 - Support for AllocationPolicy to enable non-zone allocation of backing
   stores.
 - Also refactors the code a bit.

BUG=v8:5203

Change-Id: I8b616cba8ae9dc22a7f4d76070fbb318c4edc80d
Reviewed-on: https://chromium-review.googlesource.com/444409Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Reviewed-by: 's avatarJochen Eisinger <jochen@chromium.org>
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#43362}
parent 96eda1f7
...@@ -124,7 +124,8 @@ CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate) ...@@ -124,7 +124,8 @@ CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate)
prev_canonical_scope_ = handle_scope_data->canonical_scope; prev_canonical_scope_ = handle_scope_data->canonical_scope;
handle_scope_data->canonical_scope = this; handle_scope_data->canonical_scope = this;
root_index_map_ = new RootIndexMap(isolate); root_index_map_ = new RootIndexMap(isolate);
identity_map_ = new IdentityMap<Object**>(isolate->heap(), &zone_); identity_map_ = new IdentityMap<Object**, ZoneAllocationPolicy>(
isolate->heap(), ZoneAllocationPolicy(&zone_));
canonical_level_ = handle_scope_data->level; canonical_level_ = handle_scope_data->level;
} }
......
...@@ -331,7 +331,7 @@ class HandleScope { ...@@ -331,7 +331,7 @@ class HandleScope {
// Forward declarations for CanonicalHandleScope. // Forward declarations for CanonicalHandleScope.
template <typename V> template <typename V, class AllocationPolicy>
class IdentityMap; class IdentityMap;
class RootIndexMap; class RootIndexMap;
...@@ -352,7 +352,7 @@ class V8_EXPORT_PRIVATE CanonicalHandleScope final { ...@@ -352,7 +352,7 @@ class V8_EXPORT_PRIVATE CanonicalHandleScope final {
Isolate* isolate_; Isolate* isolate_;
Zone zone_; Zone zone_;
RootIndexMap* root_index_map_; RootIndexMap* root_index_map_;
IdentityMap<Object**>* identity_map_; IdentityMap<Object**, ZoneAllocationPolicy>* identity_map_;
// Ordinary nested handle scopes within the current one are not canonical. // Ordinary nested handle scopes within the current one are not canonical.
int canonical_level_; int canonical_level_;
// We may have nested canonical scopes. Handles are canonical within each one. // We may have nested canonical scopes. Handles are canonical within each one.
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "src/base/functional.h" #include "src/base/functional.h"
#include "src/heap/heap-inl.h" #include "src/heap/heap-inl.h"
#include "src/zone/zone-containers.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -14,42 +13,45 @@ namespace internal { ...@@ -14,42 +13,45 @@ namespace internal {
static const int kInitialIdentityMapSize = 4; static const int kInitialIdentityMapSize = 4;
static const int kResizeFactor = 4; static const int kResizeFactor = 4;
IdentityMapBase::~IdentityMapBase() { Clear(); } IdentityMapBase::~IdentityMapBase() {
// Clear must be called by the subclass to avoid calling the virtual
// DeleteArray function from the destructor.
DCHECK_NULL(keys_);
}
void IdentityMapBase::Clear() { void IdentityMapBase::Clear() {
if (keys_) { if (keys_) {
DCHECK(!is_iterable());
heap_->UnregisterStrongRoots(keys_); heap_->UnregisterStrongRoots(keys_);
DeleteArray(keys_);
DeleteArray(values_);
keys_ = nullptr; keys_ = nullptr;
values_ = nullptr; values_ = nullptr;
size_ = 0; size_ = 0;
capacity_ = 0;
mask_ = 0; mask_ = 0;
} }
} }
IdentityMapBase::RawEntry IdentityMapBase::Lookup(Object* key) { void IdentityMapBase::EnableIteration() {
int index = LookupIndex(key); CHECK(!is_iterable());
return index >= 0 ? &values_[index] : nullptr; is_iterable_ = true;
}
IdentityMapBase::RawEntry IdentityMapBase::Insert(Object* key) {
int index = InsertIndex(key);
DCHECK_GE(index, 0);
return &values_[index];
} }
void IdentityMapBase::DisableIteration() {
CHECK(is_iterable());
is_iterable_ = false;
int IdentityMapBase::Hash(Object* address) { // We might need to resize due to iterator deletion - do this now.
CHECK_NE(address, heap_->not_mapped_symbol()); if (size_ * kResizeFactor < capacity_ / kResizeFactor) {
uintptr_t raw_address = reinterpret_cast<uintptr_t>(address); Resize(capacity_ / kResizeFactor);
return static_cast<int>(hasher_(raw_address)); }
} }
int IdentityMapBase::ScanKeysFor(Object* address) const {
int IdentityMapBase::LookupIndex(Object* address) {
int start = Hash(address) & mask_; int start = Hash(address) & mask_;
Object* not_mapped = heap_->not_mapped_symbol(); Object* not_mapped = heap_->not_mapped_symbol();
for (int index = start; index < size_; index++) { for (int index = start; index < capacity_; index++) {
if (keys_[index] == address) return index; // Found. if (keys_[index] == address) return index; // Found.
if (keys_[index] == not_mapped) return -1; // Not found. if (keys_[index] == not_mapped) return -1; // Not found.
} }
...@@ -60,12 +62,11 @@ int IdentityMapBase::LookupIndex(Object* address) { ...@@ -60,12 +62,11 @@ int IdentityMapBase::LookupIndex(Object* address) {
return -1; return -1;
} }
int IdentityMapBase::InsertKey(Object* address) {
int IdentityMapBase::InsertIndex(Object* address) {
Object* not_mapped = heap_->not_mapped_symbol(); Object* not_mapped = heap_->not_mapped_symbol();
while (true) { while (true) {
int start = Hash(address) & mask_; int start = Hash(address) & mask_;
int limit = size_ / 2; int limit = capacity_ / 2;
// Search up to {limit} entries. // Search up to {limit} entries.
for (int index = start; --limit > 0; index = (index + 1) & mask_) { for (int index = start; --limit > 0; index = (index + 1) & mask_) {
if (keys_[index] == address) return index; // Found. if (keys_[index] == address) return index; // Found.
...@@ -74,72 +75,162 @@ int IdentityMapBase::InsertIndex(Object* address) { ...@@ -74,72 +75,162 @@ int IdentityMapBase::InsertIndex(Object* address) {
return index; return index;
} }
} }
Resize(); // Should only have to resize once, since we grow 4x. // Should only have to resize once, since we grow 4x.
Resize(capacity_ * kResizeFactor);
} }
UNREACHABLE(); UNREACHABLE();
return -1; return -1;
} }
void* IdentityMapBase::DeleteIndex(int index) {
void* ret_value = values_[index];
Object* not_mapped = heap_->not_mapped_symbol();
DCHECK_NE(keys_[index], not_mapped);
keys_[index] = not_mapped;
values_[index] = nullptr;
size_--;
DCHECK_GE(size_, 0);
if (!is_iterable() && (size_ * kResizeFactor < capacity_ / kResizeFactor)) {
Resize(capacity_ / kResizeFactor);
return ret_value; // No need to fix collisions as resize reinserts keys.
}
// Move any collisions to their new correct location.
int next_index = index;
for (;;) {
next_index = (next_index + 1) & mask_;
Object* key = keys_[next_index];
if (key == not_mapped) break;
int expected_index = Hash(key) & mask_;
if (index < next_index) {
if (index < expected_index && expected_index <= next_index) continue;
} else {
DCHECK_GT(index, next_index);
if (index < expected_index || expected_index <= next_index) continue;
}
DCHECK_EQ(not_mapped, keys_[index]);
DCHECK_NULL(values_[index]);
std::swap(keys_[index], keys_[next_index]);
std::swap(values_[index], values_[next_index]);
index = next_index;
}
return ret_value;
}
int IdentityMapBase::Lookup(Object* key) const {
int index = ScanKeysFor(key);
if (index < 0 && gc_counter_ != heap_->gc_count()) {
// Miss; rehash if there was a GC, then lookup again.
const_cast<IdentityMapBase*>(this)->Rehash();
index = ScanKeysFor(key);
}
return index;
}
int IdentityMapBase::LookupOrInsert(Object* key) {
// Perform an optimistic lookup.
int index = ScanKeysFor(key);
if (index < 0) {
// Miss; rehash if there was a GC, then insert.
if (gc_counter_ != heap_->gc_count()) Rehash();
index = InsertKey(key);
size_++;
DCHECK_LE(size_, capacity_);
}
DCHECK_GE(index, 0);
return index;
}
int IdentityMapBase::Hash(Object* address) const {
CHECK_NE(address, heap_->not_mapped_symbol());
uintptr_t raw_address = reinterpret_cast<uintptr_t>(address);
return static_cast<int>(hasher_(raw_address));
}
// Searches this map for the given key using the object's address // Searches this map for the given key using the object's address
// as the identity, returning: // as the identity, returning:
// found => a pointer to the storage location for the value // found => a pointer to the storage location for the value
// not found => a pointer to a new storage location for the value // not found => a pointer to a new storage location for the value
IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Object* key) { IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Object* key) {
RawEntry result; CHECK(!is_iterable()); // Don't allow insertion while iterable.
if (size_ == 0) { if (capacity_ == 0) {
// Allocate the initial storage for keys and values. // Allocate the initial storage for keys and values.
size_ = kInitialIdentityMapSize; capacity_ = kInitialIdentityMapSize;
mask_ = kInitialIdentityMapSize - 1; mask_ = kInitialIdentityMapSize - 1;
gc_counter_ = heap_->gc_count(); gc_counter_ = heap_->gc_count();
keys_ = zone_->NewArray<Object*>(size_); keys_ = reinterpret_cast<Object**>(NewPointerArray(capacity_));
Object* not_mapped = heap_->not_mapped_symbol(); Object* not_mapped = heap_->not_mapped_symbol();
for (int i = 0; i < size_; i++) keys_[i] = not_mapped; for (int i = 0; i < capacity_; i++) keys_[i] = not_mapped;
values_ = zone_->NewArray<void*>(size_); values_ = NewPointerArray(capacity_);
memset(values_, 0, sizeof(void*) * size_); memset(values_, 0, sizeof(void*) * capacity_);
heap_->RegisterStrongRoots(keys_, keys_ + size_); heap_->RegisterStrongRoots(keys_, keys_ + capacity_);
result = Insert(key);
} else {
// Perform an optimistic lookup.
result = Lookup(key);
if (result == nullptr) {
// Miss; rehash if there was a GC, then insert.
if (gc_counter_ != heap_->gc_count()) Rehash();
result = Insert(key);
}
} }
return result; int index = LookupOrInsert(key);
return &values_[index];
} }
// Searches this map for the given key using the object's address // Searches this map for the given key using the object's address
// as the identity, returning: // as the identity, returning:
// found => a pointer to the storage location for the value // found => a pointer to the storage location for the value
// not found => {nullptr} // not found => {nullptr}
IdentityMapBase::RawEntry IdentityMapBase::FindEntry(Object* key) { IdentityMapBase::RawEntry IdentityMapBase::FindEntry(Object* key) const {
// Don't allow find by key while iterable (might rehash).
CHECK(!is_iterable());
if (size_ == 0) return nullptr; if (size_ == 0) return nullptr;
// Remove constness since lookup might have to rehash.
int index = Lookup(key);
return index >= 0 ? &values_[index] : nullptr;
}
RawEntry result = Lookup(key); // Deletes the given key from the map using the object's address as the
if (result == nullptr && gc_counter_ != heap_->gc_count()) { // identity, returning:
Rehash(); // Rehash is expensive, so only do it in case of a miss. // found => the value
result = Lookup(key); // not found => {nullptr}
} void* IdentityMapBase::DeleteEntry(Object* key) {
return result; CHECK(!is_iterable()); // Don't allow deletion by key while iterable.
if (size_ == 0) return nullptr;
int index = Lookup(key);
if (index < 0) return nullptr; // No entry found.
return DeleteIndex(index);
} }
IdentityMapBase::RawEntry IdentityMapBase::EntryAtIndex(int index) const {
DCHECK_LE(0, index);
DCHECK_LT(index, capacity_);
DCHECK_NE(keys_[index], heap_->not_mapped_symbol());
CHECK(is_iterable()); // Must be iterable to access by index;
return &values_[index];
}
int IdentityMapBase::NextIndex(int index) const {
DCHECK_LE(-1, index);
DCHECK_LE(index, capacity_);
CHECK(is_iterable()); // Must be iterable to access by index;
Object* not_mapped = heap_->not_mapped_symbol();
for (index++; index < capacity_; index++) {
if (keys_[index] != not_mapped) {
return index;
}
}
return capacity_;
}
void IdentityMapBase::Rehash() { void IdentityMapBase::Rehash() {
CHECK(!is_iterable()); // Can't rehash while iterating.
// Record the current GC counter. // Record the current GC counter.
gc_counter_ = heap_->gc_count(); gc_counter_ = heap_->gc_count();
// Assume that most objects won't be moved. // Assume that most objects won't be moved.
ZoneVector<std::pair<Object*, void*>> reinsert(zone_); std::vector<std::pair<Object*, void*>> reinsert;
// Search the table looking for keys that wouldn't be found with their // Search the table looking for keys that wouldn't be found with their
// current hashcode and evacuate them. // current hashcode and evacuate them.
int last_empty = -1; int last_empty = -1;
Object* not_mapped = heap_->not_mapped_symbol(); Object* not_mapped = heap_->not_mapped_symbol();
for (int i = 0; i < size_; i++) { for (int i = 0; i < capacity_; i++) {
if (keys_[i] == not_mapped) { if (keys_[i] == not_mapped) {
last_empty = i; last_empty = i;
} else { } else {
...@@ -155,42 +246,46 @@ void IdentityMapBase::Rehash() { ...@@ -155,42 +246,46 @@ void IdentityMapBase::Rehash() {
} }
// Reinsert all the key/value pairs that were in the wrong place. // Reinsert all the key/value pairs that were in the wrong place.
for (auto pair : reinsert) { for (auto pair : reinsert) {
int index = InsertIndex(pair.first); int index = InsertKey(pair.first);
DCHECK_GE(index, 0); DCHECK_GE(index, 0);
DCHECK_NE(heap_->not_mapped_symbol(), values_[index]); DCHECK_NULL(values_[index]);
values_[index] = pair.second; values_[index] = pair.second;
} }
} }
void IdentityMapBase::Resize(int new_capacity) {
void IdentityMapBase::Resize() { CHECK(!is_iterable()); // Can't resize while iterating.
// Grow the internal storage and reinsert all the key/value pairs. // Resize the internal storage and reinsert all the key/value pairs.
int old_size = size_; DCHECK_GT(new_capacity, size_);
int old_capacity = capacity_;
Object** old_keys = keys_; Object** old_keys = keys_;
void** old_values = values_; void** old_values = values_;
size_ = size_ * kResizeFactor; capacity_ = new_capacity;
mask_ = size_ - 1; mask_ = capacity_ - 1;
gc_counter_ = heap_->gc_count(); gc_counter_ = heap_->gc_count();
CHECK_LE(size_, (1024 * 1024 * 16)); // that would be extreme... keys_ = reinterpret_cast<Object**>(NewPointerArray(capacity_));
keys_ = zone_->NewArray<Object*>(size_);
Object* not_mapped = heap_->not_mapped_symbol(); Object* not_mapped = heap_->not_mapped_symbol();
for (int i = 0; i < size_; i++) keys_[i] = not_mapped; for (int i = 0; i < capacity_; i++) keys_[i] = not_mapped;
values_ = zone_->NewArray<void*>(size_); values_ = NewPointerArray(capacity_);
memset(values_, 0, sizeof(void*) * size_); memset(values_, 0, sizeof(void*) * capacity_);
for (int i = 0; i < old_size; i++) { for (int i = 0; i < old_capacity; i++) {
if (old_keys[i] == not_mapped) continue; if (old_keys[i] == not_mapped) continue;
int index = InsertIndex(old_keys[i]); int index = InsertKey(old_keys[i]);
DCHECK_GE(index, 0); DCHECK_GE(index, 0);
values_[index] = old_values[i]; values_[index] = old_values[i];
} }
// Unregister old keys and register new keys. // Unregister old keys and register new keys.
heap_->UnregisterStrongRoots(old_keys); heap_->UnregisterStrongRoots(old_keys);
heap_->RegisterStrongRoots(keys_, keys_ + size_); heap_->RegisterStrongRoots(keys_, keys_ + capacity_);
// Delete old storage;
DeleteArray(old_keys);
DeleteArray(old_values);
} }
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -13,11 +13,16 @@ namespace internal { ...@@ -13,11 +13,16 @@ namespace internal {
// Forward declarations. // Forward declarations.
class Heap; class Heap;
class Zone;
// Base class of identity maps contains shared code for all template // Base class of identity maps contains shared code for all template
// instantions. // instantions.
class IdentityMapBase { class IdentityMapBase {
public:
bool empty() const { return size_ == 0; }
int size() const { return size_; }
int capacity() const { return capacity_; }
bool is_iterable() const { return is_iterable_; }
protected: protected:
// Allow Tester to access internals, including changing the address of objects // Allow Tester to access internals, including changing the address of objects
// within the {keys_} array in order to simulate a moving GC. // within the {keys_} array in order to simulate a moving GC.
...@@ -25,51 +30,68 @@ class IdentityMapBase { ...@@ -25,51 +30,68 @@ class IdentityMapBase {
typedef void** RawEntry; typedef void** RawEntry;
IdentityMapBase(Heap* heap, Zone* zone) explicit IdentityMapBase(Heap* heap)
: heap_(heap), : heap_(heap),
zone_(zone),
gc_counter_(-1), gc_counter_(-1),
size_(0), size_(0),
capacity_(0),
mask_(0), mask_(0),
keys_(nullptr), keys_(nullptr),
values_(nullptr) {} values_(nullptr),
~IdentityMapBase(); is_iterable_(false) {}
virtual ~IdentityMapBase();
RawEntry GetEntry(Object* key); RawEntry GetEntry(Object* key);
RawEntry FindEntry(Object* key); RawEntry FindEntry(Object* key) const;
void* DeleteEntry(Object* key);
void* DeleteIndex(int index);
void Clear(); void Clear();
V8_EXPORT_PRIVATE RawEntry EntryAtIndex(int index) const;
V8_EXPORT_PRIVATE int NextIndex(int index) const;
void EnableIteration();
void DisableIteration();
virtual void** NewPointerArray(size_t length) = 0;
virtual void DeleteArray(void* array) = 0;
private: private:
// Internal implementation should not be called directly by subclasses. // Internal implementation should not be called directly by subclasses.
int LookupIndex(Object* address); int ScanKeysFor(Object* address) const;
int InsertIndex(Object* address); int InsertKey(Object* address);
int Lookup(Object* key) const;
int LookupOrInsert(Object* key);
void Rehash(); void Rehash();
void Resize(); void Resize(int new_capacity);
RawEntry Lookup(Object* key); int Hash(Object* address) const;
RawEntry Insert(Object* key);
int Hash(Object* address);
base::hash<uintptr_t> hasher_; base::hash<uintptr_t> hasher_;
Heap* heap_; Heap* heap_;
Zone* zone_;
int gc_counter_; int gc_counter_;
int size_; int size_;
int capacity_;
int mask_; int mask_;
Object** keys_; Object** keys_;
void** values_; void** values_;
bool is_iterable_;
DISALLOW_COPY_AND_ASSIGN(IdentityMapBase);
}; };
// Implements an identity map from object addresses to a given value type {V}. // Implements an identity map from object addresses to a given value type {V}.
// The map is robust w.r.t. garbage collection by synchronization with the // The map is robust w.r.t. garbage collection by synchronization with the
// supplied {heap}. // supplied {heap}.
// * Keys are treated as strong roots. // * Keys are treated as strong roots.
// * SMIs are valid keys, except SMI #0.
// * The value type {V} must be reinterpret_cast'able to {void*} // * The value type {V} must be reinterpret_cast'able to {void*}
// * The value type {V} must not be a heap type. // * The value type {V} must not be a heap type.
template <typename V> template <typename V, class AllocationPolicy>
class IdentityMap : public IdentityMapBase { class IdentityMap : public IdentityMapBase {
public: public:
IdentityMap(Heap* heap, Zone* zone) : IdentityMapBase(heap, zone) {} explicit IdentityMap(Heap* heap,
AllocationPolicy allocator = AllocationPolicy())
: IdentityMapBase(heap), allocator_(allocator) {}
~IdentityMap() override { Clear(); };
// Searches this map for the given key using the object's address // Searches this map for the given key using the object's address
// as the identity, returning: // as the identity, returning:
...@@ -82,16 +104,77 @@ class IdentityMap : public IdentityMapBase { ...@@ -82,16 +104,77 @@ class IdentityMap : public IdentityMapBase {
// as the identity, returning: // as the identity, returning:
// found => a pointer to the storage location for the value // found => a pointer to the storage location for the value
// not found => {nullptr} // not found => {nullptr}
V* Find(Handle<Object> key) { return Find(*key); } V* Find(Handle<Object> key) const { return Find(*key); }
V* Find(Object* key) { return reinterpret_cast<V*>(FindEntry(key)); } V* Find(Object* key) const { return reinterpret_cast<V*>(FindEntry(key)); }
// Set the value for the given key. // Set the value for the given key.
void Set(Handle<Object> key, V v) { Set(*key, v); } void Set(Handle<Object> key, V v) { Set(*key, v); }
void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; } void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; }
V Delete(Handle<Object> key) { return Delete(*key); }
V Delete(Object* key) { return reinterpret_cast<V>(DeleteEntry(key)); }
// Removes all elements from the map. // Removes all elements from the map.
void Clear() { IdentityMapBase::Clear(); } void Clear() { IdentityMapBase::Clear(); }
// Iterator over IdentityMap. The IteratableScope used to create this Iterator
// must be live for the duration of the iteration.
class Iterator {
public:
Iterator& operator++() {
index_ = map_->NextIndex(index_);
return *this;
}
Iterator& DeleteAndIncrement() {
map_->DeleteIndex(index_);
index_ = map_->NextIndex(index_);
return *this;
}
V* operator*() { return reinterpret_cast<V*>(map_->EntryAtIndex(index_)); }
V* operator->() { return reinterpret_cast<V*>(map_->EntryAtIndex(index_)); }
bool operator!=(const Iterator& other) { return index_ != other.index_; }
private:
Iterator(IdentityMap* map, int index) : map_(map), index_(index) {}
IdentityMap* map_;
int index_;
friend class IdentityMap;
};
class IteratableScope {
public:
explicit IteratableScope(IdentityMap* map) : map_(map) {
CHECK(!map_->is_iterable());
map_->EnableIteration();
}
~IteratableScope() {
CHECK(map_->is_iterable());
map_->DisableIteration();
}
Iterator begin() { return Iterator(map_, map_->NextIndex(-1)); }
Iterator end() { return Iterator(map_, map_->capacity()); }
private:
IdentityMap* map_;
DISALLOW_COPY_AND_ASSIGN(IteratableScope);
};
protected:
void** NewPointerArray(size_t length) override {
return static_cast<void**>(allocator_.New(sizeof(void*) * length));
}
void DeleteArray(void* array) override { allocator_.Delete(array); }
private:
AllocationPolicy allocator_;
DISALLOW_COPY_AND_ASSIGN(IdentityMap);
}; };
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -802,7 +802,8 @@ Maybe<bool> KeyAccumulator::CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, ...@@ -802,7 +802,8 @@ Maybe<bool> KeyAccumulator::CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
Zone set_zone(isolate_->allocator(), ZONE_NAME); Zone set_zone(isolate_->allocator(), ZONE_NAME);
const int kPresent = 1; const int kPresent = 1;
const int kGone = 0; const int kGone = 0;
IdentityMap<int> unchecked_result_keys(isolate_->heap(), &set_zone); IdentityMap<int, ZoneAllocationPolicy> unchecked_result_keys(
isolate_->heap(), ZoneAllocationPolicy(&set_zone));
int unchecked_result_keys_size = 0; int unchecked_result_keys_size = 0;
for (int i = 0; i < trap_result->length(); ++i) { for (int i = 0; i < trap_result->length(); ++i) {
DCHECK(trap_result->get(i)->IsUniqueName()); DCHECK(trap_result->get(i)->IsUniqueName());
......
...@@ -152,8 +152,9 @@ ValueSerializer::ValueSerializer(Isolate* isolate, ...@@ -152,8 +152,9 @@ ValueSerializer::ValueSerializer(Isolate* isolate,
: isolate_(isolate), : isolate_(isolate),
delegate_(delegate), delegate_(delegate),
zone_(isolate->allocator(), ZONE_NAME), zone_(isolate->allocator(), ZONE_NAME),
id_map_(isolate->heap(), &zone_), id_map_(isolate->heap(), ZoneAllocationPolicy(&zone_)),
array_buffer_transfer_map_(isolate->heap(), &zone_) {} array_buffer_transfer_map_(isolate->heap(),
ZoneAllocationPolicy(&zone_)) {}
ValueSerializer::~ValueSerializer() { ValueSerializer::~ValueSerializer() {
if (buffer_) { if (buffer_) {
......
...@@ -157,11 +157,11 @@ class ValueSerializer { ...@@ -157,11 +157,11 @@ class ValueSerializer {
// To avoid extra lookups in the identity map, ID+1 is actually stored in the // To avoid extra lookups in the identity map, ID+1 is actually stored in the
// map (checking if the used identity is zero is the fast way of checking if // map (checking if the used identity is zero is the fast way of checking if
// the entry is new). // the entry is new).
IdentityMap<uint32_t> id_map_; IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_;
uint32_t next_id_ = 0; uint32_t next_id_ = 0;
// A similar map, for transferred array buffers. // A similar map, for transferred array buffers.
IdentityMap<uint32_t> array_buffer_transfer_map_; IdentityMap<uint32_t, ZoneAllocationPolicy> array_buffer_transfer_map_;
DISALLOW_COPY_AND_ASSIGN(ValueSerializer); DISALLOW_COPY_AND_ASSIGN(ValueSerializer);
}; };
......
...@@ -59,7 +59,7 @@ class PatchDirectCallsHelper { ...@@ -59,7 +59,7 @@ class PatchDirectCallsHelper {
} // namespace } // namespace
CodeSpecialization::CodeSpecialization(Isolate* isolate, Zone* zone) CodeSpecialization::CodeSpecialization(Isolate* isolate, Zone* zone)
: objects_to_relocate(isolate->heap(), zone) {} : objects_to_relocate(isolate->heap(), ZoneAllocationPolicy(zone)) {}
CodeSpecialization::~CodeSpecialization() {} CodeSpecialization::~CodeSpecialization() {}
......
...@@ -60,7 +60,7 @@ class CodeSpecialization { ...@@ -60,7 +60,7 @@ class CodeSpecialization {
Handle<WasmInstanceObject> relocate_direct_calls_instance; Handle<WasmInstanceObject> relocate_direct_calls_instance;
bool has_objects_to_relocate = false; bool has_objects_to_relocate = false;
IdentityMap<Handle<Object>> objects_to_relocate; IdentityMap<Handle<Object>, ZoneAllocationPolicy> objects_to_relocate;
}; };
} // namespace wasm } // namespace wasm
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <set>
#include "src/factory.h" #include "src/factory.h"
#include "src/identity-map.h" #include "src/identity-map.h"
#include "src/isolate.h" #include "src/isolate.h"
...@@ -24,9 +26,9 @@ namespace internal { ...@@ -24,9 +26,9 @@ namespace internal {
// "move" objects to simulate GC for testing the internals of the map. // "move" objects to simulate GC for testing the internals of the map.
class IdentityMapTester : public HandleAndZoneScope { class IdentityMapTester : public HandleAndZoneScope {
public: public:
IdentityMap<void*> map; IdentityMap<void*, ZoneAllocationPolicy> map;
IdentityMapTester() : map(heap(), main_zone()) {} IdentityMapTester() : map(heap(), ZoneAllocationPolicy(main_zone())) {}
Heap* heap() { return isolate()->heap(); } Heap* heap() { return isolate()->heap(); }
Isolate* isolate() { return main_isolate(); } Isolate* isolate() { return main_isolate(); }
...@@ -79,6 +81,63 @@ class IdentityMapTester : public HandleAndZoneScope { ...@@ -79,6 +81,63 @@ class IdentityMapTester : public HandleAndZoneScope {
} }
} }
void TestFindDelete(Handle<Object> key1, void* val1, Handle<Object> key2,
void* val2) {
CHECK_NULL(map.Find(key1));
CHECK_NULL(map.Find(key2));
// Set {key1} and {key2} for the first time.
void** entry1 = map.Get(key1);
CHECK_NOT_NULL(entry1);
*entry1 = val1;
void** entry2 = map.Get(key2);
CHECK_NOT_NULL(entry2);
*entry2 = val2;
for (int i = 0; i < 3; i++) { // Find {key1} and {key2} 3 times.
{
void** nentry = map.Find(key2);
CHECK_EQ(val2, *nentry);
}
{
void** nentry = map.Find(key1);
CHECK_EQ(val1, *nentry);
}
}
// Delete {key1}
void* deleted_entry_1 = map.Delete(key1);
CHECK_NOT_NULL(deleted_entry_1);
deleted_entry_1 = val1;
for (int i = 0; i < 3; i++) { // Find {key1} and not {key2} 3 times.
{
void** nentry = map.Find(key1);
CHECK_NULL(nentry);
}
{
void** nentry = map.Find(key2);
CHECK_EQ(val2, *nentry);
}
}
// Delete {key2}
void* deleted_entry_2 = map.Delete(key2);
CHECK_NOT_NULL(deleted_entry_2);
deleted_entry_2 = val2;
for (int i = 0; i < 3; i++) { // Don't find {key1} and {key2} 3 times.
{
void** nentry = map.Find(key1);
CHECK_NULL(nentry);
}
{
void** nentry = map.Find(key2);
CHECK_NULL(nentry);
}
}
}
Handle<Smi> smi(int value) { Handle<Smi> smi(int value) {
return Handle<Smi>(Smi::FromInt(value), isolate()); return Handle<Smi>(Smi::FromInt(value), isolate());
} }
...@@ -88,7 +147,7 @@ class IdentityMapTester : public HandleAndZoneScope { ...@@ -88,7 +147,7 @@ class IdentityMapTester : public HandleAndZoneScope {
} }
void SimulateGCByIncrementingSmisBy(int shift) { void SimulateGCByIncrementingSmisBy(int shift) {
for (int i = 0; i < map.size_; i++) { for (int i = 0; i < map.capacity_; i++) {
if (map.keys_[i]->IsSmi()) { if (map.keys_[i]->IsSmi()) {
map.keys_[i] = Smi::FromInt(Smi::cast(map.keys_[i])->value() + shift); map.keys_[i] = Smi::FromInt(Smi::cast(map.keys_[i])->value() + shift);
} }
...@@ -108,16 +167,22 @@ class IdentityMapTester : public HandleAndZoneScope { ...@@ -108,16 +167,22 @@ class IdentityMapTester : public HandleAndZoneScope {
CHECK_EQ(value, *entry); CHECK_EQ(value, *entry);
} }
void CheckDelete(Handle<Object> key, void* value) {
void* entry = map.Delete(key);
CHECK_NOT_NULL(entry);
CHECK_EQ(value, entry);
}
void PrintMap() { void PrintMap() {
PrintF("{\n"); PrintF("{\n");
for (int i = 0; i < map.size_; i++) { for (int i = 0; i < map.capacity_; i++) {
PrintF(" %3d: %p => %p\n", i, reinterpret_cast<void*>(map.keys_[i]), PrintF(" %3d: %p => %p\n", i, reinterpret_cast<void*>(map.keys_[i]),
reinterpret_cast<void*>(map.values_[i])); reinterpret_cast<void*>(map.values_[i]));
} }
PrintF("}\n"); PrintF("}\n");
} }
void Resize() { map.Resize(); } void Resize() { map.Resize(map.capacity_ * 4); }
void Rehash() { map.Rehash(); } void Rehash() { map.Rehash(); }
}; };
...@@ -138,18 +203,46 @@ TEST(Find_num_not_found) { ...@@ -138,18 +203,46 @@ TEST(Find_num_not_found) {
} }
} }
TEST(Delete_smi_not_found) {
IdentityMapTester t;
for (int i = 0; i < 100; i++) {
CHECK_NULL(t.map.Delete(t.smi(i)));
}
}
TEST(Delete_num_not_found) {
IdentityMapTester t;
for (int i = 0; i < 100; i++) {
CHECK_NULL(t.map.Delete(t.num(i + 0.2)));
}
}
TEST(GetFind_smi_0) {
IdentityMapTester t;
t.TestGetFind(t.smi(0), t.isolate(), t.smi(1), t.heap());
}
TEST(GetFind_smi_13) { TEST(GetFind_smi_13) {
IdentityMapTester t; IdentityMapTester t;
t.TestGetFind(t.smi(13), t.isolate(), t.smi(17), t.heap()); t.TestGetFind(t.smi(13), t.isolate(), t.smi(17), t.heap());
} }
TEST(GetFind_num_13) { TEST(GetFind_num_13) {
IdentityMapTester t; IdentityMapTester t;
t.TestGetFind(t.num(13.1), t.isolate(), t.num(17.1), t.heap()); t.TestGetFind(t.num(13.1), t.isolate(), t.num(17.1), t.heap());
} }
TEST(Delete_smi_13) {
IdentityMapTester t;
t.TestFindDelete(t.smi(13), t.isolate(), t.smi(17), t.heap());
CHECK(t.map.empty());
}
TEST(Delete_num_13) {
IdentityMapTester t;
t.TestFindDelete(t.num(13.1), t.isolate(), t.num(17.1), t.heap());
CHECK(t.map.empty());
}
TEST(GetFind_smi_17m) { TEST(GetFind_smi_17m) {
const int kInterval = 17; const int kInterval = 17;
...@@ -179,6 +272,32 @@ TEST(GetFind_smi_17m) { ...@@ -179,6 +272,32 @@ TEST(GetFind_smi_17m) {
} }
} }
TEST(Delete_smi_17m) {
const int kInterval = 17;
const int kShift = 1099;
IdentityMapTester t;
for (int i = 1; i < 100; i += kInterval) {
t.map.Set(t.smi(i), reinterpret_cast<void*>(i + kShift));
}
for (int i = 1; i < 100; i += kInterval) {
t.CheckFind(t.smi(i), reinterpret_cast<void*>(i + kShift));
}
for (int i = 1; i < 100; i += kInterval) {
t.CheckDelete(t.smi(i), reinterpret_cast<void*>(i + kShift));
for (int j = 1; j < 100; j += kInterval) {
void** entry = t.map.Find(t.smi(j));
if (j <= i) {
CHECK_NULL(entry);
} else {
CHECK_NOT_NULL(entry);
CHECK_EQ(reinterpret_cast<void*>(j + kShift), *entry);
}
}
}
}
TEST(GetFind_num_1000) { TEST(GetFind_num_1000) {
const int kPrime = 137; const int kPrime = 137;
...@@ -191,6 +310,41 @@ TEST(GetFind_num_1000) { ...@@ -191,6 +310,41 @@ TEST(GetFind_num_1000) {
} }
} }
TEST(Delete_num_1000) {
const int kPrime = 137;
IdentityMapTester t;
for (int i = 0; i < 1000; i++) {
t.map.Set(t.smi(i * kPrime), reinterpret_cast<void*>(i * kPrime));
}
// Delete every second value in reverse.
for (int i = 999; i >= 0; i -= 2) {
void* entry = t.map.Delete(t.smi(i * kPrime));
CHECK_EQ(reinterpret_cast<void*>(i * kPrime), entry);
}
for (int i = 0; i < 1000; i++) {
void** entry = t.map.Find(t.smi(i * kPrime));
if (i % 2) {
CHECK_NULL(entry);
} else {
CHECK_NOT_NULL(entry);
CHECK_EQ(reinterpret_cast<void*>(i * kPrime), *entry);
}
}
// Delete the rest.
for (int i = 0; i < 1000; i += 2) {
void* entry = t.map.Delete(t.smi(i * kPrime));
CHECK_EQ(reinterpret_cast<void*>(i * kPrime), entry);
}
for (int i = 0; i < 1000; i++) {
void** entry = t.map.Find(t.smi(i * kPrime));
CHECK_NULL(entry);
}
}
TEST(GetFind_smi_gc) { TEST(GetFind_smi_gc) {
const int kKey = 33; const int kKey = 33;
...@@ -203,6 +357,15 @@ TEST(GetFind_smi_gc) { ...@@ -203,6 +357,15 @@ TEST(GetFind_smi_gc) {
t.CheckGet(t.smi(kKey + kShift), &t); t.CheckGet(t.smi(kKey + kShift), &t);
} }
TEST(Delete_smi_gc) {
const int kKey = 33;
const int kShift = 1211;
IdentityMapTester t;
t.map.Set(t.smi(kKey), &t);
t.SimulateGCByIncrementingSmisBy(kShift);
t.CheckDelete(t.smi(kKey + kShift), &t);
}
TEST(GetFind_smi_gc2) { TEST(GetFind_smi_gc2) {
int kKey1 = 1; int kKey1 = 1;
...@@ -219,6 +382,18 @@ TEST(GetFind_smi_gc2) { ...@@ -219,6 +382,18 @@ TEST(GetFind_smi_gc2) {
t.CheckGet(t.smi(kKey2 + kShift), &kKey2); t.CheckGet(t.smi(kKey2 + kShift), &kKey2);
} }
TEST(Delete_smi_gc2) {
int kKey1 = 1;
int kKey2 = 33;
const int kShift = 1211;
IdentityMapTester t;
t.map.Set(t.smi(kKey1), &kKey1);
t.map.Set(t.smi(kKey2), &kKey2);
t.SimulateGCByIncrementingSmisBy(kShift);
t.CheckDelete(t.smi(kKey1 + kShift), &kKey1);
t.CheckDelete(t.smi(kKey2 + kShift), &kKey2);
}
TEST(GetFind_smi_gc_n) { TEST(GetFind_smi_gc_n) {
const int kShift = 12011; const int kShift = 12011;
...@@ -245,6 +420,22 @@ TEST(GetFind_smi_gc_n) { ...@@ -245,6 +420,22 @@ TEST(GetFind_smi_gc_n) {
} }
} }
TEST(Delete_smi_gc_n) {
const int kShift = 12011;
IdentityMapTester t;
int keys[12] = {1, 2, 7, 8, 15, 23,
1 + 32, 2 + 32, 7 + 32, 8 + 32, 15 + 32, 23 + 32};
// Initialize the map first.
for (size_t i = 0; i < arraysize(keys); i++) {
t.map.Set(t.smi(keys[i]), &keys[i]);
}
// Simulate a GC by "moving" the smis in the internal keys array.
t.SimulateGCByIncrementingSmisBy(kShift);
// Check that deleting for the incremented smis finds the same values.
for (size_t i = 0; i < arraysize(keys); i++) {
t.CheckDelete(t.smi(keys[i] + kShift), &keys[i]);
}
}
TEST(GetFind_smi_num_gc_n) { TEST(GetFind_smi_num_gc_n) {
const int kShift = 12019; const int kShift = 12019;
...@@ -285,6 +476,158 @@ TEST(GetFind_smi_num_gc_n) { ...@@ -285,6 +476,158 @@ TEST(GetFind_smi_num_gc_n) {
} }
} }
TEST(Delete_smi_num_gc_n) {
const int kShift = 12019;
IdentityMapTester t;
int smi_keys[] = {1, 2, 7, 15, 23};
Handle<Object> num_keys[] = {t.num(1.1), t.num(2.2), t.num(3.3), t.num(4.4),
t.num(5.5), t.num(6.6), t.num(7.7), t.num(8.8),
t.num(9.9), t.num(10.1)};
// Initialize the map first.
for (size_t i = 0; i < arraysize(smi_keys); i++) {
t.map.Set(t.smi(smi_keys[i]), &smi_keys[i]);
}
for (size_t i = 0; i < arraysize(num_keys); i++) {
t.map.Set(num_keys[i], &num_keys[i]);
}
// Simulate a GC by moving SMIs.
// Ironically the SMIs "move", but the heap numbers don't!
t.SimulateGCByIncrementingSmisBy(kShift);
// Check that deleting for the incremented smis finds the same values.
for (size_t i = 0; i < arraysize(smi_keys); i++) {
t.CheckDelete(t.smi(smi_keys[i] + kShift), &smi_keys[i]);
}
// Check that deleting the numbers finds the same values.
for (size_t i = 0; i < arraysize(num_keys); i++) {
t.CheckDelete(num_keys[i], &num_keys[i]);
}
}
TEST(Delete_smi_resizes) {
const int kKeyCount = 1024;
const int kValueOffset = 27;
IdentityMapTester t;
// Insert one element to initialize map.
t.map.Set(t.smi(0), reinterpret_cast<void*>(kValueOffset));
int initial_capacity = t.map.capacity();
CHECK_LT(initial_capacity, kKeyCount);
// Insert another kKeyCount - 1 keys.
for (int i = 1; i < kKeyCount; i++) {
t.map.Set(t.smi(i), reinterpret_cast<void*>(i + kValueOffset));
}
// Check capacity increased.
CHECK_GT(t.map.capacity(), initial_capacity);
CHECK_GE(t.map.capacity(), kKeyCount);
// Delete all the keys.
for (int i = 0; i < kKeyCount; i++) {
t.CheckDelete(t.smi(i), reinterpret_cast<void*>(i + kValueOffset));
}
// Should resize back to initial capacity.
CHECK_EQ(t.map.capacity(), initial_capacity);
}
TEST(Iterator_smi_num) {
IdentityMapTester t;
int smi_keys[] = {1, 2, 7, 15, 23};
Handle<Object> num_keys[] = {t.num(1.1), t.num(2.2), t.num(3.3), t.num(4.4),
t.num(5.5), t.num(6.6), t.num(7.7), t.num(8.8),
t.num(9.9), t.num(10.1)};
// Initialize the map.
for (size_t i = 0; i < arraysize(smi_keys); i++) {
t.map.Set(t.smi(smi_keys[i]), reinterpret_cast<void*>(i));
}
for (size_t i = 0; i < arraysize(num_keys); i++) {
t.map.Set(num_keys[i], reinterpret_cast<void*>(i + 5));
}
// Check iterator sees all values.
std::set<intptr_t> seen;
{
IdentityMap<void*, ZoneAllocationPolicy>::IteratableScope it_scope(&t.map);
for (auto it = it_scope.begin(); it != it_scope.end(); ++it) {
seen.insert(reinterpret_cast<intptr_t>(**it));
}
}
for (intptr_t i = 0; i < 15; i++) {
CHECK(seen.find(i) != seen.end());
}
}
TEST(Iterator_smi_num_gc) {
const int kShift = 16039;
IdentityMapTester t;
int smi_keys[] = {1, 2, 7, 15, 23};
Handle<Object> num_keys[] = {t.num(1.1), t.num(2.2), t.num(3.3), t.num(4.4),
t.num(5.5), t.num(6.6), t.num(7.7), t.num(8.8),
t.num(9.9), t.num(10.1)};
// Initialize the map.
for (size_t i = 0; i < arraysize(smi_keys); i++) {
t.map.Set(t.smi(smi_keys[i]), reinterpret_cast<void*>(i));
}
for (size_t i = 0; i < arraysize(num_keys); i++) {
t.map.Set(num_keys[i], reinterpret_cast<void*>(i + 5));
}
// Simulate GC by moving the SMIs.
t.SimulateGCByIncrementingSmisBy(kShift);
// Check iterator sees all values.
std::set<intptr_t> seen;
{
IdentityMap<void*, ZoneAllocationPolicy>::IteratableScope it_scope(&t.map);
for (auto it = it_scope.begin(); it != it_scope.end(); ++it) {
seen.insert(reinterpret_cast<intptr_t>(**it));
}
}
for (intptr_t i = 0; i < 15; i++) {
CHECK(seen.find(i) != seen.end());
}
}
TEST(Iterator_smi_delete) {
IdentityMapTester t;
int smi_keys[] = {1, 2, 7, 15, 23};
// Initialize the map.
for (size_t i = 0; i < arraysize(smi_keys); i++) {
t.map.Set(t.smi(smi_keys[i]), reinterpret_cast<void*>(i));
}
// Iterate and delete half the elements.
std::set<intptr_t> deleted;
{
int i = 0;
IdentityMap<void*, ZoneAllocationPolicy>::IteratableScope it_scope(&t.map);
for (auto it = it_scope.begin(); it != it_scope.end();) {
if (i % 2) {
deleted.insert(reinterpret_cast<intptr_t>(**it));
it.DeleteAndIncrement();
} else {
++it;
}
}
}
// Check values in map are correct.
for (intptr_t i = 0; i < 5; i++) {
void** entry = t.map.Find(t.smi(smi_keys[i]));
if (deleted.find(i) != deleted.end()) {
CHECK_NULL(entry);
} else {
CHECK_NOT_NULL(entry);
CHECK_EQ(reinterpret_cast<void*>(i), *entry);
}
}
}
void CollisionTest(int stride, bool rehash = false, bool resize = false) { void CollisionTest(int stride, bool rehash = false, bool resize = false) {
for (int load = 15; load <= 120; load = load * 2) { for (int load = 15; load <= 120; load = load * 2) {
...@@ -313,7 +656,6 @@ void CollisionTest(int stride, bool rehash = false, bool resize = false) { ...@@ -313,7 +656,6 @@ void CollisionTest(int stride, bool rehash = false, bool resize = false) {
} }
} }
TEST(Collisions_1) { CollisionTest(1); } TEST(Collisions_1) { CollisionTest(1); }
TEST(Collisions_2) { CollisionTest(2); } TEST(Collisions_2) { CollisionTest(2); }
TEST(Collisions_3) { CollisionTest(3); } TEST(Collisions_3) { CollisionTest(3); }
......
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