From 2cb1d07e0f6d9456438016bab7db4688ab354fd2 Mon Sep 17 00:00:00 2001 From: Choongwoo Han Date: Thu, 6 Mar 2025 07:02:23 -0800 Subject: [PATCH] deps: V8: cherry-pick c172ffc5bf54 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: Compact retained maps array more often When we add maps to the retained maps array, we compacted the array if it's full. But, since we are now adding maps in a batch, it's unlikely to meet the condition. Thus, update the condition to check whether new size exceeds the capacity. Bug: 398528460 Change-Id: I89caa47b69532c6397596edfe5caf7c7d24768cc Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6330019 Reviewed-by: Michael Lippautz Commit-Queue: Choongwoo Han Cr-Commit-Position: refs/heads/main@{#99163} Refs: https://github.com/v8/v8/commit/c172ffc5bf54e704558103973e1d5cb7aec491cb PR-URL: https://github.com/nodejs/node/pull/57437 Fixes: https://github.com/nodejs/node/issues/57412 Reviewed-By: Luigi Pinca Reviewed-By: Michaƫl Zasso Reviewed-By: Richard Lau Co-Authored-By: tunamagur0 <47546832+tunamagur0@users.noreply.github.com> --- common.gypi | 2 +- deps/v8/src/heap/heap.cc | 13 +++++++------ deps/v8/src/heap/heap.h | 2 ++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/common.gypi b/common.gypi index 43cbd973fdf..5e39ad3f096 100644 --- a/common.gypi +++ b/common.gypi @@ -38,7 +38,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.11', + 'v8_embedder_string': '-node.12', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/heap/heap.cc b/deps/v8/src/heap/heap.cc index 87e52b1e432..aa152e5d729 100644 --- a/deps/v8/src/heap/heap.cc +++ b/deps/v8/src/heap/heap.cc @@ -6356,12 +6356,13 @@ void Heap::AddRetainedMaps(DirectHandle context, GlobalHandleVector maps) { Handle array(Cast(context->retained_maps()), isolate()); - if (array->IsFull()) { + int new_maps_size = static_cast(maps.size()) * kRetainMapEntrySize; + if (array->length() + new_maps_size > array->capacity()) { CompactRetainedMaps(*array); } int cur_length = array->length(); - array = WeakArrayList::EnsureSpace( - isolate(), array, cur_length + static_cast(maps.size()) * 2); + array = + WeakArrayList::EnsureSpace(isolate(), array, cur_length + new_maps_size); if (*array != context->retained_maps()) { context->set_retained_maps(*array); } @@ -6379,7 +6380,7 @@ void Heap::AddRetainedMaps(DirectHandle context, raw_array->Set(cur_length, MakeWeak(*map)); raw_array->Set(cur_length + 1, Smi::FromInt(v8_flags.retain_maps_for_n_gc)); - cur_length += 2; + cur_length += kRetainMapEntrySize; raw_array->set_length(cur_length); map->set_is_in_retained_map_list(true); @@ -6391,7 +6392,7 @@ void Heap::CompactRetainedMaps(Tagged retained_maps) { int length = retained_maps->length(); int new_length = 0; // This loop compacts the array by removing cleared weak cells. - for (int i = 0; i < length; i += 2) { + for (int i = 0; i < length; i += kRetainMapEntrySize) { Tagged maybe_object = retained_maps->Get(i); if (maybe_object.IsCleared()) { continue; @@ -6405,7 +6406,7 @@ void Heap::CompactRetainedMaps(Tagged retained_maps) { retained_maps->Set(new_length, maybe_object); retained_maps->Set(new_length + 1, age); } - new_length += 2; + new_length += kRetainMapEntrySize; } Tagged undefined = ReadOnlyRoots(this).undefined_value(); for (int i = new_length; i < length; i++) { diff --git a/deps/v8/src/heap/heap.h b/deps/v8/src/heap/heap.h index 7755905b41f..960a21a627d 100644 --- a/deps/v8/src/heap/heap.h +++ b/deps/v8/src/heap/heap.h @@ -1812,6 +1812,8 @@ class Heap final { void AddToRingBuffer(const char* string); void GetFromRingBuffer(char* buffer); + static constexpr int kRetainMapEntrySize = 2; + void CompactRetainedMaps(Tagged retained_maps); void CollectGarbageOnMemoryPressure();