Unverified Commit b9cd4489 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Add TurboMalloc::thread_park() to flush and collect on thread park (#92804)

### What?

Adds `TurboMalloc::thread_park()` — a new method on `TurboMalloc` that
calls `libmimalloc_sys::mi_collect(false)` to process mimalloc's lazy
free lists when a Tokio worker thread parks.

Registers it as an `on_thread_park` callback in every Tokio runtime that
already registers `on_thread_stop`:
- `turbopack-cli/src/main.rs`
- `turbopack-cli/benches/small_apps.rs`
- `next-napi-bindings/src/lib.rs`
- `next-build-test/src/main.rs`
- `turbo-tasks-backend/fuzz/src/graph.rs`

### Why?

mimalloc uses deferred freeing: memory freed by one thread is not
reclaimed immediately but is queued until that thread performs N more
allocations. Tokio worker threads that are parked (waiting for work)
don't allocate, so deferred frees accumulate indefinitely — memory is
never returned to the OS until the thread wakes up and does real work.

Calling `mi_collect(false)` on park processes the lazy free lists,
allowing mimalloc to reclaim that memory, while avoiding the syscalls
and globally synchronized operations that `mi_collect(true)` would
trigger.

### How?

- `thread_park()` only calls `mi_collect(false)` — it intentionally does
**not** call `flush()`. `flush()` transfers the thread-local allocation
counter to the global atomic and resets it to zero, which would violate
the trace server's assumption that per-thread counters are monotonically
increasing. Counter sync remains exclusively in `thread_stop()`.
- `thread_park()` is feature-gated behind `cfg(all(feature =
"custom_allocator", not(target_family = "wasm")))` and is a no-op
otherwise.
- `libmimalloc-sys` is added as a direct optional dependency under
`[target.'cfg(not(target_family = "wasm"))'.dependencies]` (it was
already a transitive dep via `mimalloc`) to access `mi_collect`
directly, since `mimalloc`'s `use ffi::*` is not a public re-export.
- In each `on_thread_park` callback, `TurboMalloc::thread_park()` is
called **after** the existing SWC atom store GC, so atoms freed by that
GC pass are also eligible for reclamation in the same collection.

<!-- NEXT_JS_LLM_PR -->

---------
Co-authored-by: 's avatarTobias Koppers <sokra@users.noreply.github.com>
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
parent b56f1555
......@@ -10132,6 +10132,7 @@ dependencies = [
name = "turbo-tasks-malloc"
version = "0.1.0"
dependencies = [
"libmimalloc-sys",
"mimalloc",
]
......
......@@ -92,6 +92,7 @@ fn main() {
*cell = Some(Instant::now());
}
});
TurboMalloc::thread_park();
})
.build()
.unwrap()
......
......@@ -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
......
......@@ -87,6 +87,9 @@ static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
.on_thread_stop(|| {
TurboMalloc::thread_stop();
})
.on_thread_park(|| {
TurboMalloc::thread_park();
})
.build()
.unwrap()
});
......
......@@ -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"]
......@@ -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()
}
......
......@@ -50,8 +50,12 @@ 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(|| {
rt.enable_all()
.on_thread_stop(|| {
TurboMalloc::thread_stop();
})
.on_thread_park(|| {
TurboMalloc::thread_park();
});
let rt = rt.build().unwrap();
......
......@@ -37,6 +37,7 @@ fn main() {
*cell = Some(Instant::now());
}
});
TurboMalloc::thread_park();
});
let args = Arguments::parse();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment