diff --git a/Cargo.lock b/Cargo.lock index 3c67eed0ac8f..849372703271 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10133,6 +10133,7 @@ dependencies = [ name = "turbo-tasks-malloc" version = "0.1.0" dependencies = [ + "libmimalloc-sys", "mimalloc", ] diff --git a/crates/next-build-test/src/main.rs b/crates/next-build-test/src/main.rs index af1aca33ff7e..2c223031f47c 100644 --- a/crates/next-build-test/src/main.rs +++ b/crates/next-build-test/src/main.rs @@ -92,6 +92,7 @@ fn main() { *cell = Some(Instant::now()); } }); + TurboMalloc::thread_park(); }) .build() .unwrap() diff --git a/crates/next-napi-bindings/src/lib.rs b/crates/next-napi-bindings/src/lib.rs index 66a884881b97..5107b983c65a 100644 --- a/crates/next-napi-bindings/src/lib.rs +++ b/crates/next-napi-bindings/src/lib.rs @@ -106,6 +106,7 @@ fn init() { *cell = Some(Instant::now()); } }); + TurboMalloc::thread_park(); }) .worker_threads(worker_threads) // Avoid a limit on threads to avoid deadlocks due to usage of block_in_place diff --git a/turbopack/crates/turbo-tasks-backend/fuzz/src/graph.rs b/turbopack/crates/turbo-tasks-backend/fuzz/src/graph.rs index 928b4d082637..2cd238eb3104 100644 --- a/turbopack/crates/turbo-tasks-backend/fuzz/src/graph.rs +++ b/turbopack/crates/turbo-tasks-backend/fuzz/src/graph.rs @@ -87,6 +87,9 @@ static RUNTIME: Lazy = Lazy::new(|| { .on_thread_stop(|| { TurboMalloc::thread_stop(); }) + .on_thread_park(|| { + TurboMalloc::thread_park(); + }) .build() .unwrap() }); diff --git a/turbopack/crates/turbo-tasks-malloc/Cargo.toml b/turbopack/crates/turbo-tasks-malloc/Cargo.toml index 2933ebad133d..4a5a971554d6 100644 --- a/turbopack/crates/turbo-tasks-malloc/Cargo.toml +++ b/turbopack/crates/turbo-tasks-malloc/Cargo.toml @@ -11,6 +11,10 @@ bench = false [dependencies] +[target.'cfg(not(target_family = "wasm"))'.dependencies] +libmimalloc-sys = { version = "0.1.44", features = [ + "extended", +], optional = true } [target.'cfg(not(any(target_os = "linux", target_family = "wasm")))'.dependencies] mimalloc = { version = "0.1.48", features = [ @@ -26,5 +30,5 @@ mimalloc = { version = "0.1.48", features = [ ], optional = true } [features] -custom_allocator = ["dep:mimalloc"] +custom_allocator = ["dep:mimalloc", "dep:libmimalloc-sys"] default = ["custom_allocator"] diff --git a/turbopack/crates/turbo-tasks-malloc/src/lib.rs b/turbopack/crates/turbo-tasks-malloc/src/lib.rs index 47410b0e11a1..edf251ab604d 100644 --- a/turbopack/crates/turbo-tasks-malloc/src/lib.rs +++ b/turbopack/crates/turbo-tasks-malloc/src/lib.rs @@ -93,6 +93,13 @@ impl TurboMalloc { flush(); } + pub fn thread_park() { + #[cfg(all(feature = "custom_allocator", not(target_family = "wasm")))] + unsafe { + libmimalloc_sys::mi_collect(false); + } + } + pub fn allocation_counters() -> AllocationCounters { self::counter::allocation_counters() } diff --git a/turbopack/crates/turbopack-cli/benches/small_apps.rs b/turbopack/crates/turbopack-cli/benches/small_apps.rs index c8a2b747b2d2..9e455ecaee87 100644 --- a/turbopack/crates/turbopack-cli/benches/small_apps.rs +++ b/turbopack/crates/turbopack-cli/benches/small_apps.rs @@ -50,9 +50,13 @@ fn bench_small_apps(c: &mut Criterion) { b.iter(move || { let mut rt = tokio::runtime::Builder::new_multi_thread(); - rt.enable_all().on_thread_stop(|| { - TurboMalloc::thread_stop(); - }); + rt.enable_all() + .on_thread_stop(|| { + TurboMalloc::thread_stop(); + }) + .on_thread_park(|| { + TurboMalloc::thread_park(); + }); let rt = rt.build().unwrap(); let apps_dir = apps_dir.clone(); diff --git a/turbopack/crates/turbopack-cli/src/main.rs b/turbopack/crates/turbopack-cli/src/main.rs index 014ed07c93f8..a540e5b0223c 100644 --- a/turbopack/crates/turbopack-cli/src/main.rs +++ b/turbopack/crates/turbopack-cli/src/main.rs @@ -37,6 +37,7 @@ fn main() { *cell = Some(Instant::now()); } }); + TurboMalloc::thread_park(); }); let args = Arguments::parse();