Reland r20960: "HashTable::EnsureCapacity() handlified."

R=yangguo@chromium.org

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20979 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
ishell@chromium.org 2014-04-25 12:59:07 +00:00
parent ff884e06ae
commit 88734238ec
2 changed files with 40 additions and 55 deletions

View File

@ -14673,7 +14673,9 @@ int NameDictionary::FindEntry(Handle<Name> key) {
template<typename Derived, typename Shape, typename Key> template<typename Derived, typename Shape, typename Key>
void HashTable<Derived, Shape, Key>::Rehash(Derived* new_table, Key key) { void HashTable<Derived, Shape, Key>::Rehash(
Handle<Derived> new_table,
Key key) {
ASSERT(NumberOfElements() < new_table->Capacity()); ASSERT(NumberOfElements() < new_table->Capacity());
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
@ -14774,39 +14776,6 @@ void HashTable<Derived, Shape, Key>::Rehash(Key key) {
} }
template<typename Derived, typename Shape, typename Key>
MaybeObject* HashTable<Derived, Shape, Key>::EnsureCapacity(
int n,
Key key,
PretenureFlag pretenure) {
int capacity = Capacity();
int nof = NumberOfElements() + n;
int nod = NumberOfDeletedElements();
// Return if:
// 50% is still free after adding n elements and
// at most 50% of the free elements are deleted elements.
if (nod <= (capacity - nof) >> 1) {
int needed_free = nof >> 1;
if (nof + needed_free <= capacity) return this;
}
const int kMinCapacityForPretenure = 256;
bool should_pretenure = pretenure == TENURED ||
((capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this));
Object* obj;
{ MaybeObject* maybe_obj =
Allocate(GetHeap(),
nof * 2,
USE_DEFAULT_MINIMUM_CAPACITY,
should_pretenure ? TENURED : NOT_TENURED);
if (!maybe_obj->ToObject(&obj)) return maybe_obj;
}
Rehash(Derived::cast(obj), key);
return Derived::cast(obj);
}
template<typename Derived, typename Shape, typename Key> template<typename Derived, typename Shape, typename Key>
Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity( Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
Handle<Derived> table, Handle<Derived> table,
@ -14814,10 +14783,29 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
Key key, Key key,
PretenureFlag pretenure) { PretenureFlag pretenure) {
Isolate* isolate = table->GetIsolate(); Isolate* isolate = table->GetIsolate();
CALL_HEAP_FUNCTION( int capacity = table->Capacity();
int nof = table->NumberOfElements() + n;
int nod = table->NumberOfDeletedElements();
// Return if:
// 50% is still free after adding n elements and
// at most 50% of the free elements are deleted elements.
if (nod <= (capacity - nof) >> 1) {
int needed_free = nof >> 1;
if (nof + needed_free <= capacity) return table;
}
const int kMinCapacityForPretenure = 256;
bool should_pretenure = pretenure == TENURED ||
((capacity > kMinCapacityForPretenure) &&
!isolate->heap()->InNewSpace(*table));
Handle<Derived> new_table = HashTable::New(
isolate, isolate,
static_cast<HashTable*>(*table)->EnsureCapacity(n, key, pretenure), nof * 2,
Derived); USE_DEFAULT_MINIMUM_CAPACITY,
should_pretenure ? TENURED : NOT_TENURED);
table->Rehash(new_table, key);
return new_table;
} }
@ -14842,13 +14830,13 @@ Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
bool pretenure = bool pretenure =
(at_least_room_for > kMinCapacityForPretenure) && (at_least_room_for > kMinCapacityForPretenure) &&
!isolate->heap()->InNewSpace(*table); !isolate->heap()->InNewSpace(*table);
Handle<Derived> new_table = New( Handle<Derived> new_table = HashTable::New(
isolate, isolate,
at_least_room_for, at_least_room_for,
USE_DEFAULT_MINIMUM_CAPACITY, USE_DEFAULT_MINIMUM_CAPACITY,
pretenure ? TENURED : NOT_TENURED); pretenure ? TENURED : NOT_TENURED);
table->Rehash(*new_table, key); table->Rehash(new_table, key);
return new_table; return new_table;
} }

View File

@ -3722,7 +3722,7 @@ class HashTable: public FixedArray {
PretenureFlag pretenure = NOT_TENURED); PretenureFlag pretenure = NOT_TENURED);
// Returns a new HashTable object. // Returns a new HashTable object.
static Handle<Derived> New( MUST_USE_RESULT static Handle<Derived> New(
Isolate* isolate, Isolate* isolate,
int at_least_space_for, int at_least_space_for,
MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY, MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY,
@ -3829,6 +3829,17 @@ class HashTable: public FixedArray {
return (last + number) & (size - 1); return (last + number) & (size - 1);
} }
// Attempt to shrink hash table after removal of key.
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);
private:
// Returns _expected_ if one of entries given by the first _probe_ probes is // Returns _expected_ if one of entries given by the first _probe_ probes is
// equal to _expected_. Otherwise, returns the entry given by the probe // equal to _expected_. Otherwise, returns the entry given by the probe
// number _probe_. // number _probe_.
@ -3837,21 +3848,7 @@ class HashTable: public FixedArray {
void Swap(uint32_t entry1, uint32_t entry2, WriteBarrierMode mode); void Swap(uint32_t entry1, uint32_t entry2, WriteBarrierMode mode);
// Rehashes this hash-table into the new table. // Rehashes this hash-table into the new table.
void Rehash(Derived* new_table, Key key); void Rehash(Handle<Derived> new_table, Key key);
// Attempt to shrink hash table after removal of key.
static Handle<Derived> Shrink(Handle<Derived> table, Key key);
// Ensure enough space for n additional elements.
MUST_USE_RESULT MaybeObject* EnsureCapacity(
int n,
Key key,
PretenureFlag pretenure = NOT_TENURED);
static Handle<Derived> EnsureCapacity(
Handle<Derived> table,
int n,
Key key,
PretenureFlag pretenure = NOT_TENURED);
}; };