From 3e8d7a73aa5d17f070ab17741e48249ea5a9853c Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 15 Apr 2016 09:01:31 -0700 Subject: [PATCH 1/2] deps: backport e093a04, 09db540 from upstream V8 Original commit messages: https://github.com/v8/v8/commit/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@{#35514} https://github.com/v8/v8/commit/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@{#35538} V8-Bug: https://crbug.com/v8/4909 Fixes: https://github.com/nodejs/node/issues/6180 --- deps/v8/src/heap/mark-compact.cc | 7 +++++++ deps/v8/src/heap/mark-compact.h | 4 ++++ deps/v8/src/objects.cc | 26 ++++++++++++++++++++++++++ deps/v8/test/cctest/test-weakmaps.cc | 2 +- deps/v8/test/cctest/test-weaksets.cc | 4 ++-- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/deps/v8/src/heap/mark-compact.cc b/deps/v8/src/heap/mark-compact.cc index 641ac7d1dc96bc..8f214ac1150fef 100644 --- a/deps/v8/src/heap/mark-compact.cc +++ b/deps/v8/src/heap/mark-compact.cc @@ -2422,6 +2422,13 @@ void MarkCompactCollector::ClearWeakCollections() { table->RemoveEntry(i); } } + // Rehash if more than 25% of the entries are deleted entries. + // TODO(jochen): Consider to shrink the fixed array in place. + if ((table->NumberOfDeletedElements() << kJSWeakCollectionLoadFactorExp) > + table->NumberOfElements()) { + HandleScope scope(heap()->isolate()); + table->Rehash(heap()->isolate()->factory()->undefined_value()); + } } weak_collection_obj = weak_collection->next(); weak_collection->set_next(heap()->undefined_value()); diff --git a/deps/v8/src/heap/mark-compact.h b/deps/v8/src/heap/mark-compact.h index cc5449f97765e7..296c7142b9cd09 100644 --- a/deps/v8/src/heap/mark-compact.h +++ b/deps/v8/src/heap/mark-compact.h @@ -365,6 +365,10 @@ class MarkCompactCollector { static const uint32_t kSingleFreeEncoding = 0; static const uint32_t kMultiFreeEncoding = 1; + // If the number of deleted slots in a JSWeakCollection exceeds the number + // of entries / 2^(factor), we rehash the table. + static const int kJSWeakCollectionLoadFactorExp = 1; + static inline bool IsMarked(Object* obj); static bool IsUnmarkedHeapObjectWithHeap(Heap* heap, Object** p); diff --git a/deps/v8/src/objects.cc b/deps/v8/src/objects.cc index 67a59638318844..7b1dfdc995301f 100644 --- a/deps/v8/src/objects.cc +++ b/deps/v8/src/objects.cc @@ -17347,6 +17347,16 @@ void HashTable::Rehash(Key key) { } } } + // Wipe deleted entries. + Heap* heap = GetHeap(); + Object* the_hole = heap->the_hole_value(); + Object* undefined = heap->undefined_value(); + for (uint32_t current = 0; current < capacity; current++) { + if (get(EntryToIndex(current)) == the_hole) { + set(EntryToIndex(current), undefined); + } + } + SetNumberOfDeletedElements(0); } @@ -18168,6 +18178,16 @@ void CompilationCacheTable::Age() { } } } + // Wipe deleted entries. + Heap* heap = GetHeap(); + Object* the_hole = heap->the_hole_value(); + Object* undefined = heap->undefined_value(); + for (uint32_t current = 0; current < capacity; current++) { + if (get(EntryToIndex(current)) == the_hole) { + set(EntryToIndex(current), undefined); + } + } + SetNumberOfDeletedElements(0); } @@ -18707,6 +18727,12 @@ Handle ObjectHashTable::Put(Handle table, return table; } + // Rehash if more than 25% of the entries are deleted entries. + // TODO(jochen): Consider to shrink the fixed array in place. + if ((table->NumberOfDeletedElements() << 1) > table->NumberOfElements()) { + table->Rehash(isolate->factory()->undefined_value()); + } + // Check whether the hash table should be extended. table = EnsureCapacity(table, 1, key); table->AddEntry(table->FindInsertionEntry(hash), *key, *value); diff --git a/deps/v8/test/cctest/test-weakmaps.cc b/deps/v8/test/cctest/test-weakmaps.cc index 781ad1f69f8877..c16bdf5094350a 100644 --- a/deps/v8/test/cctest/test-weakmaps.cc +++ b/deps/v8/test/cctest/test-weakmaps.cc @@ -124,7 +124,7 @@ TEST(Weakness) { heap->CollectAllGarbage(false); CHECK_EQ(1, NumberOfWeakCalls); CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); - CHECK_EQ(2, + CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); } diff --git a/deps/v8/test/cctest/test-weaksets.cc b/deps/v8/test/cctest/test-weaksets.cc index 643bb48ab1aa49..0de63b9228fe52 100644 --- a/deps/v8/test/cctest/test-weaksets.cc +++ b/deps/v8/test/cctest/test-weaksets.cc @@ -123,8 +123,8 @@ TEST(WeakSet_Weakness) { heap->CollectAllGarbage(false); CHECK_EQ(1, NumberOfWeakCalls); CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements()); - CHECK_EQ( - 1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); + CHECK_EQ(0, + ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); } From 5b72b4711bd7a4f11d58244a08d7b3cdd88eb054 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 15 Apr 2016 09:13:49 -0700 Subject: [PATCH 2/2] [SQUASH] fixup needed for backport --- deps/v8/src/objects.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/deps/v8/src/objects.cc b/deps/v8/src/objects.cc index 7b1dfdc995301f..4fd51e97ee5d35 100644 --- a/deps/v8/src/objects.cc +++ b/deps/v8/src/objects.cc @@ -18155,6 +18155,7 @@ Handle CompilationCacheTable::PutRegExp( void CompilationCacheTable::Age() { DisallowHeapAllocation no_allocation; Object* the_hole_value = GetHeap()->the_hole_value(); + uint32_t capacity = Capacity(); for (int entry = 0, size = Capacity(); entry < size; entry++) { int entry_index = EntryToIndex(entry); int value_index = entry_index + 1;