Skip to content

[Experiment] Replace jemalloc with mimalloc beta branch #92317

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ jobs:
- name: dist-x86_64-apple
env:
SCRIPT: "./x.py dist --exclude rust-docs --exclude extended && ./x.py dist --target=x86_64-apple-darwin rust-docs && ./x.py dist extended"
RUST_CONFIGURE_ARGS: "--host=x86_64-apple-darwin --target=x86_64-apple-darwin,aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUST_CONFIGURE_ARGS: "--host=x86_64-apple-darwin --target=x86_64-apple-darwin,aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim --enable-full-tools --enable-sanitizers --enable-profiler --set rust.mimalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
NO_LLVM_ASSERTIONS: 1
Expand All @@ -299,7 +299,7 @@ jobs:
- name: dist-x86_64-apple-alt
env:
SCRIPT: "./x.py dist"
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.mimalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
NO_LLVM_ASSERTIONS: 1
Expand All @@ -309,7 +309,7 @@ jobs:
- name: x86_64-apple
env:
SCRIPT: "./x.py --stage 2 test"
RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.mimalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.8
MACOSX_STD_DEPLOYMENT_TARGET: 10.7
Expand All @@ -320,7 +320,7 @@ jobs:
- name: dist-aarch64-apple
env:
SCRIPT: "./x.py dist --stage 2"
RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --host=aarch64-apple-darwin --target=aarch64-apple-darwin --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --host=aarch64-apple-darwin --target=aarch64-apple-darwin --enable-full-tools --enable-sanitizers --enable-profiler --set rust.mimalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
USE_XCODE_CLANG: 1
MACOSX_DEPLOYMENT_TARGET: 11.0
Expand All @@ -329,7 +329,6 @@ jobs:
NO_DEBUG_ASSERTIONS: 1
NO_OVERFLOW_CHECKS: 1
DIST_REQUIRE_ALL_TOOLS: 1
JEMALLOC_SYS_WITH_LG_PAGE: 14
os: macos-latest
- name: x86_64-msvc-1
env:
Expand Down
39 changes: 10 additions & 29 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1238,12 +1238,6 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcd1163ae48bda72a20ae26d66a04d3094135cadab911cff418ae5e33f253431"

[[package]]
name = "fs_extra"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"

[[package]]
name = "fst"
version = "0.4.5"
Expand Down Expand Up @@ -2243,6 +2237,15 @@ dependencies = [
"syn",
]

[[package]]
name = "mimallocate-sys"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a60aa763b2d53b874b186ecd188c6241b9a8eb8b1deae51190d68915e99d90"
dependencies = [
"cc",
]

[[package]]
name = "minifier"
version = "0.0.41"
Expand Down Expand Up @@ -3479,10 +3482,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
name = "rustc-main"
version = "0.0.0"
dependencies = [
"mimallocate-sys",
"rustc_codegen_ssa",
"rustc_driver",
"tikv-jemalloc-sys",
"tikv-jemallocator",
]

[[package]]
Expand Down Expand Up @@ -5303,27 +5305,6 @@ dependencies = [
name = "tier-check"
version = "0.1.0"

[[package]]
name = "tikv-jemalloc-sys"
version = "0.4.1+5.2.1-patched"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a26331b05179d4cb505c8d6814a7e18d298972f0a551b0e3cefccff927f86d3"
dependencies = [
"cc",
"fs_extra",
"libc",
]

[[package]]
name = "tikv-jemallocator"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c14a5a604eb8715bc5785018a37d00739b180bcf609916ddf4393d33d49ccdf"
dependencies = [
"libc",
"tikv-jemalloc-sys",
]

[[package]]
name = "time"
version = "0.1.43"
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ rustc_driver = { path = "../rustc_driver" }
# crate is intended to be used by codegen backends, which may not be in-tree.
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }

[dependencies.tikv-jemalloc-sys]
version = '0.4.0'
optional = true
features = ['unprefixed_malloc_on_supported_platforms']

[dependencies.tikv-jemallocator]
version = '0.4.0'
[dependencies.mimallocate-sys]
version = '0.1.1'
optional = true

