Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 21, 2025

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

@rustbot
Copy link
Collaborator

rustbot commented Mar 21, 2025

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
Copy link
Contributor Author

Zoxc commented Mar 21, 2025

Local benchmarks:

BenchmarkBeforeAfterBeforeAfterBeforeAfter
TimeTime%Physical MemoryPhysical Memory%Committed MemoryCommitted Memory%
🟣 clap:check1.4133s1.2131s💚 -14.17%147.10 MiB147.52 MiB 0.28%200.42 MiB261.79 MiB💔 30.62%
🔵 clap:release5.7162s5.4820s💚 -4.10%285.77 MiB289.07 MiB💔 1.15%324.74 MiB384.72 MiB💔 18.47%
🟠 clap:debug3.2341s2.9639s💚 -8.35%289.52 MiB305.29 MiB💔 5.44%331.62 MiB394.30 MiB💔 18.90%
🟣 hyper:check0.2368s0.2082s💚 -12.07%80.00 MiB80.85 MiB💔 1.06%130.63 MiB195.31 MiB💔 49.51%
🔵 hyper:release0.8620s0.8324s💚 -3.42%100.95 MiB103.28 MiB💔 2.31%144.99 MiB201.68 MiB💔 39.10%
🟠 hyper:debug0.4746s0.4364s💚 -8.05%113.33 MiB115.87 MiB💔 2.24%152.39 MiB213.92 MiB💔 40.38%
🟣 regex:check0.8054s0.6930s💚 -13.96%108.49 MiB108.13 MiB -0.34%155.61 MiB223.32 MiB💔 43.51%
🔵 regex:release5.2164s5.0395s💚 -3.39%311.05 MiB389.23 MiB💔 25.13%294.30 MiB428.30 MiB💔 45.53%
🟠 regex:debug1.8545s1.7079s💚 -7.91%239.77 MiB242.05 MiB 0.95%275.22 MiB324.94 MiB💔 18.07%
🟣 syn:check1.3455s1.1368s💚 -15.51%142.38 MiB141.26 MiB -0.78%190.93 MiB255.30 MiB💔 33.72%
🔵 syn:release4.2406s3.9707s💚 -6.37%256.63 MiB296.60 MiB💔 15.58%295.81 MiB349.53 MiB💔 18.16%
🟠 syn:debug2.5499s2.2981s💚 -9.87%236.51 MiB242.18 MiB💔 2.39%276.62 MiB358.63 MiB💔 29.65%
Total27.9492s25.9819s💚 -7.04%2.26 GiB2.40 GiB💔 6.48%2.71 GiB3.51 GiB💔 29.51%
Summary1.0000s0.9107s💚 -8.93%1 byte1.05 bytes💔 4.62%1 byte1.32 bytes💔 32.14%

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

@ognevny
Copy link
Contributor

ognevny commented Mar 21, 2025

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

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 21, 2025

I haven't tested that, but probably.

@Kobzol
Copy link
Contributor

Kobzol commented Mar 21, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 21, 2025
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
Copy link
Contributor

bors commented Mar 21, 2025

⌛ Trying commit 014c141 with merge b200834...

@lqd
Copy link
Member

lqd commented Mar 21, 2025

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

@bors
Copy link
Contributor

bors commented Mar 21, 2025

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

@@ -748,6 +748,12 @@
# [target.<tuple>] section.
#jemalloc = false

# Use mimalloc as the global allocator for the compiler. Rust code will prefer `mimalloc` over
# `jemalloc` if that is also enabled.
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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;
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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...

Copy link
Contributor Author

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
Copy link
Contributor

Kobzol commented Mar 23, 2025

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

@klensy
Copy link
Contributor

klensy commented Mar 23, 2025

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

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 24, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants