Skip to content

Commit 647f3c7

Browse files
ChALkeRBridgeAR
authored andcommitted
deps: V8: cherry-pick 687d865fe251
Original commit message: [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 <[email protected]> Reviewed-by: Dominik Inführ <[email protected]> Reviewed-by: Hannes Payer <[email protected]> Cr-Commit-Position: refs/heads/master@{#65480} PR-URL: #31007 Refs: v8/v8@687d865 Refs: #1671 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 7fe8399 commit 647f3c7

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

deps/v8/src/heap/heap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,14 @@ HeapObject Heap::AlignWithFiller(HeapObject object, int object_size,
27612761

27622762
void* Heap::AllocateExternalBackingStore(
27632763
const std::function<void*(size_t)>& allocate, size_t byte_length) {
2764+
size_t new_space_backing_store_bytes =
2765+
new_space()->ExternalBackingStoreBytes();
2766+
if (new_space_backing_store_bytes >= 2 * kMaxSemiSpaceSize &&
2767+
new_space_backing_store_bytes >= byte_length) {
2768+
// Performing a young generation GC amortizes over the allocated backing
2769+
// store bytes and may free enough external bytes for this allocation.
2770+
CollectGarbage(NEW_SPACE, GarbageCollectionReason::kExternalMemoryPressure);
2771+
}
27642772
// TODO(ulan): Perform GCs proactively based on the byte_length and
27652773
// the current external backing store counters.
27662774
void* result = allocate(byte_length);

deps/v8/src/heap/heap.h

-4
Original file line numberDiff line numberDiff line change
@@ -1793,10 +1793,6 @@ class Heap {
17931793

17941794
void FinalizePartialMap(Map map);
17951795

1796-
// Allocate empty fixed typed array of given type.
1797-
V8_WARN_UNUSED_RESULT AllocationResult
1798-
AllocateEmptyFixedTypedArray(ExternalArrayType array_type);
1799-
18001796
void set_force_oom(bool value) { force_oom_ = value; }
18011797

18021798
// ===========================================================================

deps/v8/src/heap/spaces.h

+16-8
Original file line numberDiff line numberDiff line change
@@ -2804,14 +2804,14 @@ class V8_EXPORT_PRIVATE NewSpace
28042804
void Shrink();
28052805

28062806
// Return the allocated bytes in the active semispace.
2807-
size_t Size() override {
2807+
size_t Size() final {
28082808
DCHECK_GE(top(), to_space_.page_low());
28092809
return to_space_.pages_used() *
28102810
MemoryChunkLayout::AllocatableMemoryInDataPage() +
28112811
static_cast<size_t>(top() - to_space_.page_low());
28122812
}
28132813

2814-
size_t SizeOfObjects() override { return Size(); }
2814+
size_t SizeOfObjects() final { return Size(); }
28152815

28162816
// Return the allocatable capacity of a semispace.
28172817
size_t Capacity() {
@@ -2829,30 +2829,38 @@ class V8_EXPORT_PRIVATE NewSpace
28292829

28302830
// Committed memory for NewSpace is the committed memory of both semi-spaces
28312831
// combined.
2832-
size_t CommittedMemory() override {
2832+
size_t CommittedMemory() final {
28332833
return from_space_.CommittedMemory() + to_space_.CommittedMemory();
28342834
}
28352835

2836-
size_t MaximumCommittedMemory() override {
2836+
size_t MaximumCommittedMemory() final {
28372837
return from_space_.MaximumCommittedMemory() +
28382838
to_space_.MaximumCommittedMemory();
28392839
}
28402840

28412841
// Approximate amount of physical memory committed for this space.
2842-
size_t CommittedPhysicalMemory() override;
2842+
size_t CommittedPhysicalMemory() final;
28432843

28442844
// Return the available bytes without growing.
2845-
size_t Available() override {
2845+
size_t Available() final {
28462846
DCHECK_GE(Capacity(), Size());
28472847
return Capacity() - Size();
28482848
}
28492849

2850-
size_t ExternalBackingStoreBytes(
2851-
ExternalBackingStoreType type) const override {
2850+
size_t ExternalBackingStoreBytes(ExternalBackingStoreType type) const final {
28522851
DCHECK_EQ(0, from_space_.ExternalBackingStoreBytes(type));
28532852
return to_space_.ExternalBackingStoreBytes(type);
28542853
}
28552854

2855+
size_t ExternalBackingStoreBytes() {
2856+
size_t result = 0;
2857+
for (int i = 0; i < ExternalBackingStoreType::kNumTypes; i++) {
2858+
result +=
2859+
ExternalBackingStoreBytes(static_cast<ExternalBackingStoreType>(i));
2860+
}
2861+
return result;
2862+
}
2863+
28562864
size_t AllocatedSinceLastGC() {
28572865
const Address age_mark = to_space_.age_mark();
28582866
DCHECK_NE(age_mark, kNullAddress);

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

+21
Original file line numberDiff line numberDiff line change
@@ -6823,6 +6823,27 @@ TEST(CodeObjectRegistry) {
68236823
CHECK(MemoryChunk::FromAddress(code2_address)->Contains(code2_address));
68246824
}
68256825

6826+
TEST(Regress9701) {
6827+
ManualGCScope manual_gc_scope;
6828+
if (!FLAG_incremental_marking) return;
6829+
CcTest::InitializeVM();
6830+
Heap* heap = CcTest::heap();
6831+
// Start with an empty new space.
6832+
CcTest::CollectGarbage(NEW_SPACE);
6833+
CcTest::CollectGarbage(NEW_SPACE);
6834+
6835+
int mark_sweep_count_before = heap->ms_count();
6836+
// Allocate many short living array buffers.
6837+
for (int i = 0; i < 1000; i++) {
6838+
HandleScope scope(heap->isolate());
6839+
CcTest::i_isolate()->factory()->NewJSArrayBufferAndBackingStore(
6840+
64 * KB, InitializedFlag::kZeroInitialized);
6841+
}
6842+
int mark_sweep_count_after = heap->ms_count();
6843+
// We expect only scavenges, no full GCs.
6844+
CHECK_EQ(mark_sweep_count_before, mark_sweep_count_after);
6845+
}
6846+
68266847
} // namespace heap
68276848
} // namespace internal
68286849
} // namespace v8

0 commit comments

Comments
 (0)