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)
prev_canonical_scope_ = handle_scope_data->canonical_scope;
handle_scope_data->canonical_scope = this;
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;
}
......
......@@ -331,7 +331,7 @@ class HandleScope {
// Forward declarations for CanonicalHandleScope.
template <typename V>
template <typename V, class AllocationPolicy>
class IdentityMap;
class RootIndexMap;
......@@ -352,7 +352,7 @@ class V8_EXPORT_PRIVATE CanonicalHandleScope final {
Isolate* isolate_;
Zone zone_;
RootIndexMap* root_index_map_;
IdentityMap<Object**>* identity_map_;
IdentityMap<Object**, ZoneAllocationPolicy>* identity_map_;
// Ordinary nested handle scopes within the current one are not canonical.
int canonical_level_;
// We may have nested canonical scopes. Handles are canonical within each one.
......
This diff is collapsed.
......@@ -13,11 +13,16 @@ namespace internal {
// Forward declarations.
class Heap;
class Zone;
// Base class of identity maps contains shared code for all template
// instantions.
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:
// Allow Tester to access internals, including changing the address of objects
// within the {keys_} array in order to simulate a moving GC.
......@@ -25,51 +30,68 @@ class IdentityMapBase {
typedef void** RawEntry;
IdentityMapBase(Heap* heap, Zone* zone)
explicit IdentityMapBase(Heap* heap)
: heap_(heap),
zone_(zone),
gc_counter_(-1),
size_(0),
capacity_(0),
mask_(0),
keys_(nullptr),
values_(nullptr) {}
~IdentityMapBase();
values_(nullptr),
is_iterable_(false) {}
virtual ~IdentityMapBase();
RawEntry GetEntry(Object* key);
RawEntry FindEntry(Object* key);
RawEntry FindEntry(Object* key) const;
void* DeleteEntry(Object* key);
void* DeleteIndex(int index);
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:
// Internal implementation should not be called directly by subclasses.
int LookupIndex(Object* address);
int InsertIndex(Object* address);
int ScanKeysFor(Object* address) const;
int InsertKey(Object* address);
int Lookup(Object* key) const;
int LookupOrInsert(Object* key);
void Rehash();
void Resize();
RawEntry Lookup(Object* key);
RawEntry Insert(Object* key);
int Hash(Object* address);
void Resize(int new_capacity);
int Hash(Object* address) const;
base::hash<uintptr_t> hasher_;
Heap* heap_;
Zone* zone_;
int gc_counter_;
int size_;
int capacity_;
int mask_;
Object** keys_;
void** values_;
bool is_iterable_;
DISALLOW_COPY_AND_ASSIGN(IdentityMapBase);
};
// 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
// supplied {heap}.
// * 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 not be a heap type.
template <typename V>
template <typename V, class AllocationPolicy>
class IdentityMap : public IdentityMapBase {
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
// as the identity, returning:
......@@ -82,16 +104,77 @@ class IdentityMap : public IdentityMapBase {
// as the identity, returning:
// found => a pointer to the storage location for the value
// not found => {nullptr}
V* Find(Handle<Object> key) { return Find(*key); }
V* Find(Object* key) { return reinterpret_cast<V*>(FindEntry(key)); }
V* Find(Handle<Object> key) const { return Find(*key); }
V* Find(Object* key) const { return reinterpret_cast<V*>(FindEntry(key)); }
// Set the value for the given key.
void Set(Handle<Object> key, V v) { Set(*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.
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 v8
......
......@@ -802,7 +802,8 @@ Maybe<bool> KeyAccumulator::CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
Zone set_zone(isolate_->allocator(), ZONE_NAME);
const int kPresent = 1;
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;
for (int i = 0; i < trap_result->length(); ++i) {
DCHECK(trap_result->get(i)->IsUniqueName());
......
......@@ -152,8 +152,9 @@ ValueSerializer::ValueSerializer(Isolate* isolate,
: isolate_(isolate),
delegate_(delegate),
zone_(isolate->allocator(), ZONE_NAME),
id_map_(isolate->heap(), &zone_),
array_buffer_transfer_map_(isolate->heap(), &zone_) {}
id_map_(isolate->heap(), ZoneAllocationPolicy(&zone_)),
array_buffer_transfer_map_(isolate->heap(),
ZoneAllocationPolicy(&zone_)) {}
ValueSerializer::~ValueSerializer() {
if (buffer_) {
......
......@@ -157,11 +157,11 @@ class ValueSerializer {
// 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
// the entry is new).
IdentityMap<uint32_t> id_map_;
IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_;
uint32_t next_id_ = 0;
// 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);
};
......
......@@ -59,7 +59,7 @@ class PatchDirectCallsHelper {
} // namespace
CodeSpecialization::CodeSpecialization(Isolate* isolate, Zone* zone)
: objects_to_relocate(isolate->heap(), zone) {}
: objects_to_relocate(isolate->heap(), ZoneAllocationPolicy(zone)) {}
CodeSpecialization::~CodeSpecialization() {}
......
......@@ -60,7 +60,7 @@ class CodeSpecialization {
Handle<WasmInstanceObject> relocate_direct_calls_instance;
bool has_objects_to_relocate = false;
IdentityMap<Handle<Object>> objects_to_relocate;
IdentityMap<Handle<Object>, ZoneAllocationPolicy> objects_to_relocate;
};
} // namespace wasm
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment