2024-03-12 01:12:28 +00:00
|
|
|
// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
|
2021-12-23 04:47:32 +00:00
|
|
|
// A note about jemalloc: rustc uses jemalloc 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.
|
2021-04-04 21:07:56 +00:00
|
|
|
//
|
2021-12-23 04:47:32 +00:00
|
|
|
// 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
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// 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
|
|
|
|
// this file is the only object code in the rustc executable.
|
2023-03-11 13:49:22 +00:00
|
|
|
//
|
|
|
|
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
|
|
|
|
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
|
|
|
|
// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
|
|
|
|
//
|
|
|
|
// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
|
|
|
|
// to compare their performance, see
|
2023-03-11 16:53:47 +00:00
|
|
|
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
|
2023-03-11 13:49:22 +00:00
|
|
|
// for an example of how to do so.
|
2021-04-04 21:07:56 +00:00
|
|
|
|
2019-01-02 21:23:45 +00:00
|
|
|
fn main() {
|
2021-12-23 04:47:32 +00:00
|
|
|
// See the comment at the top of this file for an explanation of this.
|
2024-07-15 17:01:20 +00:00
|
|
|
#[cfg(feature = "jemalloc")]
|
2019-01-02 21:23:45 +00:00
|
|
|
{
|
|
|
|
use std::os::raw::{c_int, c_void};
|
2015-11-19 23:20:12 +00:00
|
|
|
|
2019-01-02 21:23:45 +00:00
|
|
|
#[used]
|
|
|
|
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
|
|
|
|
#[used]
|
|
|
|
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
|
|
|
|
jemalloc_sys::posix_memalign;
|
|
|
|
#[used]
|
|
|
|
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
|
|
|
|
#[used]
|
|
|
|
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
|
|
|
|
#[used]
|
|
|
|
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
|
|
|
|
#[used]
|
|
|
|
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
|
2021-02-28 19:50:03 +00:00
|
|
|
|
|
|
|
// On OSX, jemalloc 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]
|
2021-03-05 12:12:02 +00:00
|
|
|
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
|
2021-02-28 19:50:03 +00:00
|
|
|
}
|
2019-01-02 21:23:45 +00:00
|
|
|
}
|
2018-12-19 16:27:23 +00:00
|
|
|
|
2018-04-10 22:55:41 +00:00
|
|
|
rustc_driver::main()
|
|
|
|
}
|