A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
fix: Copying collector: grow-vs-collect heuristic compares against the w (#15207)
wangshun5619a5dab7b1
diff --git a/crates/wasmtime/src/runtime/store/gc.rs b/crates/wasmtime/src/runtime/store/gc.rsindex ce98743ab7..76e637fdfb 100644--- a/crates/wasmtime/src/runtime/store/gc.rs+++ b/crates/wasmtime/src/runtime/store/gc.rs@@ -290,12 +290,15 @@ impl StoreOpaque {) -> Result<()> {log::trace!("collect_and_maybe_grow_gc_heap(bytes_needed = {bytes_needed:#x?})");self.do_gc(asyncness).await?;- if let Some(n) = bytes_needed- && n > u64::try_from(self.gc_heap_capacity())?.saturating_sub(- self.gc_store.as_ref().map_or(0, |gc| {- u64::try_from(gc.last_post_gc_allocated_bytes.unwrap_or(0)).unwrap()- }),+ let (allocatable_bytes, last_gc_heap_usage) = self.gc_store.as_ref().map_or((0, 0), |gc| {+ (+ gc.gc_heap_allocatable_bytes(),+ gc.last_post_gc_allocated_bytes.unwrap_or(0),)+ });+ if let Some(n) = bytes_needed+ && n > u64::try_from(allocatable_bytes)?+ .saturating_sub(u64::try_from(last_gc_heap_usage).unwrap()){if let Err(e) = self.grow_gc_heap(limiter, n, asyncness).await {if e.is::<GcHeapGrowthFailed>() {@@ -480,15 +483,15 @@ impl StoreOpaque {let mut store = WithoutGcZealAllocCounter::new(self);- let gc_heap_capacity = store+ let allocatable_bytes = store.gc_store.as_ref()- .map_or(0, |gc_store| gc_store.gc_heap_capacity());+ .map_or(0, |gc_store| gc_store.gc_heap_allocatable_bytes());let last_gc_heap_usage = store.gc_store.as_ref().map_or(0, |gc_store| {gc_store.last_post_gc_allocated_bytes.unwrap_or(0)});- if should_collect_first(bytes_needed, gc_heap_capacity, last_gc_heap_usage) {+ if should_collect_first(bytes_needed, allocatable_bytes, last_gc_heap_usage) {log::trace!("Collecting first, then retrying; growing GC heap if collecting didn't \free up enough space, then retrying again"@@ -1145,16 +1148,17 @@ impl<'a> WithoutGcZealAllocCounter<'a> {////// * `bytes_needed`: the number of bytes the mutator wants to allocate///-/// * `gc_heap_capacity`: The current size of the GC heap.+/// * `allocatable_bytes`: The number of bytes that can be allocated into the GC+/// heap before a collection is required.////// * `last_gc_heap_usage`: The precise GC heap usage after the last collection.#[track_caller]fn should_collect_first(bytes_needed: u64,- gc_heap_capacity: usize,+ allocatable_bytes: usize,last_gc_heap_usage: usize,) -> bool {- debug_assert!(last_gc_heap_usage <= gc_heap_capacity);+ debug_assert!(last_gc_heap_usage <= allocatable_bytes);// If we haven't allocated the GC heap yet, there's nothing to collect.//@@ -1162,7 +1166,7 @@ fn should_collect_first(// passes `bytes_needed = 0`. This way our retry-after-gc logic doesn't// auto-fail on its second attempt, which would be bad because it doesn't// necessarily retry more than once.- if gc_heap_capacity == 0 {+ if allocatable_bytes == 0 {return false;}@@ -1194,9 +1198,9 @@ fn should_collect_first(// Common case: to balance collection frequency (and its time overhead) with// GC heap growth (and its space overhead), only prefer growing first if the- // predicted GC heap utilization is greater than half the GC heap's- // capacity.- predicted_usage < gc_heap_capacity / 2+ // predicted GC heap utilization is greater than half of the bytes that can+ // be allocated into the heap before a collection is required.+ predicted_usage < allocatable_bytes / 2}#[cfg(test)]diff --git a/crates/wasmtime/src/runtime/vm/gc.rs b/crates/wasmtime/src/runtime/vm/gc.rsindex 0b21ba1f2c..97af9f7ee4 100644--- a/crates/wasmtime/src/runtime/vm/gc.rs+++ b/crates/wasmtime/src/runtime/vm/gc.rs@@ -135,6 +135,12 @@ impl GcStore {self.gc_heap.heap_slice().len()}+ /// Get the number of bytes that can be allocated into this GC heap before+ /// a collection is required.+ pub fn gc_heap_allocatable_bytes(&self) -> usize {+ self.gc_heap.allocatable_bytes()+ }+/// Asynchronously perform garbage collection within this heap.pub async fn gc(&mut self,diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rsindex 40636a4ac1..04a3dd90b7 100644--- a/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs@@ -944,6 +944,10 @@ unsafe impl GcHeap for CopyingHeap {usize::try_from(self.bump_ptr() - self.active_space_start).unwrap()}+ fn allocatable_bytes(&self) -> usize {+ usize::try_from(self.active_space_end() - self.active_space_start).unwrap()+ }+fn gc<'a, 'b>(&'a mut self,roots: GcRootsIter<'a>,diff --git a/crates/wasmtime/src/runtime/vm/gc/gc_runtime.rs b/crates/wasmtime/src/runtime/vm/gc/gc_runtime.rsindex 57c02a408b..e70404ef7c 100644--- a/crates/wasmtime/src/runtime/vm/gc/gc_runtime.rs+++ b/crates/wasmtime/src/runtime/vm/gc/gc_runtime.rs@@ -365,6 +365,18 @@ pub unsafe trait GcHeap: 'static + Send + Sync {/// This is distinct from the heap capacity.fn allocated_bytes(&self) -> usize;+ /// Get the number of bytes that can be allocated into this heap before a+ /// collection is required.+ ///+ /// This is distinct from the heap capacity: some collectors (e.g. the+ /// copying collector) only use a portion of the heap for allocation at any+ /// given time.+ ///+ /// Defaults to the heap's capacity.+ fn allocatable_bytes(&self) -> usize {+ self.heap_slice().len()+ }+/// Whether a GC should be performed before the next heap growth.////// Some collectors (e.g. the copying collector) need to perform a GC before