Skip to content

Commit 014c141

Browse files
committed
Use mimalloc as the global allocator on x86_64-pc-windows-msvc
1 parent 78948ac commit 014c141

File tree

9 files changed

+56
-1
lines changed

9 files changed

+56
-1
lines changed

Cargo.lock

+21-1
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
20212021
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
20222022
dependencies = [
20232023
"cfg-if",
2024-
"windows-targets 0.48.5",
2024+
"windows-targets 0.52.6",
20252025
]
20262026

20272027
[[package]]
@@ -2030,6 +2030,16 @@ version = "0.2.11"
20302030
source = "registry+https://github.com/rust-lang/crates.io-index"
20312031
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"
20322032

2033+
[[package]]
2034+
name = "libmimalloc-sys"
2035+
version = "0.1.40"
2036+
source = "registry+https://github.com/rust-lang/crates.io-index"
2037+
checksum = "07d0e07885d6a754b9c7993f2625187ad694ee985d60f23355ff0e7077261502"
2038+
dependencies = [
2039+
"cc",
2040+
"libc",
2041+
]
2042+
20332043
[[package]]
20342044
name = "libredox"
20352045
version = "0.1.3"
@@ -2217,6 +2227,15 @@ dependencies = [
22172227
"libc",
22182228
]
22192229

