Skip to content

Truly independent web browser

AK+LibJS: Remove the scratch heap of JIT compiles

Andreas Kling93336816355e
diff --git a/AK/FFIHelpers.cpp b/AK/FFIHelpers.cpp
index e79113efa86..34818e4d722 100644
--- a/AK/FFIHelpers.cpp
+++ b/AK/FFIHelpers.cpp
@@ -206,16 +206,6 @@ extern "C" void ladybird_release_unused_allocator_memory()
ak_kmalloc_collect();
}
-extern "C" void* ladybird_enter_scratch_heap()
-{
- return ak_enter_scratch_heap();
-}
-
-extern "C" void ladybird_leave_scratch_heap(void* previous_heap)
-{
- ak_leave_scratch_heap(previous_heap);
-}
-
extern "C" void* ladybird_realloc(void* pointer, size_t old_size, size_t new_size, size_t alignment)
{
if (alignment <= alignof(max_align_t))
diff --git a/AK/FFIHelpers.h b/AK/FFIHelpers.h
index ea81fddfa3a..aa4b0698139 100644
--- a/AK/FFIHelpers.h
+++ b/AK/FFIHelpers.h
@@ -30,10 +30,6 @@ void ladybird_dealloc(void*, size_t alignment);
// Gives the memory the calling thread freed back to the system (see ak_kmalloc_collect()).
void ladybird_release_unused_allocator_memory();
-// See ak_enter_scratch_heap().
-void* ladybird_enter_scratch_heap();
-void ladybird_leave_scratch_heap(void* previous_heap);
-
// For a thread that frees all of its memory when it ends: while one runs, what the allocator purges goes back to the
// system right away.
void ladybird_did_start_short_lived_thread();
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index b86ac1ee24a..4d3c1ae487c 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -118,8 +118,6 @@ unsafe extern "C" {
) -> u8;
fn ladybird_encode_base64_to_utf16(data: *const u8, length: usize, url: bool, omit_padding: bool) -> usize;
fn ladybird_release_unused_allocator_memory();
- fn ladybird_enter_scratch_heap() -> *mut core::ffi::c_void;
- fn ladybird_leave_scratch_heap(previous_heap: *mut core::ffi::c_void);
fn ladybird_convert_to_decimal_exponential_form(
value: f64,
sign: *mut bool,
@@ -564,30 +562,6 @@ pub fn populate_room_for_write<T>(vector: &mut Vec<T>, additional: usize) {
let _ = (vector, additional);
}
-/// While this lives, what the calling thread allocates goes to a heap of its own. Work that frees
-/// nearly everything it allocates uses it to keep its temporary data out of the pages of what
-/// outlives it. Blocks stay valid after it ends. Scopes end in the reverse order they were entered in.
-/// It holds a pointer so that it stays on its thread.
-pub struct ScratchHeapScope {
- previous_heap: *mut core::ffi::c_void,
-}
-
-impl ScratchHeapScope {
- #[must_use = "the scratch heap is only in use while the scope lives"]
- pub fn enter() -> Self {
- // SAFETY: Takes no arguments, and any thread may call it.
- let previous_heap = unsafe { ladybird_enter_scratch_heap() };
- Self { previous_heap }
- }
-}
-
-impl Drop for ScratchHeapScope {
- fn drop(&mut self) {
- // SAFETY: The heap is the one this thread entered the scratch heap from.
- unsafe { ladybird_leave_scratch_heap(self.previous_heap) }
- }
-}
-
pub fn validate_utf16(string: &[u16]) -> bool {
// SAFETY: The slice remains alive for the duration of the call.
unsafe { ladybird_utf16_validate(string.as_ptr(), string.len()) }
diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cpp
index 759ea183bcb..7e8ec472ff2 100644
--- a/AK/kmalloc.cpp
+++ b/AK/kmalloc.cpp
@@ -89,15 +89,6 @@ void ak_kmalloc_collect()
{
}
-void* ak_enter_scratch_heap()
-{
- return nullptr;
-}
-
-void ak_leave_scratch_heap(void*)
-{
-}
-
extern "C" void ladybird_did_start_short_lived_thread()
{
}
@@ -137,7 +128,6 @@ void* ak_kmalloc(size_t size)
// its memory. When the thread exits, mimalloc deletes its heaps, and what is still allocated stays valid until freed.
static constexpr size_t PARTITION_COUNT = to_underlying(HeapPartition::String) + 1;
AK_TLS_INITIAL_EXEC static thread_local constinit mi_heap_t* s_partition_heaps[PARTITION_COUNT] {};
-static thread_local mi_heap_t* s_scratch_heap = nullptr;
static mi_heap_t* heap_for_partition(HeapPartition partition)
{
@@ -186,35 +176,15 @@ void ak_kfree(void* ptr)
void ak_kmalloc_collect()
{
- // NB: In a scratch heap, mi_collect() would collect that heap again instead of the thread's own.
- VERIFY(!s_scratch_heap || mi_heap_get_default() != s_scratch_heap);
-
// mi_collect() only visits the calling thread's default heap, so its partitions have to be collected separately.
for (auto* heap : s_partition_heaps) {
if (heap)
mi_heap_collect(heap, true);
}
- if (s_scratch_heap)
- mi_heap_collect(s_scratch_heap, true);
mi_collect(true);
}
-void* ak_enter_scratch_heap()
-{
- if (!s_scratch_heap)
- s_scratch_heap = mi_heap_new();
- // NB: Without a heap of its own, the work allocates from the thread's heap as usual.
- if (!s_scratch_heap)
- return mi_heap_get_default();
- return mi_heap_set_default(s_scratch_heap);
-}
-
-void ak_leave_scratch_heap(void* previous_heap)
-{
- mi_heap_set_default(static_cast<mi_heap_t*>(previous_heap));
-}
-
// MADV_FREE leaves the pages in this process until the system runs short of memory. That pays off when they get reused
// soon, which is not what happens to the memory of a thread that ended. mimalloc reads this option each time it purges.
// NB: A thread that starts while another ends can leave the wrong value behind, until the next one of them. All that
diff --git a/AK/kmalloc.h b/AK/kmalloc.h
index da02a54142c..9517b892c27 100644
--- a/AK/kmalloc.h
+++ b/AK/kmalloc.h
@@ -32,13 +32,6 @@ void ak_kfree(void* ptr);
[[nodiscard]] size_t ak_kmalloc_usable_size(void const* ptr);
void ak_kmalloc_collect();
-// Sends the calling thread's general allocations to a heap of their own, until ak_leave_scratch_heap() is given what
-// this returned. Work that frees nearly everything it allocates can use it to keep its temporary data out of the pages
-// of what outlives it. Blocks stay valid and are freed as usual after leaving. The heap belongs to the thread and is
-// reused, so what it held goes back to the system with ak_kmalloc_collect(), which must be called outside of it.
-[[nodiscard]] void* ak_enter_scratch_heap();
-void ak_leave_scratch_heap(void* previous_heap);
-
[[nodiscard]] inline void* kcalloc(size_t count, size_t size)
{
return ak_kcalloc(count, size);
diff --git a/Libraries/LibJS/Rust/src/jit/compile_queue.rs b/Libraries/LibJS/Rust/src/jit/compile_queue.rs
index ae626739041..51b283eab8b 100644
--- a/Libraries/LibJS/Rust/src/jit/compile_queue.rs
+++ b/Libraries/LibJS/Rust/src/jit/compile_queue.rs
@@ -261,13 +261,7 @@ impl CompileQueue {
let results_pending = Arc::clone(&self.results_pending);
self.client.submit(Box::new(move || {
note_thread_is_active();
- // NB: Nearly all a compile allocates is gone when it ends, so that goes into the scratch heap. The code it
- // leaves outlives it, and is no block of a heap. Only the texts of the debug options are, which are
- // gone as soon as the code is installed.
- let result = {
- let _scratch = ak::ScratchHeapScope::enter();
- compile(&snapshot)
- };
+ let result = compile(&snapshot);
give_back_snapshot(snapshot);
// NB: A VM that went away has no use for the result, and the thread goes on with other VMs' jobs.
if result_sender.send((id, result)).is_ok() {