Skip to content

Commit 3e8d7a7

Browse files
committed
deps: backport e093a04, 09db540 from upstream V8
Original commit messages: v8/v8@e093a04 Rehash and clear deleted entries in weak collections during GC Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity. BUG=v8:4909 R=hpayer@chromium.org LOG=n Review URL: https://codereview.chromium.org/1877233005 Cr-Commit-Position: refs/heads/master@{nodejs#35514} v8/v8@09db540 Reland of Rehash and clear deleted entries in weak collections during GC BUG=v8:4909 R=hpayer@chromium.org,ulan@chromium.org LOG=n Review URL: https://codereview.chromium.org/1890123002 Cr-Commit-Position: refs/heads/master@{nodejs#35538} V8-Bug: https://crbug.com/v8/4909 Fixes: nodejs#6180
1 parent 92a02d5 commit 3e8d7a7

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

deps/v8/src/heap/mark-compact.cc

+7
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,13 @@ void MarkCompactCollector::ClearWeakCollections() {
24222422
table->RemoveEntry(i);
24232423
}
24242424
}
2425+
// Rehash if more than 25% of the entries are deleted entries.
2426+
// TODO(jochen): Consider to shrink the fixed array in place.
2427+
if ((table->NumberOfDeletedElements() << kJSWeakCollectionLoadFactorExp) >
2428+
table->NumberOfElements()) {
2429+
HandleScope scope(heap()->isolate());
2430+
table->Rehash(heap()->isolate()->factory()->undefined_value());
2431+
}
24252432
}
24262433
weak_collection_obj = weak_collection->next();
24272434
weak_collection->set_next(heap()->undefined_value());

deps/v8/src/heap/mark-compact.h

+4
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ class MarkCompactCollector {
365365
static const uint32_t kSingleFreeEncoding = 0;
366366
static const uint32_t kMultiFreeEncoding = 1;
367367

368+
// If the number of deleted slots in a JSWeakCollection exceeds the number
369+
// of entries / 2^(factor), we rehash the table.
370+
static const int kJSWeakCollectionLoadFactorExp = 1;
371+
368372
static inline bool IsMarked(Object* obj);
369373
static bool IsUnmarkedHeapObjectWithHeap(Heap* heap, Object** p);
370374

deps/v8/src/objects.cc

+26
Original file line numberDiff line numberDiff line change
@@ -17347,6 +17347,16 @@ void HashTable<Derived, Shape, Key>::Rehash(Key key) {
1734717347
}
1734817348
}
1734917349
}
17350+
// Wipe deleted entries.
17351+
Heap* heap = GetHeap();
17352+
Object* the_hole = heap->the_hole_value();
17353+
Object* undefined = heap->undefined_value();
17354+
for (uint32_t current = 0; current < capacity; current++) {
17355+
if (get(EntryToIndex(current)) == the_hole) {
17356+
set(EntryToIndex(current), undefined);
17357+
}
17358+
}
17359+
SetNumberOfDeletedElements(0);
1735017360
}
1735117361

1735217362

@@ -18168,6 +18178,16 @@ void CompilationCacheTable::Age() {
1816818178
}
1816918179
}
1817018180
}
18181+
// Wipe deleted entries.
18182+
Heap* heap = GetHeap();
18183+
Object* the_hole = heap->the_hole_value();
18184+
Object* undefined = heap->undefined_value();
18185+
for (uint32_t current = 0; current < capacity; current++) {
18186+
if (get(EntryToIndex(current)) == the_hole) {
18187+
set(EntryToIndex(current), undefined);
18188+
}
18189+
}
18190+
SetNumberOfDeletedElements(0);
1817118191
}
1817218192

1817318193

@@ -18707,6 +18727,12 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
1870718727
return table;
1870818728
}
1870918729

18730+
// Rehash if more than 25% of the entries are deleted entries.
18731+
// TODO(jochen): Consider to shrink the fixed array in place.
18732+
if ((table->NumberOfDeletedElements() << 1) > table->NumberOfElements()) {
18733+
table->Rehash(isolate->factory()->undefined_value());
18734+
}
18735+
1871018736
// Check whether the hash table should be extended.
1871118737
table = EnsureCapacity(table, 1, key);
1871218738
table->AddEntry(table->FindInsertionEntry(hash), *key, *value);

deps/v8/test/cctest/test-weakmaps.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TEST(Weakness) {
124124
heap->CollectAllGarbage(false);
125125
CHECK_EQ(1, NumberOfWeakCalls);
126126
CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
127-
CHECK_EQ(2,
127+
CHECK_EQ(0,
128128
ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
129129
}
130130

deps/v8/test/cctest/test-weaksets.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ TEST(WeakSet_Weakness) {
123123
heap->CollectAllGarbage(false);
124124
CHECK_EQ(1, NumberOfWeakCalls);
125125
CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements());
126-
CHECK_EQ(
127-
1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
126+
CHECK_EQ(0,
127+
ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
128128
}
129129

130130

0 commit comments

Comments
 (0)