Skip to content

Truly independent web browser

LibGC+LibJS: Count storage cells towards the next collection

Andreas Klingfa7b2fc92840
diff --git a/Libraries/LibGC/CAPI.h b/Libraries/LibGC/CAPI.h
index 9751418488a..6386e705b85 100644
--- a/Libraries/LibGC/CAPI.h
+++ b/Libraries/LibGC/CAPI.h
@@ -144,9 +144,9 @@ GC_API void gc_allocator_destroy(GCAllocator*);
// are allocated while an incremental sweep is in progress have to start out marked.
GC_API GCCell* gc_heap_allocate_cell(GCHeap*, GCAllocator*, bool* must_mark);
// Like gc_heap_allocate_cell(), for cells that other cells keep their storage in instead of malloc memory: it never
-// collects garbage and does not count towards the next collection, so storage can grow while its owner is between
-// states nothing may observe, and callers can hold on to anything across the growth. Live storage counts once a
-// collection finds it, through the live cell bytes that set the next threshold.
+// collects garbage, so storage can grow while its owner is between states nothing may observe, and callers can hold on
+// to anything across the growth. It still counts towards the next collection, which the next allocation that may
+// collect starts.
GC_API GCCell* gc_heap_allocate_storage_cell(GCHeap*, GCAllocator*, bool* must_mark);
// Each allocator takes the free cells of one block at a time into the heap, as a local free list of the cells that were
// freed and a range of cells that were never allocated, so that allocating is usually popping that list or bumping
@@ -164,7 +164,7 @@ GC_API GCCell* gc_heap_allocate_storage_cell(GCHeap*, GCAllocator*, bool* must_m
// - Before taking a cell of N bytes (the cell size of the allocator), such code checks that the heap's
// allocated_bytes_since_last_gc + N stays within its gc_bytes_threshold, and allocates through the heap otherwise
// (which may collect garbage). After taking it, it adds N to allocated_bytes_since_last_gc and total_allocated_bytes;
-// storage cells (see gc_heap_allocate_storage_cell()) skip the check and add N to total_allocated_bytes only. These
+// storage cells (see gc_heap_allocate_storage_cell()) skip the check but add N to both as well. These
// are size_t fields of the heap, at the offsets gc_get_layout() reports.
// - It then writes every byte of the cell, as after gc_heap_allocate_cell(), with a clear mark.
// Returns where the head of the allocator's list in the heap is, which stays valid as long as the heap. The list and
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index df4e05dd52f..de145152a31 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -419,16 +419,14 @@ void Heap::will_allocate(size_t size, TriggersCollection triggers_collection)
{
ASSERT(!heap_access_is_forbidden_on_this_thread());
ASSERT(is_on_owner_thread());
- if (triggers_collection == TriggersCollection::No) {
- m_total_allocated_bytes += size;
- return;
- }
-
// NB: Sweeping a block destroys its dead cells, and a destructor may allocate. A collection started from there
// would mark the block's reachable cells and queue the block for the next sweep, the rest of the current sweep
// would then clear those marks, and the next sweep of the block would free cells that are still in use. So an
// allocation made while a block is swept leaves the collection to the first allocation after the sweep.
- if (m_block_sweeps_in_progress == 0) {
+ // NB: An allocation that must not collect still counts towards the next collection, which the next allocation that
+ // may collect starts. Otherwise a program that allocates mostly storage (as one making large arrays does) would
+ // keep gigabytes of dead storage before its few other allocations reached the threshold.
+ if (m_block_sweeps_in_progress == 0 && triggers_collection == TriggersCollection::Yes) {
if (should_collect_on_every_allocation()) {
m_allocated_bytes_since_last_gc = 0;
collect_garbage();
diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.h
index 2f04eeb6e3a..53db42679a4 100644
--- a/Libraries/LibGC/Heap.h
+++ b/Libraries/LibGC/Heap.h
@@ -221,8 +221,8 @@ private:
}
// Shared by allocate<T>() and any allocation whose cell type is only known through its allocator descriptor.
- // Cells that other cells keep their storage in neither collect garbage nor count towards the next collection
- // (see gc_heap_allocate_storage_cell()).
+ // Cells that other cells keep their storage in never collect garbage, but count towards the next collection (see
+ // gc_heap_allocate_storage_cell()).
enum class TriggersCollection {
No,
Yes,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
index 8b5071a3c35..fbe21954201 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
@@ -471,10 +471,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
let scratch = self.pinned.scratch;
let at = |base: Gpr, offset: u32| Address::new(base, offset as i32);
- // Only the array cell counts towards the next collection.
- self.emit_check_heap_threshold(info.cell_size, array, temp, entry);
// NB: Both allocators must have a free cell before either gives one.
let storage_size_class = storage_size_class.filter(|_| count != 0);
+ let storage_cell_size = storage_size_class.map_or(0, |size_class| size_class.cell_size);
+ self.emit_check_heap_threshold(info.cell_size + storage_cell_size, array, temp, entry);
if let Some(size_class) = storage_size_class {
self.masm.move_imm64(scratch, size_class.allocator);
self.emit_branch_if_no_local_free_cell(scratch, temp, entry);
@@ -483,8 +483,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
if let Some(size_class) = storage_size_class {
self.emit_pop_free_list(size_class.allocator, storage, temp, entry);
}
- let storage_cell_size = storage_size_class.map_or(0, |size_class| size_class.cell_size);
- self.emit_count_allocation(info.cell_size, info.cell_size + storage_cell_size, temp, false);
+ self.emit_count_allocation(
+ info.cell_size + storage_cell_size,
+ info.cell_size + storage_cell_size,
+ temp,
+ false,
+ );
// The array, with its packed elements if it has any.
let mut image = info
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs b/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs
index 533c3ad7c1d..07ea72c98e8 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs
@@ -215,16 +215,21 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
// The cell, off the size class's local free list.
self.emit_pop_free_list_of(scratch, cell, storage, fail);
- // NB: Storage cells only count towards the heap's total.
+ // NB: Storage cells count towards the next collection without checking for it, since storage never collects.
self.masm.move32(object, capacity);
self.masm.shl64_imm(object, object, 3);
self.masm
.add64_imm(object, object, i64::from(info.storage_values_offset));
self.masm.move_imm64(scratch, objects.heap);
- let total = at(scratch, objects.heap_total_allocated_bytes_offset);
- self.masm.load64(storage, &total);
- self.masm.add64(storage, storage, object);
- self.masm.store64(&total, storage);
+ for offset in [
+ objects.heap_allocated_bytes_offset,
+ objects.heap_total_allocated_bytes_offset,
+ ] {
+ let counter = at(scratch, offset);
+ self.masm.load64(storage, &counter);
+ self.masm.add64(storage, storage, object);
+ self.masm.store64(&counter, storage);
+ }
// The header, then the values: undefined beyond the old ones.
for (index, word) in info.storage_template.iter().enumerate() {
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 21628f0f6bb..993f6e1c7ae 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1392,10 +1392,9 @@ pub struct RopeAllocationInfo {
/// `cell_size`) and, unless `count` is 0, a storage cell of the first
/// storage size class with a `capacity` of at least `count`. JIT code only
/// allocates both itself if both local free lists have a cell: it checks
-/// the heap's threshold for the array cell (storage cells never start a
-/// collection, nor count towards one), pops both cells, adds both cell
-/// sizes to the heap's total allocated bytes and the array's to its
-/// allocated bytes since the last collection. Then:
+/// the heap's threshold for both cells, pops both cells, and adds both cell
+/// sizes to the heap's total allocated bytes and to its allocated bytes
+/// since the last collection. Then:
/// 1. Copy `template` to the array cell and store
/// `array + inline_storage_offset` at `array + named_properties_offset`
/// (see `ObjectAllocationInfo`).
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index 4992cace5f7..0881491b7a0 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -302,9 +302,9 @@ impl Heap {
}
/// allocate_with_size_class() for a cell that another cell keeps its storage in, instead of malloc memory: it never
- /// collects garbage and does not count towards the next collection, so storage can grow while its owner is between
- /// states nothing may observe, and callers can hold on to anything across the growth. Live storage counts once a
- /// collection finds it, through the live cell bytes that set the next threshold.
+ /// collects garbage, so storage can grow while its owner is between states nothing may observe, and callers can hold
+ /// on to anything across the growth. It still counts towards the next collection, which the next allocation that
+ /// may collect starts.
pub fn allocate_storage_with_size_class<T: GcCell, E: Copy>(
&self,
size_class: SizeClassAllocator,
@@ -486,13 +486,11 @@ impl Heap {
cursor_cell
};
let cell = NonNull::new_unchecked(cell);
- if let AllocationKind::Cell = kind {
- let allocated_bytes = info.allocated_bytes_since_last_gc.read() + cell_size;
- if allocated_bytes > info.gc_bytes_threshold.read() {
- return None;
- }
- info.allocated_bytes_since_last_gc.write(allocated_bytes);
+ let allocated_bytes = info.allocated_bytes_since_last_gc.read() + cell_size;
+ if matches!(kind, AllocationKind::Cell) && allocated_bytes > info.gc_bytes_threshold.read() {
+ return None;
}
+ info.allocated_bytes_since_last_gc.write(allocated_bytes);
info.total_allocated_bytes
.write(info.total_allocated_bytes.read() + cell_size);
if from_list {