|
1 | | -// Configure jemalloc as the `global_allocator` when configured. This is |
2 | | -// so that we use the sized deallocation apis jemalloc provides |
3 | | -// (namely `sdallocx`). |
| 1 | +// A note about jemalloc: rustc uses jemalloc when built for CI and |
| 2 | +// distribution. The obvious way to do this is with the `#[global_allocator]` |
| 3 | +// mechanism. However, for complicated reasons (see |
| 4 | +// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some |
| 5 | +// details) that mechanism doesn't work here. Also, we must use a consistent |
| 6 | +// allocator across the rustc <-> llvm boundary, and `#[global_allocator]` |
| 7 | +// wouldn't provide that. |
4 | 8 | // |
5 | | -// The symbol overrides documented below are also performed so that we can |
6 | | -// ensure that we use a consistent allocator across the rustc <-> llvm boundary |
7 | | -#[cfg(feature = "jemalloc")] |
8 | | -#[global_allocator] |
9 | | -static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; |
10 | | - |
| 9 | +// Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a |
| 10 | +// way such that jemalloc's implementation of `malloc`, `free`, etc., override |
| 11 | +// the libc allocator's implementation. This means that Rust's `System` |
| 12 | +// allocator, which calls `libc::malloc()` et al., is actually calling into |
| 13 | +// jemalloc. |
| 14 | +// |
| 15 | +// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate |
| 16 | +// provides an impl of that trait, which is called `Jemalloc`) is that we |
| 17 | +// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides. |
| 18 | +// It's unclear how much performance is lost because of this. |
| 19 | +// |
| 20 | +// As for the symbol overrides in `main` below: we're pulling in a static copy |
| 21 | +// of jemalloc. We need to actually reference its symbols for it to get linked. |
| 22 | +// The two crates we link to here, `std` and `rustc_driver`, are both dynamic |
| 23 | +// libraries. So we must reference jemalloc symbols one way or another, because |
| 24 | +// this file is the only object code in the rustc executable. |
11 | 25 | #[cfg(feature = "tikv-jemalloc-sys")] |
12 | 26 | use tikv_jemalloc_sys as jemalloc_sys; |
13 | 27 |
|
14 | 28 | fn main() { |
15 | | - // Pull in jemalloc when enabled. |
16 | | - // |
17 | | - // Note that we're pulling in a static copy of jemalloc which means that to |
18 | | - // pull it in we need to actually reference its symbols for it to get |
19 | | - // linked. The two crates we link to here, std and rustc_driver, are both |
20 | | - // dynamic libraries. That means to pull in jemalloc we actually need to |
21 | | - // reference allocation symbols one way or another (as this file is the only |
22 | | - // object code in the rustc executable). |
| 29 | + // See the comment at the top of this file for an explanation of this. |
23 | 30 | #[cfg(feature = "tikv-jemalloc-sys")] |
24 | 31 | { |
25 | 32 | use std::os::raw::{c_int, c_void}; |
|
0 commit comments