Truly independent web browser
AK+LibJS: Keep JIT compile temporaries in a scratch heap
Andreas Klingbf5ab87c934b
diff --git a/AK/FFIHelpers.cpp b/AK/FFIHelpers.cppindex 620662ab20f..5250b73b8ac 100644--- a/AK/FFIHelpers.cpp+++ b/AK/FFIHelpers.cpp@@ -206,6 +206,16 @@ 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.hindex 9d84f726c59..e7ab2ffef11 100644--- a/AK/FFIHelpers.h+++ b/AK/FFIHelpers.h@@ -30,6 +30,10 @@ 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);+// Allocations in HeapPartition::JSObjectStorage, where JS objects keep their property values. ak_kfree frees them.void* ladybird_js_object_storage_alloc(size_t size);void* ladybird_js_object_storage_realloc(void*, size_t new_size);diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rsindex f655458ba82..82cf03c4ded 100644--- a/AK/Rust/src/lib.rs+++ b/AK/Rust/src/lib.rs@@ -97,6 +97,8 @@ 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,@@ -480,6 +482,30 @@ pub fn release_unused_allocator_memory() {unsafe { ladybird_release_unused_allocator_memory() }}+/// 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.cppindex 70ad04dd5f4..1c857dd6fcf 100644--- a/AK/kmalloc.cpp+++ b/AK/kmalloc.cpp@@ -5,6 +5,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Assertions.h>#include <AK/Platform.h>#include <AK/kmalloc.h>@@ -85,6 +86,15 @@ void ak_kmalloc_collect(){}+void* ak_enter_scratch_heap()+{+ return nullptr;+}++void ak_leave_scratch_heap(void*)+{+}+#else# if defined(AK_OS_LINUX) || defined(AK_OS_MACOS)@@ -114,6 +124,7 @@ void* ak_kmalloc(size_t size)static thread_local mi_heap_t* s_buffer_heap = nullptr;static thread_local mi_heap_t* s_string_heap = nullptr;+static thread_local mi_heap_t* s_scratch_heap = nullptr;static mi_heap_t* heap_for_partition(HeapPartition partition){@@ -178,14 +189,34 @@ 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 the thread-local partitions have to be collected// separately. The remaining partitions are shared between threads and are left to their owners.if (s_buffer_heap)mi_heap_collect(s_buffer_heap, true);if (s_string_heap)mi_heap_collect(s_string_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));+}+#endifdiff --git a/AK/kmalloc.h b/AK/kmalloc.hindex 9517b892c27..da02a54142c 100644--- a/AK/kmalloc.h+++ b/AK/kmalloc.h@@ -32,6 +32,13 @@ 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.rsindex 4d3128c7081..0ebd650cd22 100644--- a/Libraries/LibJS/Rust/src/jit/compile_queue.rs+++ b/Libraries/LibJS/Rust/src/jit/compile_queue.rs@@ -90,7 +90,19 @@ impl CompileQueue {continue;}};- let result = libjs_jit::compile(&snapshot);+ let compiled = {+ let _scratch = ak::ScratchHeapScope::enter();+ libjs_jit::compile(&snapshot)+ };+ // NB: Nearly all a compile allocates is gone when it ends. The code it leaves+ // outlives it, and would keep the pages of its temporary data if it stayed+ // there, so it moves out of the scratch heap here.+ #[allow(+ clippy::map_clone,+ clippy::redundant_clone,+ reason = "the copy is what moves the code out of the scratch heap"+ )]+ let result = compiled.map(|code| code.clone());if result_sender.send((id, result)).is_err() {return;}