Commit 05463c49 authored by adamk@chromium.org's avatar adamk@chromium.org

Avoid unnecessary hashing in OrderedHashTable

Add an overload of OrderedHashTable::FindEntry that takes
a hash along with the key to allow callsites which need to
re-use the hash (such as Add()) to avoid recomputing it.

On my Macbook this results in improvements on the Collections
microbenchmarks:
  Map-Collections: +4%
  Set-Collections: +5%

R=mstarzinger@chromium.org

Review URL: https://codereview.chromium.org/373323002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22308 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 79ddf746
......@@ -16138,17 +16138,14 @@ Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash(
}
template<class Derived, class Iterator, int entrysize>
template <class Derived, class Iterator, int entrysize>
int OrderedHashTable<Derived, Iterator, entrysize>::FindEntry(
Handle<Object> key) {
Handle<Object> key, int hash) {
ASSERT(!IsObsolete());
DisallowHeapAllocation no_gc;
ASSERT(!key->IsTheHole());
Object* hash = key->GetHash();
if (hash->IsUndefined()) return kNotFound;
for (int entry = HashToEntry(Smi::cast(hash)->value());
entry != kNotFound;
for (int entry = HashToEntry(hash); entry != kNotFound;
entry = ChainAt(entry)) {
Object* candidate = KeyAt(entry);
if (candidate->SameValueZero(*key))
......@@ -16158,7 +16155,17 @@ int OrderedHashTable<Derived, Iterator, entrysize>::FindEntry(
}
template<class Derived, class Iterator, int entrysize>
template <class Derived, class Iterator, int entrysize>
int OrderedHashTable<Derived, Iterator, entrysize>::FindEntry(
Handle<Object> key) {
DisallowHeapAllocation no_gc;
Object* hash = key->GetHash();
if (!hash->IsSmi()) return kNotFound;
return FindEntry(key, Smi::cast(hash)->value());
}
template <class Derived, class Iterator, int entrysize>
int OrderedHashTable<Derived, Iterator, entrysize>::AddEntry(int hash) {
ASSERT(!IsObsolete());
......@@ -16206,8 +16213,9 @@ template Handle<OrderedHashSet>
OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Remove(
Handle<OrderedHashSet> table, Handle<Object> key, bool* was_present);
template int
OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::FindEntry(
template int OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::FindEntry(
Handle<Object> key, int hash);
template int OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::FindEntry(
Handle<Object> key);
template int
......@@ -16237,8 +16245,9 @@ template Handle<OrderedHashMap>
OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Remove(
Handle<OrderedHashMap> table, Handle<Object> key, bool* was_present);
template int
OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::FindEntry(
template int OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::FindEntry(
Handle<Object> key, int hash);
template int OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::FindEntry(
Handle<Object> key);
template int
......@@ -16255,12 +16264,12 @@ bool OrderedHashSet::Contains(Handle<Object> key) {
Handle<OrderedHashSet> OrderedHashSet::Add(Handle<OrderedHashSet> table,
Handle<Object> key) {
if (table->FindEntry(key) != kNotFound) return table;
int hash = GetOrCreateHash(table->GetIsolate(), key)->value();
if (table->FindEntry(key, hash) != kNotFound) return table;
table = EnsureGrowable(table);
Handle<Smi> hash = GetOrCreateHash(table->GetIsolate(), key);
int index = table->AddEntry(hash->value());
int index = table->AddEntry(hash);
table->set(index, *key);
return table;
}
......@@ -16279,7 +16288,8 @@ Handle<OrderedHashMap> OrderedHashMap::Put(Handle<OrderedHashMap> table,
Handle<Object> value) {
ASSERT(!key->IsTheHole());
int entry = table->FindEntry(key);
int hash = GetOrCreateHash(table->GetIsolate(), key)->value();
int entry = table->FindEntry(key, hash);
if (entry != kNotFound) {
table->set(table->EntryToIndex(entry) + kValueOffset, *value);
......@@ -16288,8 +16298,7 @@ Handle<OrderedHashMap> OrderedHashMap::Put(Handle<OrderedHashMap> table,
table = EnsureGrowable(table);
Handle<Smi> hash = GetOrCreateHash(table->GetIsolate(), key);
int index = table->AddEntry(hash->value());
int index = table->AddEntry(hash);
table->set(index, *key);
table->set(index + kValueOffset, *value);
return table;
......
......@@ -4362,6 +4362,9 @@ class OrderedHashTable: public FixedArray {
bool* was_present);
// Returns kNotFound if the key isn't present.
int FindEntry(Handle<Object> key, int hash);
// Like the above, but doesn't require the caller to provide a hash.
int FindEntry(Handle<Object> key);
int NumberOfElements() {
......
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