Skip to content

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

Draft
Zoxc wants to merge 2 commits intorust-lang:mainfrom
Zoxc:win-mimalloc
Draft

Use mimalloc as the allocator on x86_64-pc-windows-msvc#138764
Zoxc wants to merge 2 commits intorust-lang:mainfrom
Zoxc:win-mimalloc

Conversation

@Zoxc
Copy link
Copy Markdown
Contributor

@Zoxc Zoxc commented Mar 21, 2025

View all comments

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

ognevny commented Mar 21, 2025

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

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Mar 21, 2025

I haven't tested that, but probably.

@Kobzol
Copy link
Copy Markdown
Member

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
Copy Markdown
Collaborator

bors commented Mar 21, 2025

⌛ Trying commit 014c141 with merge b200834...

@lqd
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

bors commented Mar 21, 2025

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

Comment thread bootstrap.example.toml Outdated
#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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Comment thread compiler/rustc_driver_impl/src/lib.rs Outdated

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 12, 2025
@thynson
Copy link
Copy Markdown

thynson commented Jan 19, 2026

mimalloc v3.2.7 release candidate was just released, it should worth a try.

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 21, 2026

Some new benchmarks, jemalloc as baseline:

Benchmarkjemallocmimalloc v2mimalloc v3Windowsjemallocmimalloc v2mimalloc v3Windowsjemallocmimalloc v2mimalloc v3Windows
TimeTime%Time%Time%Physical MemoryPhysical Memory%Physical Memory%Physical Memory%Committed MemoryCommitted Memory%Committed Memory%Committed Memory%
🟣 clap:check0.9428s0.9305s💚 -1.31%0.9299s💚 -1.37%1.0440s💔 10.73%151.64 MiB154.88 MiB💔 2.14%158.93 MiB💔 4.80%151.06 MiB -0.38%202.14 MiB257.59 MiB💔 27.43%239.11 MiB💔 18.29%196.46 MiB💚 -2.81%
🟣 hyper:check0.1750s0.1705s💚 -2.57%0.1690s💚 -3.40%0.1882s💔 7.54%82.55 MiB82.90 MiB 0.42%79.99 MiB💚 -3.10%80.78 MiB💚 -2.15%133.20 MiB192.25 MiB💔 44.34%172.44 MiB💔 29.46%125.24 MiB💚 -5.97%
🟣 regex:check0.5275s0.5179s💚 -1.82%0.5194s💚 -1.54%0.5817s💔 10.27%113.82 MiB114.88 MiB 0.93%115.78 MiB💔 1.72%113.63 MiB -0.17%161.40 MiB220.15 MiB💔 36.40%201.84 MiB💔 25.06%155.33 MiB💚 -3.76%
🟣 syn:check0.8963s0.8804s💚 -1.77%0.8820s💚 -1.60%1.0105s💔 12.74%143.23 MiB145.20 MiB💔 1.38%147.24 MiB💔 2.80%142.97 MiB -0.18%189.70 MiB249.62 MiB💔 31.59%232.12 MiB💔 22.36%183.87 MiB💚 -3.07%
Total2.5416s2.4993s💚 -1.67%2.5002s💚 -1.63%2.8243s💔 11.12%491.25 MiB497.86 MiB💔 1.35%501.94 MiB💔 2.18%488.43 MiB -0.57%686.43 MiB919.62 MiB💔 33.97%845.51 MiB💔 23.17%660.89 MiB💚 -3.72%
Summary1.0000s0.9813s💚 -1.87%0.9802s💚 -1.98%1.1032s💔 10.32%1 byte1.01 bytes💔 1.21%1.02 bytes💔 1.56%0.99 bytes -0.72%1 byte1.35 bytes💔 34.94%1.24 bytes💔 23.79%0.96 bytes💚 -3.90%

@rustbot rustbot added A-CI Area: Our Github Actions CI A-meta Area: Issues & PRs about the rust-lang/rust repository itself labels Apr 22, 2026
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 22, 2026
@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 22, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 22, 2026
Use mimalloc as the global allocator on `x86_64-pc-windows-msvc`

