Skip to content

Commit 5a488dc

Browse files
committed
Auto merge of #138764 - Zoxc:win-mimalloc, r=<try>
Use mimalloc as the global allocator on `x86_64-pc-windows-msvc` try-job: dist-x86_64-msvc
2 parents f9988fe + 908a5d3 commit 5a488dc

32 files changed

Lines changed: 95 additions & 54 deletions

File tree

Cargo.lock

Lines changed: 20 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]]
@@ -2216,6 +2216,15 @@ version = "0.2.15"
22162216
source = "registry+https://github.com/rust-lang/crates.io-index"
22172217
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
22182218

2219+
[[package]]
2220+
name = "libmimalloc-sys"
2221+
version = "0.1.46"
2222+
source = "registry+https://github.com/rust-lang/crates.io-index"
2223+
checksum = "bc89deee4af0429081d2a518c0431ae068222a5a262a3bc6ff4d8535ec2e02fe"
2224+
dependencies = [
2225+
"cc",
2226+
]
2227+
22192228
[[package]]
22202229
name = "libredox"
22212230
version = "0.1.10"
@@ -2399,6 +2408,15 @@ dependencies = [
23992408
"libc",
24002409
]
24012410

2411+
[[package]]
2412+
name = "mimalloc"
2413+
version = "0.1.49"
2414+
source = "registry+https://github.com/rust-lang/crates.io-index"
2415+
checksum = "aca3c01a711f395b4257b81674c0e90e8dd1f1e62c4b7db45f684cc7a4fcb18a"
2416+
dependencies = [
2417+
"libmimalloc-sys",
2418+
]
2419+
24022420
[[package]]
24032421
name = "minifier"
24042422
version = "0.3.6"
@@ -3808,6 +3826,7 @@ dependencies = [
38083826
"ctrlc",
38093827
"jiff",
38103828
"libc",
3829+
"mimalloc",
38113830
"rustc_abi",
38123831
"rustc_ast",
38133832
"rustc_ast_pretty",

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ See [the rustc-dev-guide for more info][sysllvm].
9696
--set llvm.libzstd=true \
9797
--set llvm.ninja=false \
9898
--set rust.debug-assertions=false \
99-
--set rust.jemalloc \
99+
--set rust.override-allocator \
100100
--set rust.bootstrap-override-lld=true \
101101
--set rust.lto=thin \
102102
--set rust.codegen-units=1

bootstrap.example.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@
848848
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
849849
# This option is only tested on Linux and OSX. It can also be configured per-target in the
850850
# [target.<tuple>] section.
851-
#rust.jemalloc = false
851+
#rust.override-allocator = false
852852

853853
# Run tests in various test suites with the "nll compare mode" in addition to
854854
# running the tests in normal mode. Largely only used on CI and during local
@@ -1121,8 +1121,8 @@
11211121
#optimized-compiler-builtins = build.optimized-compiler-builtins (bool or path)
11221122

11231123
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
1124-
# This overrides the global `rust.jemalloc` option. See that option for more info.
1125-
#jemalloc = rust.jemalloc (bool)
1124+
# This overrides the global `rust.override-allocator` option. See that option for more info.
1125+
#override-allocator = rust.override-allocator (bool)
11261126

11271127
# The linker configuration that will *override* the default linker used for Linux
11281128
# targets in the built compiler.

compiler/rustc/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@ rustc_public_bridge = { path = "../rustc_public_bridge" }
2626
getrandom = "=0.3.3"
2727
wasi = "=0.14.2"
2828

29-
30-
[dependencies.tikv-jemalloc-sys]
29+
[target.'cfg(not(target_os = "windows"))'.dependencies.tikv-jemalloc-sys]
3130
version = "0.6.1"
3231
optional = true
3332
features = ['override_allocator_on_supported_platforms']
3433

3534
[features]
3635
# tidy-alphabetical-start
3736
check_only = ['rustc_driver_impl/check_only']
38-
jemalloc = ['dep:tikv-jemalloc-sys']
3937
llvm = ['rustc_driver_impl/llvm']
4038
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
4139
llvm_offload = ['rustc_driver_impl/llvm_offload']
4240
max_level_info = ['rustc_driver_impl/max_level_info']
41+
override_allocator = ['dep:tikv-jemalloc-sys', 'rustc_driver_impl/override_allocator']
4342
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
4443
# tidy-alphabetical-end
4544

compiler/rustc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::process::ExitCode;
3737
// to compare their performance, see
3838
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
3939
// for an example of how to do so.
40-
#[cfg(feature = "jemalloc")]
40+
#[cfg(all(feature = "override_allocator", not(target_os = "windows")))]
4141
use tikv_jemalloc_sys as _;
4242

4343
fn main() -> ExitCode {

compiler/rustc_driver_impl/Cargo.toml

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

58+
[target.'cfg(target_os = "windows")'.dependencies.mimalloc]
59+
version = "0.1.49"
60+
optional = true
61+
5862
[features]
5963
# tidy-alphabetical-start
6064
check_only = ['rustc_interface/check_only']
6165
llvm = ['rustc_interface/llvm']
6266
llvm_enzyme = ['rustc_interface/llvm_enzyme']
6367
llvm_offload = ['rustc_interface/llvm_offload']
6468
max_level_info = ['rustc_log/max_level_info']
69+
override_allocator = ['dep:mimalloc']
6570
rustc_randomized_layouts = [
6671
'rustc_index/rustc_randomized_layouts',
6772
'rustc_middle/rustc_randomized_layouts'

compiler/rustc_driver_impl/src/lib.rs

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

66+
#[cfg(all(feature = "override_allocator", target_os = "windows"))]
67+
#[global_allocator]
68+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
69+
6670
#[allow(unused_macros)]
6771
macro do_not_use_print($($t:tt)*) {
6872
std::compile_error!(

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,9 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
14111411
}
14121412

14131413
// See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the tool build step.
1414-
if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
1414+
if builder.config.override_allocator(target)
1415+
&& env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
1416+
{
14151417
// Build jemalloc on AArch64 with support for page sizes up to 64K
14161418
// See: https://github.com/rust-lang/rust/pull/135081
14171419
if target.starts_with("aarch64") {

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ pub fn prepare_tool_cargo(
229229
cargo.env("LZMA_API_STATIC", "1");
230230

231231
// See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the compile build step.
232-
if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
232+
if builder.config.override_allocator(target)
233+
&& env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
234+
{
233235
// Build jemalloc on AArch64 with support for page sizes up to 64K
234236
// See: https://github.com/rust-lang/rust/pull/135081
235237
if target.starts_with("aarch64") {
@@ -752,8 +754,8 @@ impl Step for Rustdoc {
752754
// to build rustdoc.
753755
//
754756
let mut extra_features = Vec::new();
755-
if builder.config.jemalloc(target) {
756-
extra_features.push("jemalloc".to_string());
757+
if builder.config.override_allocator(target) {
758+
extra_features.push("override_allocator".to_string());
757759
}
758760

759761
let compilers = RustcPrivateCompilers::from_target_compiler(builder, target_compiler);
@@ -1576,8 +1578,8 @@ tool_rustc_extended!(Clippy {
15761578
stable: true,
15771579
add_bins_to_sysroot: ["clippy-driver"],
15781580
add_features: |builder, target, features| {
1579-
if builder.config.jemalloc(target) {
1580-
features.push("jemalloc".to_string());
1581+
if builder.config.override_allocator(target) {
1582+
features.push("override_allocator".to_string());
15811583
}
15821584
}
15831585
});
@@ -1587,8 +1589,8 @@ tool_rustc_extended!(Miri {
15871589
stable: false,
15881590
add_bins_to_sysroot: ["miri"],
15891591
add_features: |builder, target, features| {
1590-
if builder.config.jemalloc(target) {
1591-
features.push("jemalloc".to_string());
1592+
if builder.config.override_allocator(target) {
1593+
features.push("override_allocator".to_string());
15921594
}
15931595
},
15941596
// Always compile also tests when building miri. Otherwise feature unification can cause rebuilds between building and testing miri.

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub struct Config {
242242
pub hosts: Vec<TargetSelection>,
243243
pub targets: Vec<TargetSelection>,
244244
pub local_rebuild: bool,
245-
pub jemalloc: bool,
245+
pub override_allocator: bool,
246246
pub control_flow_guard: bool,
247247
pub ehcont_guard: bool,
248248

@@ -570,7 +570,7 @@ impl Config {
570570
thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
571571
parallel_frontend_threads: rust_parallel_frontend_threads,
572572
remap_debuginfo: rust_remap_debuginfo,
573-
jemalloc: rust_jemalloc,
573+
override_allocator: rust_override_allocator,
574574
test_compare_mode: rust_test_compare_mode,
575575
llvm_libunwind: rust_llvm_libunwind,
576576
control_flow_guard: rust_control_flow_guard,
@@ -900,7 +900,7 @@ impl Config {
900900
codegen_backends: target_codegen_backends,
901901
runner: target_runner,
902902
optimized_compiler_builtins: target_optimized_compiler_builtins,
903-
jemalloc: target_jemalloc,
903+
override_allocator: target_override_allocator,
904904
} = cfg;
905905

906906
let mut target = Target::from_triple(&triple);
@@ -976,7 +976,7 @@ impl Config {
976976
target.rpath = target_rpath;
977977
target.rustflags = target_rustflags.unwrap_or_default();
978978
target.optimized_compiler_builtins = target_optimized_compiler_builtins;
979-
target.jemalloc = target_jemalloc;
979+
target.override_allocator = target_override_allocator;
980980
if let Some(backends) = target_codegen_backends {
981981
target.codegen_backends =
982982
Some(parse_codegen_backends(backends, &format!("target.{triple}")))
@@ -1371,7 +1371,6 @@ impl Config {
13711371
initial_rustdoc,
13721372
initial_rustfmt,
13731373
initial_sysroot,
1374-
jemalloc: rust_jemalloc.unwrap_or(false),
13751374
jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),
13761375
json_output: flags_json_output,
13771376
keep_stage: flags_keep_stage,
@@ -1433,6 +1432,7 @@ impl Config {
14331432
on_fail: flags_on_fail,
14341433
optimized_compiler_builtins,
14351434
out,
1435+
override_allocator: rust_override_allocator.unwrap_or(false),
14361436
patch_binaries_for_nix: build_patch_binaries_for_nix,
14371437
path_modification_cache,
14381438
paths: flags_paths,
@@ -1864,8 +1864,11 @@ impl Config {
18641864
self.enabled_codegen_backends(target).first().unwrap()
18651865
}
18661866

1867-
pub fn jemalloc(&self, target: TargetSelection) -> bool {
1868-
self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
1867+
pub fn override_allocator(&self, target: TargetSelection) -> bool {
1868+
self.target_config
1869+
.get(&target)
1870+
.and_then(|cfg| cfg.override_allocator)
1871+
.unwrap_or(self.override_allocator)
18691872
}
18701873

18711874
pub fn rpath_enabled(&self, target: TargetSelection) -> bool {

0 commit comments

Comments
 (0)