Unverified Commit 80cff8c8 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: use mimalloc v3 (#82221)

### What?

* Upgrade to mimalloc 3
* Compute allocations size more accurately (for tracing)
parent 22899755
...@@ -1748,6 +1748,12 @@ dependencies = [ ...@@ -1748,6 +1748,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.14.4" version = "0.14.4"
...@@ -3654,16 +3660,6 @@ dependencies = [ ...@@ -3654,16 +3660,6 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "libmimalloc-sys"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf88cd67e9de251c1781dbe2f641a1a3ad66eaae831b8a2c38fbdc5ddae16d4d"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "libunwind" name = "libunwind"
version = "1.3.3" version = "1.3.3"
...@@ -4034,12 +4030,12 @@ dependencies = [ ...@@ -4034,12 +4030,12 @@ dependencies = [
] ]
[[package]] [[package]]
name = "mimalloc" name = "mimalloc-rspack"
version = "0.1.47" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1791cbe101e95af5764f06f20f6760521f7158f69dbf9d6baf941ee1bf6bc40" checksum = "de20bb33bf95d9c060030d53bcb5d5dc03cbbedfbd1dcda5f6f285b946dae2c0"
dependencies = [ dependencies = [
"libmimalloc-sys", "rspack-libmimalloc-sys",
] ]
[[package]] [[package]]
...@@ -6091,6 +6087,17 @@ dependencies = [ ...@@ -6091,6 +6087,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "rspack-libmimalloc-sys"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c741844b1fbe0cfbae8dfeb73b5e5fdc95daf7be58bdd72afb3fd85c86bccdb"
dependencies = [
"cc",
"cty",
"libc",
]
[[package]] [[package]]
name = "rstest" name = "rstest"
version = "0.16.0" version = "0.16.0"
...@@ -9455,7 +9462,8 @@ dependencies = [ ...@@ -9455,7 +9462,8 @@ dependencies = [
name = "turbo-tasks-malloc" name = "turbo-tasks-malloc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"mimalloc", "mimalloc-rspack",
"rspack-libmimalloc-sys",
] ]
[[package]] [[package]]
......
...@@ -9,14 +9,22 @@ autobenches = false ...@@ -9,14 +9,22 @@ autobenches = false
[lib] [lib]
bench = false bench = false
[dependencies]
rspack-libmimalloc-sys = { version = "0.2.4", default-features = false, features = [], optional = true }
[target.'cfg(not(any(target_os = "linux", target_family = "wasm", target_env = "musl")))'.dependencies] [target.'cfg(not(any(target_os = "linux", target_family = "wasm", target_env = "musl")))'.dependencies]
mimalloc = { version = "0.1.47", features = [], optional = true } mimalloc-rspack = { version = "0.2.4", features = [
"v3",
"extended"
], optional = true }
[target.'cfg(all(target_os = "linux", not(any(target_family = "wasm", target_env = "musl"))))'.dependencies] [target.'cfg(all(target_os = "linux", not(any(target_family = "wasm", target_env = "musl"))))'.dependencies]
mimalloc = { version = "0.1.47", features = [ mimalloc-rspack = { version = "0.2.4", features = [
"v3",
"extended",
"local_dynamic_tls", "local_dynamic_tls",
], optional = true } ], optional = true }
[features] [features]
custom_allocator = ["dep:mimalloc"] custom_allocator = ["dep:mimalloc-rspack", "dep:rspack-libmimalloc-sys"]
default = ["custom_allocator"] default = ["custom_allocator"]
...@@ -84,7 +84,7 @@ fn base_alloc() -> &'static impl GlobalAlloc { ...@@ -84,7 +84,7 @@ fn base_alloc() -> &'static impl GlobalAlloc {
feature = "custom_allocator", feature = "custom_allocator",
not(any(target_family = "wasm", target_env = "musl")) not(any(target_family = "wasm", target_env = "musl"))
))] ))]
return &mimalloc::MiMalloc; return &mimalloc_rspack::MiMalloc;
#[cfg(any( #[cfg(any(
not(feature = "custom_allocator"), not(feature = "custom_allocator"),
any(target_family = "wasm", target_env = "musl") any(target_family = "wasm", target_env = "musl")
...@@ -92,32 +92,53 @@ fn base_alloc() -> &'static impl GlobalAlloc { ...@@ -92,32 +92,53 @@ fn base_alloc() -> &'static impl GlobalAlloc {
return &std::alloc::System; return &std::alloc::System;
} }
#[allow(unused_variables)]
unsafe fn base_alloc_size(ptr: *const u8, layout: Layout) -> usize {
#[cfg(all(
feature = "custom_allocator",
not(any(target_family = "wasm", target_env = "musl"))
))]
return unsafe { mimalloc_rspack::MiMalloc.usable_size(ptr) };
#[cfg(any(
not(feature = "custom_allocator"),
any(target_family = "wasm", target_env = "musl")
))]
return layout.size();
}
unsafe impl GlobalAlloc for TurboMalloc { unsafe impl GlobalAlloc for TurboMalloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ret = unsafe { base_alloc().alloc(layout) }; let ret = unsafe { base_alloc().alloc(layout) };
if !ret.is_null() { if !ret.is_null() {
add(layout.size()); let size = unsafe { base_alloc_size(ret, layout) };
add(size);
} }
ret ret
} }
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let size = unsafe { base_alloc_size(ptr, layout) };
unsafe { base_alloc().dealloc(ptr, layout) }; unsafe { base_alloc().dealloc(ptr, layout) };
remove(layout.size()); remove(size);
} }
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ret = unsafe { base_alloc().alloc_zeroed(layout) }; let ret = unsafe { base_alloc().alloc_zeroed(layout) };
if !ret.is_null() { if !ret.is_null() {
add(layout.size()); let size = unsafe { base_alloc_size(ret, layout) };
add(size);
} }
ret ret
} }
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let old_size = unsafe { base_alloc_size(ptr, layout) };
let ret = unsafe { base_alloc().realloc(ptr, layout, new_size) }; let ret = unsafe { base_alloc().realloc(ptr, layout, new_size) };
if !ret.is_null() { if !ret.is_null() {
let old_size = layout.size(); // SAFETY: the caller must ensure that the `new_size` does not overflow.
// `layout.align()` comes from a `Layout` and is thus guaranteed to be valid.
let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
let new_size = unsafe { base_alloc_size(ret, new_layout) };
update(old_size, new_size); update(old_size, new_size);
} }
ret ret
......
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