try-job: dist-x86_64-msvc
@lqd
Copy link
Copy Markdown
Member

lqd commented Apr 22, 2026

Can you show the rustc-perf benchmarks results? The collector runs on windows, at least it used to.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 22, 2026

☀️ Try build successful (CI)
Build commit: 5a488dc (5a488dc27f433b972d507ccc4b6d6870e6d93859, parent: f9988fefd3add01f414f52b414308e7872622fee)

@teor2345
Copy link
Copy Markdown
Contributor

Some new benchmarks, jemalloc as baseline:

It's hidden on the right of the table, but is the memory usage increase a concern?

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 25, 2026

Can you show the rustc-perf benchmarks results?

I don't really have a setup for that / longer benches.

Release builds appear closer than last time:

Benchmarkjemallocmimalloc v2mimalloc v3Windowsjemallocmimalloc v2mimalloc v3Windowsjemallocmimalloc v2mimalloc v3Windows
TimeTime%Time%Time%Physical MemoryPhysical Memory%Physical Memory%Physical Memory%Committed MemoryCommitted Memory%Committed Memory%Committed Memory%
🔵 clap:release13.1544s13.2008s 0.35%13.2678s 0.86%13.2656s 0.85%222.30 MiB223.43 MiB 0.51%228.07 MiB💔 2.60%218.18 MiB💚 -1.85%262.32 MiB296.05 MiB💔 12.86%297.50 MiB💔 13.41%249.18 MiB💚 -5.01%
🔵 hyper:release0.6349s0.6324s -0.40%0.6288s -0.96%0.6496s💔 2.31%118.94 MiB119.07 MiB 0.11%116.67 MiB💚 -1.91%116.41 MiB💚 -2.13%157.46 MiB205.02 MiB💔 30.20%190.85 MiB💔 21.20%144.47 MiB💚 -8.25%
🔵 regex:release12.8749s12.8484s -0.21%12.9637s 0.69%12.9278s 0.41%230.58 MiB229.94 MiB -0.28%232.89 MiB 1.00%185.01 MiB💚 -19.77%224.86 MiB280.14 MiB💔 24.58%254.21 MiB💔 13.05%212.13 MiB💚 -5.66%
🔵 syn:release12.6447s12.7238s 0.63%12.7661s 0.96%12.7174s 0.57%193.25 MiB194.77 MiB 0.79%197.90 MiB💔 2.41%187.88 MiB💚 -2.77%230.56 MiB294.68 MiB💔 27.81%265.32 MiB💔 15.08%215.40 MiB💚 -6.58%
Total39.3090s39.4054s 0.25%39.6263s 0.81%39.5604s 0.64%765.07 MiB767.20 MiB 0.28%775.53 MiB💔 1.37%707.48 MiB💚 -7.53%875.20 MiB1.05 GiB💔 22.93%1007.88 MiB💔 15.16%821.18 MiB💚 -6.17%
Summary1.0000s1.0009s 0.09%1.0039s 0.39%1.0104s💔 1.04%1 byte1.00 bytes 0.28%1.01 bytes💔 1.02%0.93 bytes💚 -6.63%1 byte1.24 bytes💔 23.86%1.16 bytes💔 15.69%0.94 bytes💚 -6.37%

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 27, 2026

Test with -Zthreads=7. Here mimalloc extends its lead. mimalloc v3 commits more memory then in the single threaded case.

