Skip to content

Commit a437887

Browse files
committed
Use mimalloc as the allocator on x86_64-pc-windows-msvc
1 parent 93e80cc commit a437887

8 files changed

Lines changed: 103 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681"
756756
dependencies = [
757757
"serde",
758758
"termcolor",
759-
"unicode-width 0.1.14",
759+
"unicode-width 0.2.2",
760760
]
761761

762762
[[package]]
@@ -977,6 +977,12 @@ dependencies = [
977977
"windows-sys 0.61.2",
978978
]
979979

980+
[[package]]
981+
name = "cty"
982+
version = "0.2.2"
983+
source = "registry+https://github.com/rust-lang/crates.io-index"
984+
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
985+
980986
[[package]]
981987
name = "curl"
982988
version = "0.4.49"
@@ -2216,6 +2222,16 @@ version = "0.2.15"
22162222
source = "registry+https://github.com/rust-lang/crates.io-index"
22172223
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
22182224

2225+
[[package]]
2226+
name = "libmimalloc-sys"
2227+
version = "0.1.47"
2228+
source = "registry+https://github.com/rust-lang/crates.io-index"
2229+
checksum = "2d1eacfa31c33ec25e873c136ba5669f00f9866d0688bea7be4d3f7e43067df6"
2230+
dependencies = [
2231+
"cc",
2232+
"cty",
2233+
]
2234+
22192235
[[package]]
22202236
name = "libredox"
22212237
version = "0.1.10"
@@ -2399,6 +2415,15 @@ dependencies = [
23992415
"libc",
24002416
]
24012417

