From 3d110c7374a70d821a0ea7cac93a36024c4b0ca3 Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Wed, 29 Apr 2026 23:08:04 +0000 Subject: [PATCH 1/2] feat: expose EdgeRuntime.miCollect() to main worker for mimalloc force purge --- Cargo.lock | 1 + ext/runtime/Cargo.toml | 1 + ext/runtime/js/namespaces.js | 1 + ext/runtime/lib.rs | 29 +++++++++++++++++++++++++++++ types/global.d.ts | 1 + 5 files changed, 33 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6106e6ba8..3894a38cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3339,6 +3339,7 @@ dependencies = [ "httparse", "hyper 0.14.28", "hyper 1.5.2", + "libc", "log", "memmem", "once_cell", diff --git a/ext/runtime/Cargo.toml b/ext/runtime/Cargo.toml index 771ba4af9..10d1099d8 100644 --- a/ext/runtime/Cargo.toml +++ b/ext/runtime/Cargo.toml @@ -30,6 +30,7 @@ http.workspace = true httparse.workspace = true hyper.workspace = true hyper_v014.workspace = true +libc.workspace = true log.workspace = true once_cell.workspace = true regex.workspace = true diff --git a/ext/runtime/js/namespaces.js b/ext/runtime/js/namespaces.js index 7bff9c577..9a8ef3bca 100644 --- a/ext/runtime/js/namespaces.js +++ b/ext/runtime/js/namespaces.js @@ -33,6 +33,7 @@ function installEdgeRuntimeNamespace(kind, terminationRequestTokenRid) { applySupabaseTag: (src, dest) => applySupabaseTag(src, dest), systemMemoryInfo: () => ops.op_system_memory_info(), raiseSegfault: () => ops.op_raise_segfault(), + miCollect: () => ops.op_mi_collect(), ...props, }; break; diff --git a/ext/runtime/lib.rs b/ext/runtime/lib.rs index 77378732e..73b308149 100644 --- a/ext/runtime/lib.rs +++ b/ext/runtime/lib.rs @@ -4,6 +4,9 @@ use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::sync::Arc; +#[cfg(target_os = "linux")] +use libc; + use base_mem_check::WorkerHeapStatistics; use base_rt::DropToken; use base_rt::RuntimeState; @@ -443,6 +446,31 @@ fn op_cancel_drop_token( Ok(()) } +#[op2(fast)] +fn op_mi_collect() { + #[cfg(target_os = "linux")] + { + use std::sync::OnceLock; + + type MiCollectFn = unsafe extern "C" fn(force: bool); + + static MI_COLLECT: OnceLock> = OnceLock::new(); + + let f = MI_COLLECT.get_or_init(|| unsafe { + let sym = libc::dlsym(libc::RTLD_DEFAULT, c"mi_collect".as_ptr()); + if sym.is_null() { + None + } else { + Some(std::mem::transmute::<*mut libc::c_void, MiCollectFn>(sym)) + } + }); + + if let Some(mi_collect) = f { + unsafe { mi_collect(true) }; + } + } +} + #[op2] #[serde] pub fn op_bootstrap_unstable_args(_state: &mut OpState) -> Vec { @@ -485,6 +513,7 @@ deno_core::extension!( op_tap_promise_metrics, op_cancel_drop_token, op_check_outbound_rate_limit, + op_mi_collect, ], esm_entry_point = "ext:runtime/bootstrap.js", esm = [ diff --git a/types/global.d.ts b/types/global.d.ts index dceade281..a00ee6dc2 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -196,6 +196,7 @@ declare namespace EdgeRuntime { export function applySupabaseTag(src: Request, dest: Request): void; export function systemMemoryInfo(): MemInfo; export function raiseSegfault(): void; + export function miCollect(): void; export { UserWorker as userWorkers }; } From b1bd3e8c6ba21d1d14e2d0f2bb30e633c4ba1797 Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Wed, 29 Apr 2026 23:31:46 +0000 Subject: [PATCH 2/2] stamp: clippy --- ext/runtime/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ext/runtime/lib.rs b/ext/runtime/lib.rs index 73b308149..ff78a5c7d 100644 --- a/ext/runtime/lib.rs +++ b/ext/runtime/lib.rs @@ -4,9 +4,6 @@ use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::sync::Arc; -#[cfg(target_os = "linux")] -use libc; - use base_mem_check::WorkerHeapStatistics; use base_rt::DropToken; use base_rt::RuntimeState;