Use mimalloc as the global allocator on x86_64-pc-windows-msvc by Zoxc · Pull Request #138764 · rust-lang/rust (original) (raw)

Zoxc

This adds using mimalloc as the global allocator for the compiler as an option to bootstrap.toml and enables it for the x86_64-pc-windows-msvc dist builder.

try-job: dist-x86_64-msvc

@Zoxc

@rustbot

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-testsuite

Area: The testsuite used to check the correctness of rustc

A-tidy

Area: The tidy tool

S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

T-infra

Relevant to the infrastructure team, which will review and decide on the PR/issue.

labels

Mar 21, 2025

@Zoxc

Local benchmarks:

Benchmark Before After Before After Before After
Time Time % Physical Memory Physical Memory % Committed Memory Committed Memory %
🟣 clap:check 1.4133s 1.2131s 💚 -14.17% 147.10 MiB 147.52 MiB 0.28% 200.42 MiB 261.79 MiB 💔 30.62%
🔵 clap:release 5.7162s 5.4820s 💚 -4.10% 285.77 MiB 289.07 MiB 💔 1.15% 324.74 MiB 384.72 MiB 💔 18.47%
🟠 clap:debug 3.2341s 2.9639s 💚 -8.35% 289.52 MiB 305.29 MiB 💔 5.44% 331.62 MiB 394.30 MiB 💔 18.90%
🟣 hyper:check 0.2368s 0.2082s 💚 -12.07% 80.00 MiB 80.85 MiB 💔 1.06% 130.63 MiB 195.31 MiB 💔 49.51%
🔵 hyper:release 0.8620s 0.8324s 💚 -3.42% 100.95 MiB 103.28 MiB 💔 2.31% 144.99 MiB 201.68 MiB 💔 39.10%
🟠 hyper:debug 0.4746s 0.4364s 💚 -8.05% 113.33 MiB 115.87 MiB 💔 2.24% 152.39 MiB 213.92 MiB 💔 40.38%
🟣 regex:check 0.8054s 0.6930s 💚 -13.96% 108.49 MiB 108.13 MiB -0.34% 155.61 MiB 223.32 MiB 💔 43.51%
🔵 regex:release 5.2164s 5.0395s 💚 -3.39% 311.05 MiB 389.23 MiB 💔 25.13% 294.30 MiB 428.30 MiB 💔 45.53%
🟠 regex:debug 1.8545s 1.7079s 💚 -7.91% 239.77 MiB 242.05 MiB 0.95% 275.22 MiB 324.94 MiB 💔 18.07%
🟣 syn:check 1.3455s 1.1368s 💚 -15.51% 142.38 MiB 141.26 MiB -0.78% 190.93 MiB 255.30 MiB 💔 33.72%
🔵 syn:release 4.2406s 3.9707s 💚 -6.37% 256.63 MiB 296.60 MiB 💔 15.58% 295.81 MiB 349.53 MiB 💔 18.16%
🟠 syn:debug 2.5499s 2.2981s 💚 -9.87% 236.51 MiB 242.18 MiB 💔 2.39% 276.62 MiB 358.63 MiB 💔 29.65%
Total 27.9492s 25.9819s 💚 -7.04% 2.26 GiB 2.40 GiB 💔 6.48% 2.71 GiB 3.51 GiB 💔 29.51%
Summary 1.0000s 0.9107s 💚 -8.93% 1 byte 1.05 bytes 💔 4.62% 1 byte 1.32 bytes 💔 32.14%

We do see the memory use regressions compared to Windows' allocator too.

@Zoxc Zoxc mentioned this pull request

Mar 21, 2025

@ognevny

just for my interest: does it work for windows-gnu too?

@Zoxc

I haven't tested that, but probably.

@Kobzol

bors added a commit to rust-lang-ci/rust that referenced this pull request

Mar 21, 2025

@bors

Use mimalloc as the global allocator on x86_64-pc-windows-msvc

This adds using mimalloc as the global allocator for the compiler as an option to bootstrap.toml and enables it for the x86_64-pc-windows-msvc dist builder.

try-job: dist-x86_64-msvc

@bors

@lqd

These benchmarks also seem pretty small, what's the result on a bigger project like cargo?

@bors

☀️ Try build successful - checks-actions
Build commit: b200834 (b200834f64a4cc752eb39d638ca76d023af0e45f)

Mark-Simulacrum

@@ -748,6 +748,12 @@
# [target.] section.
#jemalloc = false
# Use mimalloc as the global allocator for the compiler. Rust code will prefer `mimalloc` over
# `jemalloc` if that is also enabled.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reject setting both? That feels needlessly confusing to me and probably not hugely needed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably just remove jemalloc if mimalloc v3 solves all regressions over it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name this field override-allocators in case we'd want to swap allocators in the future?

@@ -70,6 +70,10 @@ use time::OffsetDateTime;
use time::macros::format_description;
use tracing::trace;
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just temporary, or the long-term state? As-is this doesn't affect LLVM's malloc calls?

IOW, why is this not changign the rustc/src/main.rs malloc overrides?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term. It's better than the linker hacks in main.rs.

Overriding the C allocator only in main.rs doesn't affect DLLs making it unsound without further work dealing with loadable backends and LLVM plugins.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't this still a regression for LLVM's allocations? It seems odd to measure perf for a halfway shift like this to mimalloc...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd still want the linker hacks on Linux / macOS for LLVM, using global_allocator just makes Rust allocation overriding less hacky.

As for perf on this PR, LLVM uses Windows' heap allocator both before and after.

@Kobzol

Nit: if we modify the bootstrap.toml config, I would go for allocator = jemalloc/mimalloc/default, rather than introducing another boolean option.

@klensy

Is there a noticeable difference with jemalloc on windows? Given, that it already exist but not enabled for windows.

@Zoxc

I haven't tried to make jemalloc work on Windows. I focused on mimalloc since it outperformed jemalloc on our Linux tests.