Commit dcbd5234 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

[runtime] The return of the StringTable shrinking

This CL fixes a bug where we would accidentally shrink to the same size of
the StringTable causing repeated unecessary allocations.

Bug: v8:5443, chromium:818642
Change-Id: I353b179616d5293f6d7143e7381ae6711343a835
Reviewed-on: https://chromium-review.googlesource.com/1044207Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53012}
parent 1a9034e2
...@@ -16671,18 +16671,17 @@ Handle<Derived> HashTable<Derived, Shape>::Shrink(Handle<Derived> table, ...@@ -16671,18 +16671,17 @@ Handle<Derived> HashTable<Derived, Shape>::Shrink(Handle<Derived> table,
// there is extra room in the dictionary for additions. Don't go lower than // there is extra room in the dictionary for additions. Don't go lower than
// room for {kMinShrinkCapacity} elements. // room for {kMinShrinkCapacity} elements.
int at_least_room_for = nof + additionalCapacity; int at_least_room_for = nof + additionalCapacity;
DCHECK_LE(at_least_room_for, capacity); int new_capacity = ComputeCapacity(at_least_room_for);
if (at_least_room_for < Derived::kMinShrinkCapacity) return table; if (new_capacity < Derived::kMinShrinkCapacity) return table;
if (new_capacity == capacity) return table;
Isolate* isolate = table->GetIsolate(); Isolate* isolate = table->GetIsolate();
const int kMinCapacityForPretenure = 256; const int kMinCapacityForPretenure = 256;
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 =
Handle<Derived> new_table = HashTable::New( HashTable::New(isolate, new_capacity, pretenure ? TENURED : NOT_TENURED,
isolate, USE_CUSTOM_MINIMUM_CAPACITY);
at_least_room_for,
pretenure ? TENURED : NOT_TENURED);
table->Rehash(*new_table); table->Rehash(*new_table);
return new_table; return new_table;
...@@ -17215,10 +17214,9 @@ Handle<StringTable> StringTable::CautiousShrink(Handle<StringTable> table) { ...@@ -17215,10 +17214,9 @@ Handle<StringTable> StringTable::CautiousShrink(Handle<StringTable> table) {
int nof = table->NumberOfElements(); int nof = table->NumberOfElements();
if (capacity <= StringTable::kMinCapacity) return table; if (capacity <= StringTable::kMinCapacity) return table;
if (nof > (capacity / kMaxEmptyFactor)) return table; if (nof > (capacity / kMaxEmptyFactor)) return table;
// Make sure that after shrinking the table is half empty (aka. has capacity // Keep capacity for at least half of the current nof elements.
// for another {nof} elements). int slack_capacity = nof >> 2;
DCHECK_LE(nof * 2, capacity); return Shrink(table, slack_capacity);
return Shrink(table, nof);
} }
namespace { namespace {
......
...@@ -79,7 +79,7 @@ class StringTable : public HashTable<StringTable, StringTableShape> { ...@@ -79,7 +79,7 @@ class StringTable : public HashTable<StringTable, StringTableShape> {
DECL_CAST(StringTable) DECL_CAST(StringTable)
static const int kMaxEmptyFactor = 8; static const int kMaxEmptyFactor = 4;
static const int kMinCapacity = 2048; static const int kMinCapacity = 2048;
static const int kMinShrinkCapacity = kMinCapacity; static const int kMinShrinkCapacity = kMinCapacity;
......
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