2418+
[[package]]
2419+
name = "mimalloc"
2420+
version = "0.1.50"
2421+
source = "registry+https://github.com/rust-lang/crates.io-index"
2422+
checksum = "b3627c4272df786b9260cabaa46aec1d59c93ede723d4c3ef646c503816b0640"
2423+
dependencies = [
2424+
"libmimalloc-sys",
2425+
]
2426+
24022427
[[package]]
24032428
name = "minifier"
24042429
version = "0.3.6"
@@ -3808,6 +3833,8 @@ dependencies = [
38083833
"ctrlc",
38093834
"jiff",
38103835
"libc",
3836+
"libmimalloc-sys",
3837+
"mimalloc",
38113838
"rustc_abi",
38123839
"rustc_ast",
38133840
"rustc_ast_pretty",

compiler/rustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ llvm = ['rustc_driver_impl/llvm']
4040
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
4141
llvm_offload = ['rustc_driver_impl/llvm_offload']
4242
max_level_info = ['rustc_driver_impl/max_level_info']
43+
mimalloc = ['rustc_driver_impl/mimalloc']
4344
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
4445
# tidy-alphabetical-end
4546

compiler/rustc_driver_impl/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ features = [
5555
ctrlc = "3.4.4"
5656
# tidy-alphabetical-end
5757

58+
[target.'cfg(target_os = "windows")'.dependencies.mimalloc]
59+
version = "0.1.50"
60+
optional = true
61+
62+
[target.'cfg(target_os = "windows")'.dependencies.libmimalloc-sys]
63+
version = "0.1.47"
64+
optional = true
65+
features = ["extended"]
66+
5867
[features]
5968
# tidy-alphabetical-start
6069
check_only = ['rustc_interface/check_only']
6170
llvm = ['rustc_interface/llvm']
6271
llvm_enzyme = ['rustc_interface/llvm_enzyme']
6372
llvm_offload = ['rustc_interface/llvm_offload']
6473
max_level_info = ['rustc_log/max_level_info']
74+
mimalloc = ['dep:mimalloc', 'dep:libmimalloc-sys']
6575
rustc_randomized_layouts = [
6676
'rustc_index/rustc_randomized_layouts',
6777
'rustc_middle/rustc_randomized_layouts'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#[cfg(all(target_os = "windows", feature = "mimalloc"))]
2+
#[global_allocator]
3+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
4+
5+
#[cfg(all(target_os = "windows", feature = "mimalloc"))]
6+
pub mod fns {
7+
use std::os::raw::{c_char, c_int, c_void};
8+
9+
#[unsafe(no_mangle)]
10+
pub unsafe extern "C" fn calloc(items: usize, size: usize) -> *mut c_void {
11+
unsafe { libmimalloc_sys::mi_calloc(items, size) }
12+
}
13+
14+
#[unsafe(no_mangle)]
15+
pub unsafe extern "C" fn posix_memalign(
16+
ptr: *mut *mut c_void,
17+
align: usize,
18+
size: usize,
19+
) -> c_int {
20+
unsafe { libmimalloc_sys::mi_posix_memalign(ptr, align, size) }
21+
}
22+
23+
#[unsafe(no_mangle)]
24+
pub unsafe extern "C" fn strndup(s: *const c_char, n: usize) -> *mut c_char {
25+
unsafe { libmimalloc_sys::mi_strndup(s, n) }
26+
}
27+
28+
#[unsafe(no_mangle)]
29+
pub unsafe extern "C" fn strdup(s: *const c_char) -> *mut c_char {
30+
unsafe { libmimalloc_sys::mi_strdup(s) }
31+
}
32+
33+
#[unsafe(no_mangle)]
34+
pub unsafe extern "C" fn aligned_alloc(align: usize, size: usize) -> *mut c_void {
35+
unsafe { libmimalloc_sys::mi_aligned_alloc(align, size) }
36+
}
37+
38+
#[unsafe(no_mangle)]
39+
pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void {
40+
unsafe { libmimalloc_sys::mi_malloc(size) }
41+
}
42+
43+
#[unsafe(no_mangle)]
44+
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
45+
unsafe { libmimalloc_sys::mi_realloc(ptr, size) }
46+
}
47+
48+
#[unsafe(no_mangle)]
49+
pub unsafe extern "C" fn free(ptr: *mut c_void) {
50+
unsafe {
51+
libmimalloc_sys::mi_free(ptr);
52+
}
53+
}
54+
}

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ use rustc_target::json::ToJson;
6363
use rustc_target::spec::{Target, TargetTuple};
6464
use tracing::trace;
6565

66+
pub mod alloc;
67+
6668
#[allow(unused_macros)]
6769
macro do_not_use_print($($t:tt)*) {
6870
std::compile_error!(

src/bootstrap/src/core/config/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,14 @@ impl<'de> Deserialize<'de> for CompilerBuiltins {
254254
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
255255
pub enum OverrideAllocator {
256256
Jemalloc,
257+
Mimalloc,
257258
}
258259

259260
impl OverrideAllocator {
260261
pub fn feature_name(self) -> &'static str {
261262
match self {
262263
OverrideAllocator::Jemalloc => "jemalloc",
264+
OverrideAllocator::Mimalloc => "mimalloc",
263265
}
264266
}
265267
}
@@ -272,7 +274,8 @@ impl<'de> Deserialize<'de> for OverrideAllocator {
272274
let name = String::deserialize(deserializer)?;
273275
match name.as_str() {
274276
"jemalloc" => Ok(Self::Jemalloc),
275-
other => Err(serde::de::Error::unknown_variant(other, &["jemalloc"])),
277+
"mimalloc" => Ok(Self::Mimalloc),
278+
other => Err(serde::de::Error::unknown_variant(other, &["jemalloc", "mimalloc"])),
276279
}
277280
}
278281
}

src/ci/github-actions/jobs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ auto:
659659
--target=x86_64-pc-windows-msvc
660660
--enable-full-tools
661661
--enable-profiler
662+
--set rust.override-allocator=mimalloc
662663
--set rust.codegen-units=1
663664
SCRIPT: python x.py build --set rust.debug=true opt-dist && PGO_HOST=x86_64-pc-windows-msvc ./build/x86_64-pc-windows-msvc/stage1-tools-bin/opt-dist windows-ci -- python x.py dist bootstrap --include-default-paths
664665
DIST_REQUIRE_ALL_TOOLS: 1

src/tools/tidy/src/deps.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
304304
"crossbeam-utils",
305305
"crypto-common",
306306
"ctrlc",
307+
"cty",
307308
"darling",
308309
"darling_core",
309310
"darling_macro",
@@ -356,6 +357,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
356357
"leb128",
357358
"libc",
358359
"libloading",
360+
"libmimalloc-sys",
359361
"linux-raw-sys",
360362
"litemap",
361363
"lock_api",
@@ -365,6 +367,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
365367
"measureme",
366368
"memchr",
367369
"memmap2",
370+
"mimalloc",
368371
"miniz_oxide",
369372
"nix",
370373
"nu-ansi-term",

0 commit comments

Comments
 (0)