Benchmarkjemallocmimalloc v2mimalloc v3jemallocmimalloc v2mimalloc v3jemallocmimalloc v2mimalloc v3
TimeTime%Time%Physical MemoryPhysical Memory%Physical Memory%Committed MemoryCommitted Memory%Committed Memory%
🟣 clap:check0.3285s0.3191s💚 -2.87%0.3221s💚 -1.95%212.16 MiB217.39 MiB💔 2.47%218.00 MiB💔 2.75%312.75 MiB394.18 MiB💔 26.04%494.61 MiB💔 58.15%
🟣 hyper:check0.1162s0.1099s💚 -5.39%0.1107s💚 -4.76%131.52 MiB130.06 MiB💚 -1.11%128.35 MiB💚 -2.41%229.59 MiB309.76 MiB💔 34.92%366.73 MiB💔 59.73%
🟣 regex:check0.2214s0.2130s💚 -3.80%0.2149s💚 -2.93%170.60 MiB171.31 MiB 0.42%168.65 MiB💚 -1.14%259.93 MiB357.67 MiB💔 37.60%427.03 MiB💔 64.29%
🟣 syn:check0.3823s0.3693s💚 -3.41%0.3744s💚 -2.07%205.62 MiB208.04 MiB💔 1.17%203.95 MiB -0.81%294.04 MiB363.24 MiB💔 23.53%475.20 MiB💔 61.61%
Total1.0484s1.0113s💚 -3.54%1.0221s💚 -2.51%719.89 MiB726.81 MiB 0.96%718.94 MiB -0.13%1.07 GiB1.39 GiB💔 29.97%1.72 GiB💔 60.87%
Summary1.0000s0.9613s💚 -3.87%0.9707s💚 -2.93%1 byte1.01 bytes 0.74%1.00 bytes -0.40%1 byte1.31 bytes💔 30.52%1.61 bytes💔 60.95%

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 28, 2026

Here's a test with the C allocator also overridden. It reduces the memory regression down to 11% which is reasonable for a -7.6% runtime reduction.

BenchmarkWindowsmimalloc v3 (Rust)mimalloc v3 (Rust + C)Windowsmimalloc v3 (Rust)mimalloc v3 (Rust + C)Windowsmimalloc v3 (Rust)mimalloc v3 (Rust + C)
TimeTime%Time%Physical MemoryPhysical Memory%Physical Memory%Committed MemoryCommitted Memory%Committed Memory%
🔵 regex:release13.0406s12.9062s💚 -1.03%12.0511s💚 -7.59%185.63 MiB233.16 MiB💔 25.61%206.12 MiB💔 11.04%213.26 MiB256.50 MiB💔 20.28%309.51 MiB💔 45.13%
Total13.0406s12.9062s💚 -1.03%12.0511s💚 -7.59%185.63 MiB233.16 MiB💔 25.61%206.12 MiB💔 11.04%213.26 MiB256.50 MiB💔 20.28%309.51 MiB💔 45.13%
Summary1.0000s0.9897s💚 -1.03%0.9241s💚 -7.59%1 byte1.26 bytes💔 25.61%1.11 bytes💔 11.04%1 byte1.20 bytes💔 20.28%1.45 bytes💔 45.13%

@Zoxc Zoxc changed the title Use mimalloc as the global allocator on x86_64-pc-windows-msvc Use mimalloc as the allocator on x86_64-pc-windows-msvc Apr 30, 2026
@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 30, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 30, 2026
Use mimalloc as the allocator on `x86_64-pc-windows-msvc`

try-job: dist-x86_64-msvc
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 30, 2026

💔 Test for 18f6c32 failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 30, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 30, 2026
Use mimalloc as the allocator on `x86_64-pc-windows-msvc`

try-job: dist-x86_64-msvc
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 30, 2026

💔 Test for ea0be1e failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@Zoxc
Copy link
Copy Markdown
Contributor Author

Zoxc commented Apr 30, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 30, 2026
Use mimalloc as the allocator on `x86_64-pc-windows-msvc`

try-job: dist-x86_64-msvc
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 30, 2026

☀️ Try build successful (CI)
Build commit: 4b70614 (4b70614536b9ae0383a266439aa75880163c740c, parent: f53b654a8882fd5fc036c4ca7a4ff41ce32497a6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CI Area: Our Github Actions CI A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-rustc-dev-guide Area: rustc-dev-guide A-testsuite Area: The testsuite used to check the correctness of rustc A-tidy Area: The tidy tool S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-clippy Relevant to the Clippy team. 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.