Truly independent web browser
LibGC+LibJS: Defer collections from Rust without a call into LibGC
Shannon Booth43eacff4942f
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cppindex e4d17c64473..ba43f9d4fe1 100644--- a/Libraries/LibGC/CAPI.cpp+++ b/Libraries/LibGC/CAPI.cpp@@ -159,6 +159,11 @@ struct CAPI {static void* foreign_context(Heap& heap) { return heap.m_foreign_context; }static void defer_gc(Heap& heap) { heap.defer_gc(); }static void undefer_gc(Heap& heap) { heap.undefer_gc(); }+ static void deferral_state(Heap& heap, size_t*& deferrals, bool*& collect_when_deferrals_end)+ {+ deferrals = &heap.m_gc_deferrals;+ collect_when_deferrals_end = &heap.m_should_gc_when_deferral_ends;+ }static StackInfo const& stack_info(Heap const& heap) { return heap.m_stack_info; }};@@ -259,6 +264,11 @@ void gc_heap_undefer_gc(GCHeap* heap)CAPI::undefer_gc(as_heap(heap));}+void gc_heap_deferral_state(GCHeap* heap, size_t** deferrals, bool** collect_when_deferrals_end)+{+ CAPI::deferral_state(as_heap(heap), *deferrals, *collect_when_deferrals_end);+}+bool gc_heap_should_collect_on_every_allocation(GCHeap const* heap){return as_heap(heap).should_collect_on_every_allocation();diff --git a/Libraries/LibGC/CAPI.h b/Libraries/LibGC/CAPI.hindex 4e598750a6d..52ba07d85f2 100644--- a/Libraries/LibGC/CAPI.h+++ b/Libraries/LibGC/CAPI.h@@ -113,6 +113,7 @@ GC_API void gc_heap_collect_garbage(GCHeap*, int collection_type, bool print_repGC_API bool gc_heap_is_collecting_everything(GCHeap const*);GC_API void gc_heap_defer_gc(GCHeap*);GC_API void gc_heap_undefer_gc(GCHeap*);+GC_API void gc_heap_deferral_state(GCHeap*, size_t** deferrals, bool** collect_when_deferrals_end);GC_API bool gc_heap_should_collect_on_every_allocation(GCHeap const*);GC_API void gc_heap_set_should_collect_on_every_allocation(GCHeap*, bool);GC_API void gc_heap_set_incremental_sweep_enabled(GCHeap*, bool);diff --git a/Libraries/LibJS/Rust/src/gc/capi.rs b/Libraries/LibJS/Rust/src/gc/capi.rsindex fdf34810e00..230884f2105 100644--- a/Libraries/LibJS/Rust/src/gc/capi.rs+++ b/Libraries/LibJS/Rust/src/gc/capi.rs@@ -99,8 +99,12 @@ unsafe extern "C" {// NB: The functions that take a reference to the heap have no other preconditions.pub safe fn gc_heap_collect_garbage(heap: &GCHeap, collection_type: i32, print_report: bool);pub safe fn gc_heap_is_collecting_everything(heap: &GCHeap) -> bool;- pub safe fn gc_heap_defer_gc(heap: &GCHeap);pub safe fn gc_heap_undefer_gc(heap: &GCHeap);+ pub safe fn gc_heap_deferral_state(+ heap: &GCHeap,+ deferrals: &mut *mut usize,+ collect_when_deferrals_end: &mut *mut bool,+ );pub safe fn gc_heap_should_collect_on_every_allocation(heap: &GCHeap) -> bool;pub safe fn gc_heap_set_should_collect_on_every_allocation(heap: &GCHeap, should_collect: bool);pub safe fn gc_heap_set_incremental_sweep_enabled(heap: &GCHeap, enabled: bool);diff --git a/Libraries/LibJS/Rust/src/gc/defer_gc.rs b/Libraries/LibJS/Rust/src/gc/defer_gc.rsindex df8dc864f6e..dbe7486bf99 100644--- a/Libraries/LibJS/Rust/src/gc/defer_gc.rs+++ b/Libraries/LibJS/Rust/src/gc/defer_gc.rs@@ -7,7 +7,6 @@use core::marker::PhantomData;use core::ops::{Deref, DerefMut};-use super::capi;use super::heap::Heap;use crate::interpreter::vm::Vm;@@ -20,7 +19,7 @@ impl<'heap> DeferGc<'heap> {}pub(super) fn of_heap(heap: &'heap Heap) -> Self {- capi::gc_heap_defer_gc(heap.handle());+ heap.defer_gc();Self(heap)}@@ -35,7 +34,7 @@ impl<'heap> DeferGc<'heap> {impl Drop for DeferGc<'_> {fn drop(&mut self) {- capi::gc_heap_undefer_gc(self.0.handle());+ self.0.undefer_gc();}}diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rsindex 9c98d396d95..d5b4a74e54d 100644--- a/Libraries/LibJS/Rust/src/gc/heap.rs+++ b/Libraries/LibJS/Rust/src/gc/heap.rs@@ -31,6 +31,9 @@ pub struct Heap {/// The size classes of each class, by class id, out of line, as few classes have any.size_class_tables: Box<[OnceCell<SizeClassTable>; CLASS_COUNT]>,inline_allocation_info: InlineAllocationInfo,+ /// LibGC's count of deferrals, and whether a collection waits for it to reach zero.+ deferrals: NonNull<usize>,+ collect_when_deferrals_end: NonNull<bool>,}/// The size classes of a class, with the type infos their blocks dispatch through, which have to outlive the@@ -158,6 +161,9 @@ impl Heap {NonNull::new(raw.as_ptr().wrapping_byte_add(offset as usize).cast::<usize>()).expect("the fields of LibGC's heap are not at null")};+ let (mut deferrals, mut collect_when_deferrals_end) = (core::ptr::null_mut(), core::ptr::null_mut());+ // SAFETY: LibGC just created the heap.+ capi::gc_heap_deferral_state(unsafe { raw.as_ref() }, &mut deferrals, &mut collect_when_deferrals_end);Self {raw,allocators: [const { Cell::new(None) }; CLASS_COUNT],@@ -173,6 +179,9 @@ impl Heap {local_end_offset: layout.local_end_offset as usize,local_cell_size_offset: layout.local_cell_size_offset as usize,},+ deferrals: NonNull::new(deferrals).expect("LibGC counts deferrals"),+ collect_when_deferrals_end: NonNull::new(collect_when_deferrals_end)+ .expect("LibGC tracks waiting collections"),}}@@ -195,6 +204,26 @@ impl Heap {unsafe { self.raw.as_ref() }}+ /// GC::Heap::defer_gc(), without a call into LibGC.+ pub(super) fn defer_gc(&self) {+ // SAFETY: The count is a field of the live heap, which only this thread uses.+ unsafe { *self.deferrals.as_ptr() += 1 };+ }++ /// GC::Heap::undefer_gc(), without a call into LibGC unless a collection waits for the last deferral to end.+ pub(super) fn undefer_gc(&self) {+ // SAFETY: The count and the flag are fields of the live heap, which only this thread uses.+ unsafe {+ let deferrals = self.deferrals.as_ptr();+ debug_assert!(*deferrals > 0);+ if *deferrals == 1 && *self.collect_when_deferrals_end.as_ptr() {+ capi::gc_heap_undefer_gc(self.handle());+ } else {+ *deferrals -= 1;+ }+ }+ }+/// Moves `cell` into the heap. Nothing can collect garbage between taking the storage and writing the cell, so/// the collector never sees a partially written cell.pub fn allocate<T: GcCell>(&self, cell: T) -> Gc<T> {