Skip to content

Commit 22f306c

Browse files
committed
Mimalloc support as a separate config.toml option.
Remove misleading OSX from config.toml option comments as it is not really supported.
1 parent 35d9352 commit 22f306c

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

Cargo.lock

+18
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,12 @@ dependencies = [
12221222
"rustc-std-workspace-core",
12231223
]
12241224

1225+
[[package]]
1226+
name = "fs_extra"
1227+
version = "1.2.0"
1228+
source = "registry+https://github.com/rust-lang/crates.io-index"
1229+
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
1230+
12251231
[[package]]
12261232
name = "fst"
12271233
version = "0.3.5"
@@ -1612,6 +1618,17 @@ version = "0.4.6"
16121618
source = "registry+https://github.com/rust-lang/crates.io-index"
16131619
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
16141620

1621+
[[package]]
1622+
name = "jemalloc-sys"
1623+
version = "0.3.2"
1624+
source = "registry+https://github.com/rust-lang/crates.io-index"
1625+
checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45"
1626+
dependencies = [
1627+
"cc",
1628+
"fs_extra",
1629+
"libc",
1630+
]
1631+
16151632
[[package]]
16161633
name = "jobserver"
16171634
version = "0.1.21"
@@ -3405,6 +3422,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
34053422
name = "rustc-main"
34063423
version = "0.0.0"
34073424
dependencies = [
3425+
"jemalloc-sys",
34083426
"libmimalloc-sys",
34093427
"rustc_codegen_ssa",
34103428
"rustc_driver",

compiler/rustc/Cargo.toml

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ rustc_driver = { path = "../rustc_driver" }
1111
# crate is intended to be used by codegen backends, which may not be in-tree.
1212
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
1313

14-
#[dependencies.jemalloc-sys]
15-
#version = '0.3.0'
16-
#optional = true
17-
#features = ['unprefixed_malloc_on_supported_platforms']
14+
[dependencies.jemalloc-sys]
15+
version = '0.3.0'
16+
optional = true
17+
features = ['unprefixed_malloc_on_supported_platforms']
1818

1919
[dependencies.libmimalloc-sys]
20-
version = "0.1.20"
20+
version = '0.1.20'
2121
optional = true
2222
default-features = false
23-
features = ["extended", "override"]
23+
features = ['extended', 'override']
2424

2525
[features]
26-
jemalloc = ['libmimalloc-sys']
26+
jemalloc = ['jemalloc-sys']
27+
mimalloc = ['libmimalloc-sys']
2728
llvm = ['rustc_driver/llvm']
2829
max_level_info = ['rustc_driver/max_level_info']

compiler/rustc/src/main.rs

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ fn main() {
77
// dynamic libraries. That means to pull in jemalloc we need to actually
88
// reference allocation symbols one way or another (as this file is the only
99
// object code in the rustc executable).
10+
#[cfg(feature = "jemalloc-sys")]
11+
{
12+
use std::os::raw::{c_int, c_void};
13+
14+
#[used]
15+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
16+
#[used]
17+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
18+
jemalloc_sys::posix_memalign;
19+
#[used]
20+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
21+
#[used]
22+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
23+
#[used]
24+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
25+
#[used]
26+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
27+
}
28+
29+
// Pull in mimalloc when enabled.
30+
//
31+
// Note that we're pulling in a static copy of mimalloc which means that to
32+
// pull it in we need to actually reference its symbols for it to get
33+
// linked. The two crates we link to here, std and rustc_driver, are both
34+
// dynamic libraries. That means to pull in mimalloc we need to actually
35+
// reference allocation symbols one way or another (as this file is the only
36+
// object code in the rustc executable).
1037
#[cfg(feature = "libmimalloc-sys")]
1138
{
1239
use std::os::raw::{c_int, c_void};

config.toml.example

+6-2
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,13 @@ changelog-seen = 2
535535
# Map debuginfo paths to `/rust/$sha/...`, generally only set for releases
536536
#remap-debuginfo = false
537537

538-
# Link the compiler against `jemalloc`, where on Linux and OSX it should
539-
# override the default allocator for rustc and LLVM.
538+
# Link the compiler against `jemalloc`, where on Linux it should override
539+
# the default allocator for rustc and LLVM.
540540
#jemalloc = false
541+
#
542+
# Link the compiler against `mimalloc`, where on Linux it should override
543+
# the default allocator for rustc and LLVM.
544+
#mimalloc = false
541545

542546
# Run tests in various test suites with the "nll compare mode" in addition to
543547
# running the tests in normal mode. Largely only used on CI and during local

src/bootstrap/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub struct Config {
142142
pub targets: Vec<TargetSelection>,
143143
pub local_rebuild: bool,
144144
pub jemalloc: bool,
145+
pub mimalloc: bool,
145146
pub control_flow_guard: bool,
146147

147148
// dist misc
@@ -497,6 +498,7 @@ struct Rust {
497498
thin_lto_import_instr_limit: Option<u32>,
498499
remap_debuginfo: Option<bool>,
499500
jemalloc: Option<bool>,
501+
mimalloc: Option<bool>,
500502
test_compare_mode: Option<bool>,
501503
llvm_libunwind: Option<String>,
502504
control_flow_guard: Option<bool>,
@@ -849,6 +851,7 @@ impl Config {
849851
set(&mut config.codegen_tests, rust.codegen_tests);
850852
set(&mut config.rust_rpath, rust.rpath);
851853
set(&mut config.jemalloc, rust.jemalloc);
854+
set(&mut config.mimalloc, rust.mimalloc);
852855
set(&mut config.test_compare_mode, rust.test_compare_mode);
853856
config.llvm_libunwind = rust
854857
.llvm_libunwind

src/bootstrap/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ impl Build {
567567
if self.config.jemalloc {
568568
features.push_str("jemalloc");
569569
}
570+
if self.config.mimalloc {
571+
features.push_str("mimalloc");
572+
}
570573
if self.config.llvm_enabled() {
571574
features.push_str(" llvm");
572575
}

0 commit comments

Comments
 (0)