Skip to content

Commit bd4cd7e

Browse files
committed
Auto merge of #148896 - Kobzol:revert-146627, r=madsmtm
Revert "Rollup merge of #146627 - madsmtm:jemalloc-simplify, r=jdonszelmann" This reverts commit 5dc3c19417d0fbfd979c5a1ecd8bebe2d8d9110e, reversing changes made to 11339a0ef5ed586bb7ea4f85a9b7287880caac3a. Reverts rust-lang/rust#146627 due to a [perf regression](rust-lang/rust#148851 (comment)). r? `@Zalathar`
2 parents c9b1387 + 83b09aa commit bd4cd7e

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ serde_json = { version = "1.0", optional = true }
3333
# But only for some targets, it fails for others. Rustc configures this in its CI, but we can't
3434
# easily use that since we support of-tree builds.
3535
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies.tikv-jemalloc-sys]
36-
version = "0.6.1"
37-
features = ['override_allocator_on_supported_platforms']
36+
version = "0.6.0"
37+
features = ['unprefixed_malloc_on_supported_platforms']
3838

3939
[target.'cfg(unix)'.dependencies]
4040
libc = "0.2"

src/bin/miri.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ extern crate rustc_middle;
2020
extern crate rustc_session;
2121
extern crate rustc_span;
2222

23-
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
24-
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
25-
#[cfg(any(target_os = "linux", target_os = "macos"))]
26-
use tikv_jemalloc_sys as _;
27-
2823
mod log;
2924

3025
use std::env;
@@ -397,7 +392,48 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
397392
Ok(from..to)
398393
}
399394

395+
#[cfg(any(target_os = "linux", target_os = "macos"))]
396+
fn jemalloc_magic() {
397+
// These magic runes are copied from
398+
// <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
399+
// See there for further comments.
400+
use std::os::raw::{c_int, c_void};
401+
402+
use tikv_jemalloc_sys as jemalloc_sys;
403+
404+
#[used]
405+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
406+
#[used]
407+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
408+
jemalloc_sys::posix_memalign;
409+
#[used]
410+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
411+
#[used]
412+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
413+
#[used]
414+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
415+
#[used]
416+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
417+
418+
// On OSX, jemalloc doesn't directly override malloc/free, but instead
419+
// registers itself with the allocator's zone APIs in a ctor. However,
420+
// the linker doesn't seem to consider ctors as "used" when statically
421+
// linking, so we need to explicitly depend on the function.
422+
#[cfg(target_os = "macos")]
423+
{
424+
unsafe extern "C" {
425+
fn _rjem_je_zone_register();
426+
}
427+
428+
#[used]
429+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
430+
}
431+
}
432+
400433
fn main() {
434+
#[cfg(any(target_os = "linux", target_os = "macos"))]
435+
jemalloc_magic();
436+
401437
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
402438

403439
// Snapshot a copy of the environment before `rustc` starts messing with it.

0 commit comments

Comments
 (0)