Skip to content

Replace jemalloc with mimalloc config.toml option #81782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ dependencies = [
"syn",
]

[[package]]
name = "cty"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3"

[[package]]
name = "curl"
version = "0.4.34"
Expand Down Expand Up @@ -1218,9 +1224,9 @@ dependencies = [

[[package]]
name = "fs_extra"
version = "1.1.0"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"

[[package]]
name = "fst"
Expand Down Expand Up @@ -1805,6 +1811,16 @@ dependencies = [
"pkg-config",
]

[[package]]
name = "libmimalloc-sys"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e58f42b6424a0ed536678c65fd97cd64b4344bcf86251e284f7c0ce9eee40e64"
dependencies = [
"cmake",
"cty",
]

[[package]]
name = "libnghttp2-sys"
version = "0.1.4+1.41.0"
Expand Down Expand Up @@ -3407,6 +3423,7 @@ name = "rustc-main"
version = "0.0.0"
dependencies = [
"jemalloc-sys",
"libmimalloc-sys",
"rustc_codegen_ssa",
"rustc_driver",
]
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ version = '0.3.0'
optional = true
features = ['unprefixed_malloc_on_supported_platforms']

[dependencies.libmimalloc-sys]
version = '0.1.20'
optional = true
default-features = false
features = ['extended', 'override']

[features]
jemalloc = ['jemalloc-sys']
mimalloc = ['libmimalloc-sys']
llvm = ['rustc_driver/llvm']
max_level_info = ['rustc_driver/max_level_info']
29 changes: 29 additions & 0 deletions compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ fn main() {
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
}

// Pull in mimalloc when enabled.
//
// Note that we're pulling in a static copy of mimalloc which means that to
// pull it in we need to actually reference its symbols for it to get
// linked. The two crates we link to here, std and rustc_driver, are both
// dynamic libraries. That means to pull in mimalloc we need to actually
// reference allocation symbols one way or another (as this file is the only
// object code in the rustc executable).
#[cfg(feature = "libmimalloc-sys")]
{
use std::os::raw::{c_int, c_void};

#[used]
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = libmimalloc_sys::mi_calloc;
#[used]
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
libmimalloc_sys::mi_posix_memalign;
#[used]
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void =
libmimalloc_sys::mi_aligned_alloc;
#[used]
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = libmimalloc_sys::mi_malloc;
#[used]
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
libmimalloc_sys::mi_realloc;
#[used]
static _F6: unsafe extern "C" fn(*mut c_void) = libmimalloc_sys::mi_free;
}

rustc_driver::set_sigpipe_handler();
rustc_driver::main()
}
8 changes: 6 additions & 2 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,13 @@ changelog-seen = 2
# Map debuginfo paths to `/rust/$sha/...`, generally only set for releases
#remap-debuginfo = false

# Link the compiler against `jemalloc`, where on Linux and OSX it should
# override the default allocator for rustc and LLVM.
# Link the compiler against `jemalloc`, where on Linux it should override
# the default allocator for rustc and LLVM.
#jemalloc = false
#
# Link the compiler against `mimalloc`, where on Linux it should override
# the default allocator for rustc and LLVM.
#mimalloc = false

# Run tests in various test suites with the "nll compare mode" in addition to
# running the tests in normal mode. Largely only used on CI and during local
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct Config {
pub targets: Vec<TargetSelection>,
pub local_rebuild: bool,
pub jemalloc: bool,
pub mimalloc: bool,
pub control_flow_guard: bool,

// dist misc
Expand Down Expand Up @@ -497,6 +498,7 @@ struct Rust {
thin_lto_import_instr_limit: Option<u32>,
remap_debuginfo: Option<bool>,
jemalloc: Option<bool>,
mimalloc: Option<bool>,
test_compare_mode: Option<bool>,
llvm_libunwind: Option<String>,
control_flow_guard: Option<bool>,
Expand Down Expand Up @@ -849,6 +851,7 @@ impl Config {
set(&mut config.codegen_tests, rust.codegen_tests);
set(&mut config.rust_rpath, rust.rpath);
set(&mut config.jemalloc, rust.jemalloc);
set(&mut config.mimalloc, rust.mimalloc);
set(&mut config.test_compare_mode, rust.test_compare_mode);
config.llvm_libunwind = rust
.llvm_libunwind
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ impl Build {
if self.config.jemalloc {
features.push_str("jemalloc");
}
if self.config.mimalloc {
features.push_str("mimalloc");
}
if self.config.llvm_enabled() {
features.push_str(" llvm");
}
Expand Down