Skip to content

Commit 687d865

Browse files
ulanCommit Bot
authored andcommitted
[heap] Perform GCs on v8::BackingStore allocation
This adds heuristics to perform young and full GCs on allocation of external ArrayBuffer backing stores. Young GCs are performed proactively based on the external backing store bytes for the young generation. Full GCs are performed only if the allocation fails. Subsequent CLs will add heuristics to start incremental full GCs based on the external backing store bytes. This will allow us to remove AdjustAmountOfExternalMemory for ArrayBuffers. Bug: v8:9701, chromium:1008938 Change-Id: I0e8688f582989518926c38260b5cf14e2ca93f84 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1803614 Commit-Queue: Ulan Degenbaev <ulan@chromium.org> Reviewed-by: Dominik Inführ <dinfuehr@chromium.org> Reviewed-by: Hannes Payer <hpayer@chromium.org> Cr-Commit-Position: refs/heads/master@{#65480}
1 parent 7ec8b6b commit 687d865

4 files changed

Lines changed: 45 additions & 12 deletions

File tree

src/heap/heap.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,14 @@ HeapObject Heap::AlignWithFiller(HeapObject object, int object_size,
27872787

27882788
void* Heap::AllocateExternalBackingStore(
27892789
const std::function<void*(size_t)>& allocate, size_t byte_length) {
2790+
size_t new_space_backing_store_bytes =
2791+
new_space()->ExternalBackingStoreBytes();
2792+
if (new_space_backing_store_bytes >= 2 * kMaxSemiSpaceSize &&
2793+
new_space_backing_store_bytes >= byte_length) {
2794+
// Performing a young generation GC amortizes over the allocated backing
2795+
// store bytes and may free enough external bytes for this allocation.
2796+
CollectGarbage(NEW_SPACE, GarbageCollectionReason::kExternalMemoryPressure);
2797+
}
27902798
// TODO(ulan): Perform GCs proactively based on the byte_length and
27912799
// the current external backing store counters.
27922800
void* result = allocate(byte_length);

src/heap/heap.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,10 +1807,6 @@ class Heap {
18071807

18081808
void FinalizePartialMap(Map map);
18091809

1810-
// Allocate empty fixed typed array of given type.
1811-
V8_WARN_UNUSED_RESULT AllocationResult
1812-
AllocateEmptyFixedTypedArray(ExternalArrayType array_type);
1813-
18141810
void set_force_oom(bool value) { force_oom_ = value; }
18151811

18161812
// ===========================================================================

src/heap/spaces.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,14 +2812,14 @@ class V8_EXPORT_PRIVATE NewSpace
28122812
void Shrink();
28132813

28142814
// Return the allocated bytes in the active semispace.
2815-
size_t Size() override {
2815+
size_t Size() final {
28162816
DCHECK_GE(top(), to_space_.page_low());
28172817
return to_space_.pages_used() *
28182818
MemoryChunkLayout::AllocatableMemoryInDataPage() +
28192819
static_cast<size_t>(top() - to_space_.page_low());
28202820
}
28212821

2822-
size_t SizeOfObjects() override { return Size(); }
2822+
size_t SizeOfObjects() final { return Size(); }
28232823

28242824
// Return the allocatable capacity of a semispace.
28252825
size_t Capacity() {
@@ -2837,30 +2837,38 @@ class V8_EXPORT_PRIVATE NewSpace
28372837

28382838
// Committed memory for NewSpace is the committed memory of both semi-spaces
28392839
// combined.
2840-
size_t CommittedMemory() override {
2840+
size_t CommittedMemory() final {
28412841
return from_space_.CommittedMemory() + to_space_.CommittedMemory();
28422842
}
28432843

2844-
size_t MaximumCommittedMemory() override {
2844+
size_t MaximumCommittedMemory() final {
28452845
return from_space_.MaximumCommittedMemory() +
28462846
to_space_.MaximumCommittedMemory();
28472847
}
28482848

28492849
// Approximate amount of physical memory committed for this space.
2850-
size_t CommittedPhysicalMemory() override;
2850+
size_t CommittedPhysicalMemory() final;
28512851

28522852
// Return the available bytes without growing.
2853-
size_t Available() override {
2853+
size_t Available() final {
28542854
DCHECK_GE(Capacity(), Size());
28552855
return Capacity() - Size();
28562856
}
28572857

2858-
size_t ExternalBackingStoreBytes(
2859-
ExternalBackingStoreType type) const override {
2858+
size_t ExternalBackingStoreBytes(ExternalBackingStoreType type) const final {
28602859
DCHECK_EQ(0, from_space_.ExternalBackingStoreBytes(type));
28612860
return to_space_.ExternalBackingStoreBytes(type);
28622861
}
28632862

2863+
size_t ExternalBackingStoreBytes() {
2864+
size_t result = 0;
2865+
for (int i = 0; i < ExternalBackingStoreType::kNumTypes; i++) {
2866+
result +=
2867+
ExternalBackingStoreBytes(static_cast<ExternalBackingStoreType>(i));
2868+
}
2869+
return result;
2870+
}
2871+
28642872
size_t AllocatedSinceLastGC() {
28652873
const Address age_mark = to_space_.age_mark();
28662874
DCHECK_NE(age_mark, kNullAddress);

test/cctest/heap/test-heap.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6872,6 +6872,27 @@ TEST(CodeObjectRegistry) {
68726872
CHECK(MemoryChunk::FromAddress(code2_address)->Contains(code2_address));
68736873
}
68746874

6875+
TEST(Regress9701) {
6876+
ManualGCScope manual_gc_scope;
6877+
if (!FLAG_incremental_marking) return;
6878+
CcTest::InitializeVM();
6879+
Heap* heap = CcTest::heap();
6880+
// Start with an empty new space.
6881+
CcTest::CollectGarbage(NEW_SPACE);
6882+
CcTest::CollectGarbage(NEW_SPACE);
6883+
6884+
int mark_sweep_count_before = heap->ms_count();
6885+
// Allocate many short living array buffers.
6886+
for (int i = 0; i < 1000; i++) {
6887+
HandleScope scope(heap->isolate());
6888+
CcTest::i_isolate()->factory()->NewJSArrayBufferAndBackingStore(
6889+
64 * KB, InitializedFlag::kZeroInitialized);
6890+
}
6891+
int mark_sweep_count_after = heap->ms_count();
6892+
// We expect only scavenges, no full GCs.
6893+
CHECK_EQ(mark_sweep_count_before, mark_sweep_count_after);
6894+
}
6895+
68756896
} // namespace heap
68766897
} // namespace internal
68776898
} // namespace v8

0 commit comments

Comments
 (0)