2230+
[[package]]
2231+
name = "mimalloc"
2232+
version = "0.1.44"
2233+
source = "registry+https://github.com/rust-lang/crates.io-index"
2234+
checksum = "99585191385958383e13f6b822e6b6d8d9cf928e7d286ceb092da92b43c87bc1"
2235+
dependencies = [
2236+
"libmimalloc-sys",
2237+
]
2238+
22202239
[[package]]
22212240
name = "mime"
22222241
version = "0.3.17"
@@ -3518,6 +3537,7 @@ version = "0.0.0"
35183537
dependencies = [
35193538
"ctrlc",
35203539
"libc",
3540+
"mimalloc",
35213541
"rustc_abi",
35223542
"rustc_ast",
35233543
"rustc_ast_lowering",

bootstrap.example.toml

+6
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,12 @@
748748
# [target.<tuple>] section.
749749
#jemalloc = false
750750

751+
# Use mimalloc as the global allocator for the compiler. Rust code will prefer `mimalloc` over
752+
# `jemalloc` if that is also enabled.
753+
# This option is only tested on Windows. It can also be configured per-target in the
754+
# [target.<tuple>] section.
755+
#mimalloc = false
756+
751757
# Run tests in various test suites with the "nll compare mode" in addition to
752758
# running the tests in normal mode. Largely only used on CI and during local
753759
# development of NLL

compiler/rustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ features = ['unprefixed_malloc_on_supported_platforms']
3030
jemalloc = ['dep:tikv-jemalloc-sys']
3131
llvm = ['rustc_driver_impl/llvm']
3232
max_level_info = ['rustc_driver_impl/max_level_info']
33+
mimalloc = ['rustc_driver_impl/mimalloc']
3334
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
3435
# tidy-alphabetical-end

compiler/rustc_driver_impl/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ time = { version = "0.3.36", default-features = false, features = ["alloc", "for
5454
tracing = { version = "0.1.35" }
5555
# tidy-alphabetical-end
5656

57+
[dependencies.mimalloc]
58+
version = "0.1.44"
59+
optional = true
60+
5761
[target.'cfg(all(unix, any(target_env = "gnu", target_os = "macos")))'.dependencies]
5862
# tidy-alphabetical-start
5963
libc = "0.2"
@@ -74,6 +78,7 @@ ctrlc = "3.4.4"
7478
# tidy-alphabetical-start
7579
llvm = ['rustc_interface/llvm']
7680
max_level_info = ['rustc_log/max_level_info']
81+
mimalloc = ['dep:mimalloc']
7782
rustc_randomized_layouts = [
7883
'rustc_index/rustc_randomized_layouts',
7984
'rustc_middle/rustc_randomized_layouts'

compiler/rustc_driver_impl/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ use time::OffsetDateTime;
7070
use time::macros::format_description;
7171
use tracing::trace;
7272

73+
#[cfg(feature = "mimalloc")]
74+
#[global_allocator]
75+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
76+
7377
#[allow(unused_macros)]
7478
macro do_not_use_print($($t:tt)*) {
7579
std::compile_error!(

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

+13
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ pub struct Config {
345345
jemalloc: bool,
346346
#[cfg(test)]
347347
pub jemalloc: bool,
348+
pub mimalloc: bool,
348349
pub control_flow_guard: bool,
349350
pub ehcont_guard: bool,
350351

@@ -663,6 +664,7 @@ pub struct Target {
663664
pub codegen_backends: Option<Vec<String>>,
664665
pub optimized_compiler_builtins: Option<bool>,
665666
pub jemalloc: Option<bool>,
667+
pub mimalloc: Option<bool>,
666668
}
667669

668670
impl Target {
@@ -1225,6 +1227,7 @@ define_config! {
12251227
thin_lto_import_instr_limit: Option<u32> = "thin-lto-import-instr-limit",
12261228
remap_debuginfo: Option<bool> = "remap-debuginfo",
12271229
jemalloc: Option<bool> = "jemalloc",
1230+
mimalloc: Option<bool> = "mimalloc",
12281231
test_compare_mode: Option<bool> = "test-compare-mode",
12291232
llvm_libunwind: Option<String> = "llvm-libunwind",
12301233
control_flow_guard: Option<bool> = "control-flow-guard",
@@ -1267,6 +1270,7 @@ define_config! {
12671270
runner: Option<String> = "runner",
12681271
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
12691272
jemalloc: Option<bool> = "jemalloc",
1273+
mimalloc: Option<bool> = "mimalloc",
12701274
}
12711275
}
12721276

@@ -1891,6 +1895,7 @@ impl Config {
18911895
thin_lto_import_instr_limit,
18921896
remap_debuginfo,
18931897
jemalloc,
1898+
mimalloc,
18941899
test_compare_mode,
18951900
llvm_libunwind,
18961901
control_flow_guard,
@@ -1963,6 +1968,7 @@ impl Config {
19631968
set(&mut config.rust_frame_pointers, frame_pointers);
19641969
config.rust_stack_protector = stack_protector;
19651970
set(&mut config.jemalloc, jemalloc);
1971+
set(&mut config.mimalloc, mimalloc);
19661972
set(&mut config.test_compare_mode, test_compare_mode);
19671973
set(&mut config.backtrace, backtrace);
19681974
if rust_description.is_some() {
@@ -2230,6 +2236,7 @@ impl Config {
22302236
target.rpath = cfg.rpath;
22312237
target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
22322238
target.jemalloc = cfg.jemalloc;
2239+
target.mimalloc = cfg.mimalloc;
22332240

22342241
if let Some(ref backends) = cfg.codegen_backends {
22352242
let available_backends = ["llvm", "cranelift", "gcc"];
@@ -2811,6 +2818,10 @@ impl Config {
28112818
self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
28122819
}
28132820

2821+
pub fn mimalloc(&self, target: TargetSelection) -> bool {
2822+
self.target_config.get(&target).and_then(|cfg| cfg.mimalloc).unwrap_or(self.mimalloc)
2823+
}
2824+
28142825
pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<String> {
28152826
self.codegen_backends(target).first().cloned()
28162827
}
@@ -3374,6 +3385,7 @@ fn check_incompatible_options_for_ci_rustc(
33743385
strip,
33753386
lld_mode,
33763387
jemalloc,
3388+
mimalloc,
33773389
rpath,
33783390
channel,
33793391
description,
@@ -3437,6 +3449,7 @@ fn check_incompatible_options_for_ci_rustc(
34373449
err!(current_rust_config.llvm_tools, llvm_tools, "rust");
34383450
err!(current_rust_config.llvm_bitcode_linker, llvm_bitcode_linker, "rust");
34393451
err!(current_rust_config.jemalloc, jemalloc, "rust");
3452+
err!(current_rust_config.mimalloc, mimalloc, "rust");
34403453
err!(current_rust_config.default_linker, default_linker, "rust");
34413454
err!(current_rust_config.stack_protector, stack_protector, "rust");
34423455
err!(current_rust_config.lto, lto, "rust");

src/bootstrap/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,9 @@ impl Build {
712712
if self.config.jemalloc(target) && check("jemalloc") {
713713
features.push("jemalloc");
714714
}
715+
if self.config.mimalloc(target) && check("mimalloc") {
716+
features.push("mimalloc");
717+
}
715718
if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
716719
features.push("llvm");
717720
}

src/ci/github-actions/jobs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ auto:
584584
--enable-full-tools
585585
--enable-profiler
586586
--set rust.codegen-units=1
587+
--set rust.mimalloc
587588
SCRIPT: python x.py build --set rust.debug=true opt-dist && PGO_HOST=x86_64-pc-windows-msvc ./build/x86_64-pc-windows-msvc/stage0-tools-bin/opt-dist windows-ci -- python x.py dist bootstrap --include-default-paths
588589
DIST_REQUIRE_ALL_TOOLS: 1
589590
CODEGEN_BACKENDS: llvm,cranelift

src/tools/tidy/src/deps.rs

+2
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
316316
"leb128",
317317
"libc",
318318
"libloading",
319+
"libmimalloc-sys",
319320
"linux-raw-sys",
320321
"litemap",
321322
"lock_api",
@@ -325,6 +326,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
325326
"measureme",
326327
"memchr",
327328
"memmap2",
329+
"mimalloc",
328330
"miniz_oxide",
329331
"nix",
330332
"nu-ansi-term",

0 commit comments

Comments
 (0)