[features]
jemalloc = ['tikv-jemalloc-sys', 'tikv-jemallocator']
mimalloc = ['mimallocate-sys']
llvm = ['rustc_driver/llvm']
max_level_info = ['rustc_driver/max_level_info']
47 changes: 18 additions & 29 deletions compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,52 @@
// A note about jemalloc: rustc uses jemalloc when built for CI and
// A note about mimalloc: rustc uses mimalloc when built for CI and
// distribution. The obvious way to do this is with the `#[global_allocator]`
// mechanism. However, for complicated reasons (see
// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
// details) that mechanism doesn't work here. Also, we must use a consistent
// allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
// wouldn't provide that.
//
// Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a
// way such that jemalloc's implementation of `malloc`, `free`, etc., override
// Instead, we use a lower-level mechanism. rustc is linked with mimalloc in a
// way such that mimalloc's implementation of `malloc`, `free`, etc., override
// the libc allocator's implementation. This means that Rust's `System`
// allocator, which calls `libc::malloc()` et al., is actually calling into
// jemalloc.
//
// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
// provides an impl of that trait, which is called `Jemalloc`) is that we
// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
// It's unclear how much performance is lost because of this.
// mimalloc.
//
// As for the symbol overrides in `main` below: we're pulling in a static copy
// of jemalloc. We need to actually reference its symbols for it to get linked.
// of mimalloc. We need to actually reference its symbols for it to get linked.
// The two crates we link to here, `std` and `rustc_driver`, are both dynamic
// libraries. So we must reference jemalloc symbols one way or another, because
// libraries. So we must reference mimalloc symbols one way or another, because
// this file is the only object code in the rustc executable.
#[cfg(feature = "tikv-jemalloc-sys")]
use tikv_jemalloc_sys as jemalloc_sys;

