Commit b42ecda5 authored by leszeks's avatar leszeks Committed by Commit bot

[base] Move hashmap allocator to a field

Moves the hashmap's allocator from being a parameter in the various
hashmap functions, to being a field in the hashmap itself. This

1. Protects against incorrectly passed allocators, and
2. Cleans up the API so that e.g. callers don't have to store their
   allocator

This is part of a wider set of changes discussed in:
https://groups.google.com/forum/#!topic/v8-dev/QLsC0XPYLeM

Review-Url: https://codereview.chromium.org/2345233003
Cr-Commit-Position: refs/heads/master@{#39538}
parent 044a62be
......@@ -361,8 +361,8 @@ bool AsmTyper::AddGlobal(Variable* variable, VariableInfo* info) {
return false;
}
ZoneHashMap::Entry* entry = global_scope_.LookupOrInsert(
variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
ZoneHashMap::Entry* entry =
global_scope_.LookupOrInsert(variable, ComputePointerHash(variable));
if (entry->value != nullptr) {
return false;
......@@ -378,8 +378,8 @@ bool AsmTyper::AddLocal(Variable* variable, VariableInfo* info) {
DCHECK(!info->IsGlobal());
DCHECK(ValidAsmIdentifier(variable->name()));
ZoneHashMap::Entry* entry = local_scope_.LookupOrInsert(
variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
ZoneHashMap::Entry* entry =
local_scope_.LookupOrInsert(variable, ComputePointerHash(variable));
if (entry->value != nullptr) {
return false;
......
......@@ -664,8 +664,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
FunctionTableIndices* container = new (zone()) FunctionTableIndices();
container->start_index = start_index;
container->signature_index = signature_index;
ZoneHashMap::Entry* entry = function_tables_.LookupOrInsert(
v, ComputePointerHash(v), ZoneAllocationPolicy(zone()));
ZoneHashMap::Entry* entry =
function_tables_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = container;
}
......@@ -700,8 +700,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
void AddImport(Variable* v, const char* name, int name_length) {
ImportedFunctionIndices* indices = new (builder_->zone())
ImportedFunctionIndices(name, name_length, builder_->zone());
ZoneHashMap::Entry* entry = table_.LookupOrInsert(
v, ComputePointerHash(v), ZoneAllocationPolicy(builder_->zone()));
ZoneHashMap::Entry* entry =
table_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = indices;
}
......@@ -1694,8 +1694,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
index = current_function_builder_->AddLocal(type);
IndexContainer* container = new (zone()) IndexContainer();
container->index = index;
entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v),
ZoneAllocationPolicy(zone()));
entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = container;
}
return (reinterpret_cast<IndexContainer*>(entry->value))->index;
......@@ -1709,8 +1708,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
DCHECK_NULL(entry);
IndexContainer* container = new (zone()) IndexContainer();
container->index = index;
entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v),
ZoneAllocationPolicy(zone()));
entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = container;
}
......@@ -1721,8 +1719,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
uint32_t index = builder_->AddGlobal(type, 0);
IndexContainer* container = new (zone()) IndexContainer();
container->index = index;
entry = global_variables_.LookupOrInsert(v, ComputePointerHash(v),
ZoneAllocationPolicy(zone()));
entry = global_variables_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = container;
}
return (reinterpret_cast<IndexContainer*>(entry->value))->index;
......@@ -1735,8 +1732,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
uint32_t index = builder_->AddFunction();
IndexContainer* container = new (zone()) IndexContainer();
container->index = index;
entry = functions_.LookupOrInsert(v, ComputePointerHash(v),
ZoneAllocationPolicy(zone()));
entry = functions_.LookupOrInsert(v, ComputePointerHash(v));
entry->value = container;
}
return (reinterpret_cast<IndexContainer*>(entry->value))->index;
......
......@@ -472,7 +472,7 @@ void ObjectLiteral::CalculateEmitStore(Zone* zone) {
// If there is an existing entry do not emit a store unless the previous
// entry was also an accessor.
uint32_t hash = literal->Hash();
ZoneHashMap::Entry* entry = table.LookupOrInsert(literal, hash, allocator);
ZoneHashMap::Entry* entry = table.LookupOrInsert(literal, hash);
if (entry->value != NULL) {
auto previous_kind =
static_cast<ObjectLiteral::Property*>(entry->value)->kind();
......
......@@ -134,14 +134,13 @@ typedef ZoneList<Handle<Object>> ZoneObjectList;
class FeedbackVectorSlotCache {
public:
explicit FeedbackVectorSlotCache(Zone* zone)
: zone_(zone),
hash_map_(base::HashMap::PointersMatch,
: hash_map_(base::HashMap::PointersMatch,
ZoneHashMap::kDefaultHashMapCapacity,
ZoneAllocationPolicy(zone)) {}
void Put(Variable* variable, FeedbackVectorSlot slot) {
ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
ZoneHashMap::Entry* entry =
hash_map_.LookupOrInsert(variable, ComputePointerHash(variable));
entry->value = reinterpret_cast<void*>(slot.ToInt());
}
......@@ -150,7 +149,6 @@ class FeedbackVectorSlotCache {
}
private:
Zone* zone_;
ZoneHashMap hash_map_;
};
......@@ -1530,7 +1528,7 @@ class AccessorTable
zone_(zone) {}
Iterator lookup(Literal* literal) {
Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
Iterator it = find(literal, true);
if (it->second == NULL) it->second = new (zone_) ObjectLiteral::Accessors();
return it;
}
......
......@@ -36,9 +36,8 @@ Variable* VariableMap::Declare(Zone* zone, Scope* scope,
// AstRawStrings are unambiguous, i.e., the same string is always represented
// by the same AstRawString*.
// FIXME(marja): fix the type of Lookup.
Entry* p =
ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(),
ZoneAllocationPolicy(zone));
Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name),
name->hash());
if (added) *added = p->value == nullptr;
if (p->value == nullptr) {
// The variable has not been declared yet -> insert it.
......@@ -54,11 +53,10 @@ void VariableMap::Remove(Variable* var) {
ZoneHashMap::Remove(const_cast<AstRawString*>(name), name->hash());
}
void VariableMap::Add(Zone* zone, Variable* var) {
void VariableMap::Add(Variable* var) {
const AstRawString* name = var->raw_name();
Entry* p =
ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(),
ZoneAllocationPolicy(zone));
Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name),
name->hash());
DCHECK_NULL(p->value);
DCHECK_EQ(name, p->key);
p->value = var;
......@@ -77,13 +75,12 @@ Variable* VariableMap::Lookup(const AstRawString* name) {
SloppyBlockFunctionMap::SloppyBlockFunctionMap(Zone* zone)
: ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)) {}
void SloppyBlockFunctionMap::Declare(Zone* zone, const AstRawString* name,
void SloppyBlockFunctionMap::Declare(const AstRawString* name,
SloppyBlockFunctionStatement* stmt) {
// AstRawStrings are unambiguous, i.e., the same string is always represented
// by the same AstRawString*.
Entry* p =
ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(),
ZoneAllocationPolicy(zone));
Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name),
name->hash());
stmt->set_next(static_cast<SloppyBlockFunctionStatement*>(p->value));
p->value = stmt;
}
......@@ -697,7 +694,7 @@ void Scope::Snapshot::Reparent(DeclarationScope* new_parent) const {
new_parent->AddLocal(local);
if (local->mode() == VAR) {
outer_closure->variables_.Remove(local);
new_parent->variables_.Add(new_parent->zone(), local);
new_parent->variables_.Add(local);
}
}
outer_closure->locals_.Rewind(top_local_);
......
......@@ -35,7 +35,7 @@ class VariableMap: public ZoneHashMap {
Variable* Lookup(const AstRawString* name);
void Remove(Variable* var);
void Add(Zone* zone, Variable* var);
void Add(Variable* var);
};
......@@ -43,7 +43,7 @@ class VariableMap: public ZoneHashMap {
class SloppyBlockFunctionMap : public ZoneHashMap {
public:
explicit SloppyBlockFunctionMap(Zone* zone);
void Declare(Zone* zone, const AstRawString* name,
void Declare(const AstRawString* name,
SloppyBlockFunctionStatement* statement);
};
......@@ -752,7 +752,7 @@ class DeclarationScope : public Scope {
void DeclareSloppyBlockFunction(const AstRawString* name,
SloppyBlockFunctionStatement* statement) {
sloppy_block_function_map_.Declare(zone(), name, statement);
sloppy_block_function_map_.Declare(name, statement);
}
// Go through sloppy_block_function_map_ and hoist those (into this scope)
......
......@@ -55,11 +55,9 @@ class TemplateHashMapImpl {
// If an entry with matching key is found, returns that entry.
// If no matching entry is found, a new entry is inserted with
// corresponding key, key hash, and default initialized value.
Entry* LookupOrInsert(const Key& key, uint32_t hash,
AllocationPolicy allocator = AllocationPolicy());
Entry* LookupOrInsert(const Key& key, uint32_t hash);
Entry* InsertNew(const Key& key, uint32_t hash,
AllocationPolicy allocator = AllocationPolicy());
Entry* InsertNew(const Key& key, uint32_t hash);
// Removes the entry with matching key.
// It returns the value of the deleted entry
......@@ -102,11 +100,12 @@ class TemplateHashMapImpl {
Entry* map_;
uint32_t capacity_;
uint32_t occupancy_;
AllocationPolicy allocator_;
Entry* map_end() const { return map_ + capacity_; }
Entry* Probe(const Key& key, uint32_t hash) const;
void Initialize(uint32_t capacity, AllocationPolicy allocator);
void Resize(AllocationPolicy allocator);
void Initialize(uint32_t capacity);
void Resize();
};
template <typename Key>
......@@ -125,14 +124,15 @@ typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap;
template <typename Key, typename Value, class AllocationPolicy>
TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl(
MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) {
MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator)
: allocator_(allocator) {
match_ = match;
Initialize(initial_capacity, allocator);
Initialize(initial_capacity);
}
template <typename Key, typename Value, class AllocationPolicy>
TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() {
AllocationPolicy::Delete(map_);
allocator_.Delete(map_);
}
template <typename Key, typename Value, class AllocationPolicy>
......@@ -146,20 +146,20 @@ TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key,
template <typename Key, typename Value, class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert(
const Key& key, uint32_t hash, AllocationPolicy allocator) {
const Key& key, uint32_t hash) {
// Find a matching entry.
Entry* p = Probe(key, hash);
if (p->exists()) {
return p;
}
return InsertNew(key, hash, allocator);
return InsertNew(key, hash);
}
template <typename Key, typename Value, class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(
const Key& key, uint32_t hash, AllocationPolicy allocator) {
TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(const Key& key,
uint32_t hash) {
// Find a matching entry.
Entry* p = Probe(key, hash);
DCHECK(!p->exists());
......@@ -171,7 +171,7 @@ TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(
// Grow the map if we reached >= 80% occupancy.
if (occupancy_ + occupancy_ / 4 >= capacity_) {
Resize(allocator);
Resize();
p = Probe(key, hash);
}
......@@ -290,9 +290,9 @@ TemplateHashMapImpl<Key, Value, AllocationPolicy>::Probe(const Key& key,
template <typename Key, typename Value, class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize(
uint32_t capacity, AllocationPolicy allocator) {
uint32_t capacity) {
DCHECK(base::bits::IsPowerOfTwo32(capacity));
map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
map_ = reinterpret_cast<Entry*>(allocator_.New(capacity * sizeof(Entry)));
if (map_ == nullptr) {
FATAL("Out of memory: HashMap::Initialize");
return;
......@@ -302,25 +302,24 @@ void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize(
}
template <typename Key, typename Value, class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize(
AllocationPolicy allocator) {
void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize() {
Entry* map = map_;
uint32_t n = occupancy_;
// Allocate larger map.
Initialize(capacity_ * 2, allocator);
Initialize(capacity_ * 2);
// Rehash all current entries.
for (Entry* p = map; n > 0; p++) {
if (p->exists()) {
Entry* entry = LookupOrInsert(p->key, p->hash, allocator);
Entry* entry = LookupOrInsert(p->key, p->hash);
entry->value = p->value;
n--;
}
}
// Delete old map.
AllocationPolicy::Delete(map);
allocator_.Delete(map);
}
// A hash map for pointer keys and values with an STL-like interface.
......@@ -368,10 +367,9 @@ class TemplateHashMap
Iterator begin() const { return Iterator(this, this->Start()); }
Iterator end() const { return Iterator(this, nullptr); }
Iterator find(Key* key, bool insert = false,
AllocationPolicy allocator = AllocationPolicy()) {
Iterator find(Key* key, bool insert = false) {
if (insert) {
return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
return Iterator(this, this->LookupOrInsert(key, key->Hash()));
}
return Iterator(this, this->Lookup(key, key->Hash()));
}
......
......@@ -104,8 +104,7 @@ int StateValuesHashKey(Node** nodes, size_t count) {
Node* StateValuesCache::GetValuesNodeFromCache(Node** nodes, size_t count) {
StateValuesKey key(count, nodes);
int hash = StateValuesHashKey(nodes, count);
ZoneHashMap::Entry* lookup =
hash_map_.LookupOrInsert(&key, hash, ZoneAllocationPolicy(zone()));
ZoneHashMap::Entry* lookup = hash_map_.LookupOrInsert(&key, hash);
DCHECK_NOT_NULL(lookup);
Node* node;
if (lookup->value == nullptr) {
......
......@@ -316,16 +316,14 @@ BoundsCheckTable::BoundsCheckTable(Zone* zone)
BoundsCheckBbData** BoundsCheckTable::LookupOrInsert(BoundsCheckKey* key,
Zone* zone) {
return reinterpret_cast<BoundsCheckBbData**>(
&(ZoneHashMap::LookupOrInsert(key, key->Hash(),
ZoneAllocationPolicy(zone))->value));
&(ZoneHashMap::LookupOrInsert(key, key->Hash())->value));
}
void BoundsCheckTable::Insert(BoundsCheckKey* key,
BoundsCheckBbData* data,
Zone* zone) {
ZoneHashMap::LookupOrInsert(key, key->Hash(), ZoneAllocationPolicy(zone))
->value = data;
ZoneHashMap::LookupOrInsert(key, key->Hash())->value = data;
}
......
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