diff --git a/Cargo.lock b/Cargo.lock index 02d9e3a7ae1..95fef23e08c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1488,6 +1488,12 @@ dependencies = [ "cipher", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "cursor-icon" version = "1.2.0" @@ -4311,6 +4317,17 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" +[[package]] +name = "libmimalloc-sys" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec9d6fac27761dabcd4ee73571cdb06b7022dc99089acbe5435691edffaac0f4" +dependencies = [ + "cc", + "cty", + "libc", +] + [[package]] name = "libredox" version = "0.1.3" @@ -4622,6 +4639,15 @@ dependencies = [ "servo_url", ] +[[package]] +name = "mimalloc" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "995942f432bbb4822a7e9c3faa87a695185b0d09273ba85f097b54f4e458f2af" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" @@ -6894,6 +6920,8 @@ name = "servo_allocator" version = "0.0.1" dependencies = [ "libc", + "libmimalloc-sys", + "mimalloc", "tikv-jemalloc-sys", "tikv-jemallocator", "windows-sys 0.59.0", diff --git a/components/allocator/Cargo.toml b/components/allocator/Cargo.toml index ae38e78db82..477598bc25c 100644 --- a/components/allocator/Cargo.toml +++ b/components/allocator/Cargo.toml @@ -12,6 +12,7 @@ path = "lib.rs" [features] use-system-allocator = ["libc"] +use-mimalloc = ["mimalloc", "libmimalloc-sys"] [target.'cfg(not(any(windows, target_env = "ohos")))'.dependencies] libc = { workspace = true, optional = true } @@ -23,3 +24,7 @@ windows-sys = { workspace = true, features = ["Win32_System_Memory"] } [target.'cfg(target_env = "ohos")'.dependencies] libc = { workspace = true } + +[dependencies] +mimalloc = { version = "0.1", features = ["extended"], optional = true } +libmimalloc-sys = { version = "0.1", optional = true } diff --git a/components/allocator/lib.rs b/components/allocator/lib.rs index 96160b01e7c..db801b14f31 100644 --- a/components/allocator/lib.rs +++ b/components/allocator/lib.rs @@ -9,7 +9,12 @@ static ALLOC: Allocator = Allocator; pub use crate::platform::*; -#[cfg(not(any(windows, feature = "use-system-allocator", target_env = "ohos")))] +#[cfg(not(any( + windows, + feature = "use-system-allocator", + feature = "use-mimalloc", + target_env = "ohos" +)))] mod platform { use std::os::raw::c_void; @@ -32,6 +37,7 @@ mod platform { #[cfg(all( not(windows), + not(feature = "use-mimalloc"), any(feature = "use-system-allocator", target_env = "ohos") ))] mod platform { @@ -60,7 +66,7 @@ mod platform { } } -#[cfg(windows)] +#[cfg(all(windows, not(feature = "use-mimalloc")))] mod platform { pub use std::alloc::System as Allocator; use std::os::raw::c_void; @@ -85,3 +91,24 @@ mod platform { } } } + +#[cfg(feature = "use-mimalloc")] +mod platform { + use std::os::raw::c_void; + + pub use mimalloc::MiMalloc as Allocator; + + /// Get the size of a heap block. + /// + /// # Safety + /// + /// Passing a non-heap allocated pointer to this function results in undefined behavior. + pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize { + unsafe { Allocator.usable_size(ptr as _) } + } + + /// Memory allocation APIs compatible with libc + pub mod libc_compat { + pub use libmimalloc_sys::{mi_free as free, mi_malloc as malloc, mi_realloc as realloc}; + } +} diff --git a/ports/servoshell/Cargo.toml b/ports/servoshell/Cargo.toml index edeaef5408f..7070f37fca5 100644 --- a/ports/servoshell/Cargo.toml +++ b/ports/servoshell/Cargo.toml @@ -52,6 +52,7 @@ webdriver = ["libservo/webdriver"] webgl_backtrace = ["libservo/webgl_backtrace"] webgpu = ["libservo/webgpu"] webxr = ["libservo/webxr"] +use-mimalloc = ["servo_allocator/use-mimalloc"] [dependencies] cfg-if = { workspace = true }