fn main() {
// See the comment at the top of this file for an explanation of this.
#[cfg(feature = "tikv-jemalloc-sys")]
#[cfg(feature = "mimallocate-sys")]
{
use std::os::raw::{c_int, c_void};

#[used]
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = mimallocate_sys::mi_calloc;
#[used]
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
jemalloc_sys::posix_memalign;
mimallocate_sys::mi_posix_memalign;
#[used]
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void =
mimallocate_sys::mi_aligned_alloc;
#[used]
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = mimallocate_sys::mi_malloc;
#[used]
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
mimallocate_sys::mi_realloc;
#[used]
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
static _F6: unsafe extern "C" fn(*mut c_void) = mimallocate_sys::mi_free;

// On OSX, jemalloc doesn't directly override malloc/free, but instead
// On OSX, mimalloc doesn't directly override malloc/free, but instead
// registers itself with the allocator's zone APIs in a ctor. However,
// the linker doesn't seem to consider ctors as "used" when statically
// linking, so we need to explicitly depend on the function.
#[cfg(target_os = "macos")]
{
extern "C" {
fn _rjem_je_zone_register();
}

#[used]
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
}
#[used]
static _F7: unsafe extern "C" fn() = mimallocate_sys::_mi_macos_override_malloc;
}

rustc_driver::set_sigpipe_handler();
Expand Down
4 changes: 2 additions & 2 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ changelog-seen = 2
# Map debuginfo paths to `/rust/$sha/...`, generally only set for releases
#remap-debuginfo = false

# Link the compiler against `jemalloc`, where on Linux and OSX it should
# Link the compiler against `mimalloc`, where on Linux and OSX it should
# override the default allocator for rustc and LLVM.
#jemalloc = false
#mimalloc = false

# Run tests in various test suites with the "nll compare mode" in addition to
# running the tests in normal mode. Largely only used on CI and during local
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub struct Config {
pub hosts: Vec<TargetSelection>,
pub targets: Vec<TargetSelection>,
pub local_rebuild: bool,
pub jemalloc: bool,
pub mimalloc: bool,
pub control_flow_guard: bool,

// dist misc
Expand Down Expand Up @@ -512,7 +512,7 @@ struct Rust {
verify_llvm_ir: Option<bool>,
thin_lto_import_instr_limit: Option<u32>,
remap_debuginfo: Option<bool>,
jemalloc: Option<bool>,
mimalloc: Option<bool>,
test_compare_mode: Option<bool>,
llvm_libunwind: Option<String>,
control_flow_guard: Option<bool>,
Expand Down Expand Up @@ -878,7 +878,7 @@ impl Config {
set(&mut config.rust_optimize_tests, rust.optimize_tests);
set(&mut config.codegen_tests, rust.codegen_tests);
set(&mut config.rust_rpath, rust.rpath);
set(&mut config.jemalloc, rust.jemalloc);
set(&mut config.mimalloc, rust.mimalloc);
set(&mut config.test_compare_mode, rust.test_compare_mode);
config.llvm_libunwind = rust
.llvm_libunwind
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@ impl Build {
/// Gets the space-separated set of activated features for the compiler.
fn rustc_features(&self) -> String {
let mut features = String::new();
if self.config.jemalloc {
features.push_str("jemalloc");
if self.config.mimalloc {
features.push_str("mimalloc");
}
if self.config.llvm_enabled() {
features.push_str(" llvm");
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ impl Step for Rustdoc {
// to build rustdoc.
//
let mut features = Vec::new();
if builder.config.jemalloc {
features.push("jemalloc".to_string());
if builder.config.mimalloc {
features.push("mimalloc".to_string());
}

let cargo = prepare_tool_cargo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ RUN sh /scripts/sccache.sh
ENV HOST=x86_64-unknown-haiku
ENV TARGET=target.$HOST

ENV RUST_CONFIGURE_ARGS --disable-jemalloc \
--set=$TARGET.cc=x86_64-unknown-haiku-gcc \
ENV RUST_CONFIGURE_ARGS --set=$TARGET.cc=x86_64-unknown-haiku-gcc \
--set=$TARGET.cxx=x86_64-unknown-haiku-g++ \
--set=$TARGET.llvm-config=/bin/llvm-config-haiku
ENV SCRIPT python3 ../x.py dist --host=$HOST --target=$HOST
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/dist-i686-linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ENV RUST_CONFIGURE_ARGS \
--set target.i686-unknown-linux-gnu.linker=clang \
--build=i686-unknown-linux-gnu \
--set llvm.ninja=false \
--set rust.jemalloc
--set rust.mimalloc
ENV SCRIPT python3 ../x.py dist --build $HOSTS --host $HOSTS --target $HOSTS
ENV CARGO_TARGET_I686_UNKNOWN_LINUX_GNU_LINKER=clang

Expand Down
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ ENV RUST_CONFIGURE_ARGS \
--set target.x86_64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \
--set llvm.thin-lto=true \
--set llvm.ninja=false \
--set rust.jemalloc
--set rust.mimalloc
ENV SCRIPT ../src/ci/pgo.sh python3 ../x.py dist \
--host $HOSTS --target $HOSTS \
--include-default-paths \
Expand Down
16 changes: 4 additions & 12 deletions src/ci/github-actions/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ jobs:
- name: dist-x86_64-apple
env:
SCRIPT: ./x.py dist --exclude rust-docs --exclude extended && ./x.py dist --target=x86_64-apple-darwin rust-docs && ./x.py dist extended
RUST_CONFIGURE_ARGS: --host=x86_64-apple-darwin --target=x86_64-apple-darwin,aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false
RUST_CONFIGURE_ARGS: --host=x86_64-apple-darwin --target=x86_64-apple-darwin,aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim --enable-full-tools --enable-sanitizers --enable-profiler --set rust.mimalloc --set llvm.ninja=false
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
NO_LLVM_ASSERTIONS: 1
Expand All @@ -465,7 +465,7 @@ jobs:
- name: dist-x86_64-apple-alt
env:
SCRIPT: ./x.py dist
RUST_CONFIGURE_ARGS: --enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false
RUST_CONFIGURE_ARGS: --enable-extended --enable-profiler --set rust.mimalloc --set llvm.ninja=false
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
NO_LLVM_ASSERTIONS: 1
Expand All @@ -476,7 +476,7 @@ jobs:
- name: x86_64-apple
env:
SCRIPT: ./x.py --stage 2 test
RUST_CONFIGURE_ARGS: --build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false
RUST_CONFIGURE_ARGS: --build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.mimalloc --set llvm.ninja=false
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.8
MACOSX_STD_DEPLOYMENT_TARGET: 10.7
Expand All @@ -496,7 +496,7 @@ jobs:
--enable-full-tools
--enable-sanitizers
--enable-profiler
--set rust.jemalloc
--set rust.mimalloc
--set llvm.ninja=false
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
USE_XCODE_CLANG: 1
Expand All @@ -506,14 +506,6 @@ jobs:
NO_DEBUG_ASSERTIONS: 1
NO_OVERFLOW_CHECKS: 1
DIST_REQUIRE_ALL_TOOLS: 1
# Corresponds to 16K page size
#
# Shouldn't be needed if jemalloc-sys is updated to
# handle this platform like iOS or if we build on
# aarch64-apple-darwin itself.
#
# https://github.com/gnzlbg/jemallocator/blob/c27a859e98e3cb790dc269773d9da71a1e918458/jemalloc-sys/build.rs#L237
JEMALLOC_SYS_WITH_LG_PAGE: 14
<<: *job-macos-xl

######################
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
expect-test = "1.0"

[features]
jemalloc = []
mimalloc = []

[package.metadata.rust-analyzer]
rustc_private = true
Loading