diff --git a/Cargo.lock b/Cargo.lock index 8a42a1322..bf9e88c19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,16 @@ version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +[[package]] +name = "libmimalloc-sys" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec9d6fac27761dabcd4ee73571cdb06b7022dc99089acbe5435691edffaac0f4" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "lock_api" version = "0.4.12" @@ -105,6 +115,15 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "mimalloc" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "995942f432bbb4822a7e9c3faa87a695185b0d09273ba85f097b54f4e458f2af" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "nasm-rs" version = "0.3.0" @@ -177,11 +196,13 @@ dependencies = [ "cc", "cfg-if", "libc", + "mimalloc", "nasm-rs", "parking_lot", "paste", "raw-cpuid", "strum", + "tikv-jemallocator", "to_method", "zerocopy", ] @@ -272,6 +293,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tikv-jemalloc-sys" +version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "tikv-jemallocator" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cec5ff18518d81584f477e9bfdf957f5bb0979b0bac3af4ca30b5b3ae2d2865" +dependencies = [ + "libc", + "tikv-jemalloc-sys", +] + [[package]] name = "to_method" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1895a3b41..71e324338 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,10 @@ raw-cpuid = "11.0.1" strum = { version = "0.27", features = ["derive"] } to_method = "1.1.0" zerocopy = { version = "0.7.32", features = ["derive"] } +mimalloc = { version = "0.1.46", optional = true } + +[target.'cfg(not(target_env = "msvc"))'.dependencies] +tikv-jemallocator = { version = "0.6.0", optional = true } [build-dependencies] cc = "1.0.79" @@ -42,6 +46,13 @@ asm_arm64_i8mm = ["asm"] asm_arm64_sve2 = ["asm"] bitdepth_8 = [] bitdepth_16 = [] +# Use the mimalloc memory allocator +mimalloc = ["dep:mimalloc"] +# Use the mimalloc memory allocator in secure mode +# (This has a ~10% performance penalty!) +mimalloc_secure = ["mimalloc/secure"] +# Use the jemalloc memory allocator (used in Firefox) +jemalloc = ["dep:tikv-jemallocator"] [profile.dev] panic = "abort" diff --git a/meson.build b/meson.build index b985b4ade..90ddf611e 100644 --- a/meson.build +++ b/meson.build @@ -556,6 +556,9 @@ cargo_command = [ if get_option('buildtype') == 'release' cargo_command += ['--release'] endif +if get_option('malloc') != 'default' + cargo_command += ['--features', get_option('malloc')] +endif librav1d = custom_target( 'librav1d', diff --git a/meson_options.txt b/meson_options.txt index 374e3ca31..92e5a0493 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -78,4 +78,10 @@ option('test_rust_path', option('seek_stress_test_rust_path', type: 'string', value: '', - description: 'Use specified Rust binary instead of building it. Path must be relative to build directory.') \ No newline at end of file + description: 'Use specified Rust binary instead of building it. Path must be relative to build directory.') + +option('malloc', + type: 'combo', + choices: ['default', 'mimalloc', 'mimalloc_secure', 'jemalloc'], + value: 'default', + description: 'Select memory allocator to use') diff --git a/src/lib.rs b/src/lib.rs index 6c9366eb0..9c0826d60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,16 @@ #![deny(unsafe_op_in_unsafe_fn)] +#[cfg(all(feature = "mimalloc", feature = "jemalloc"))] +compile_error!("You may only use one allocator at a time."); + +#[cfg(feature = "mimalloc")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + #[cfg(feature = "bitdepth_16")] use crate::include::common::bitdepth::BitDepth16; #[cfg(feature = "bitdepth_8")]