Skip to content

Commit af844e1

Browse files
committed
Review feedback.
1 parent db77a39 commit af844e1

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

crates/wasmtime/src/runtime/store/gc.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use super::*;
44
use crate::runtime::vm::VMGcRef;
55

66
impl StoreOpaque {
7-
/// Attempt to grow the GC heap by `bytes_needed` or, if that fails, perform
8-
/// a garbage collection.
7+
/// Perform any growth or GC needed to allocate `bytes_needed` bytes.
98
///
109
/// Note that even when this function returns it is not guaranteed
1110
/// that a GC allocation of size `bytes_needed` will succeed. Growing the GC
@@ -28,7 +27,7 @@ impl StoreOpaque {
2827
let root = root.map(|r| scope.gc_roots_mut().push_lifo_root(store_id, r));
2928

3029
scope
31-
.grow_or_collect_gc_heap(limiter, bytes_needed, asyncness)
30+
.collect_and_maybe_grow_gc_heap(limiter, bytes_needed, asyncness)
3231
.await;
3332

3433
root.map(|r| {
@@ -50,16 +49,26 @@ impl StoreOpaque {
5049
}
5150
}
5251

53-
async fn grow_or_collect_gc_heap(
52+
/// Helper invoked as part of `gc`, whose purpose is to GC and
53+
/// maybe grow for a pending allocation of a given size.
54+
async fn collect_and_maybe_grow_gc_heap(
5455
&mut self,
5556
limiter: Option<&mut StoreResourceLimiter<'_>>,
5657
bytes_needed: Option<u64>,
5758
asyncness: Asyncness,
5859
) {
59-
// When explicitly called (e.g., from Store::gc), always collect.
60-
// If bytes_needed is specified, also try to grow if needed.
60+
// First, always collect. Then, if bytes_needed is specified,
61+
// also try to grow if that size is greater than GC heap
62+
// capacity minus sum of allocated layout sizes.
6163
self.do_gc(asyncness).await;
62-
if let Some(n) = bytes_needed {
64+
if let Some(n) = bytes_needed
65+
&& n > u64::try_from(self.gc_heap_capacity())
66+
.unwrap()
67+
.checked_sub(self.gc_store.as_ref().map_or(0, |gc| {
68+
u64::try_from(gc.last_post_gc_allocated_bytes.unwrap_or(0)).unwrap()
69+
}))
70+
.expect("allocated bytes cannot be greater than capacity")
71+
{
6372
let _ = self.grow_gc_heap(limiter, n).await;
6473
}
6574
}

crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ impl DrcHeap {
180180
fn dealloc(&mut self, gc_ref: VMGcRef) {
181181
let drc_ref = drc_ref(&gc_ref);
182182
let size = self.index(drc_ref).object_size();
183-
self.allocated_bytes -= size;
184183
let layout = FreeList::layout(size);
184+
self.allocated_bytes -= layout.size();
185185
self.free_list
186186
.as_mut()
187187
.unwrap()

crates/wasmtime/src/runtime/vm/gc/gc_runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ pub unsafe trait GcHeap: 'static + Send + Sync {
342342
////////////////////////////////////////////////////////////////////////////
343343
// Garbage Collection Methods
344344

345-
/// Get the total number of bytes currently allocated (live) in this heap.
345+
/// Get the total number of bytes currently allocated (live or
346+
/// dead-but-not-collected) in this heap.
346347
///
347-
/// This is the sum of all object sizes that have been allocated but not yet
348-
/// freed. This is distinct from the heap capacity.
348+
/// This is distinct from the heap capacity.
349349
fn allocated_bytes(&self) -> usize;
350350

351351
/// Start a new garbage collection process.

0 commit comments

Comments
 (0)