Skip to content

Commit bb23bfc

Browse files
committed
Remove useless #[global_allocator] from rustc and rustdoc.
This was added in #83152, which has several errors in its comments. This commit also fix up the comments, which are quite wrong and misleading.
1 parent e95e084 commit bb23bfc

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

compiler/rustc/src/main.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
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.
48
//
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.
1125
#[cfg(feature = "tikv-jemalloc-sys")]
1226
use tikv_jemalloc_sys as jemalloc_sys;
1327

1428
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.
2330
#[cfg(feature = "tikv-jemalloc-sys")]
2431
{
2532
use std::os::raw::{c_int, c_void};

src/librustdoc/lib.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ extern crate rustc_trait_selection;
6363
extern crate rustc_typeck;
6464
extern crate test;
6565

66+
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
67+
// about jemalloc.
6668
#[cfg(feature = "jemalloc")]
6769
extern crate tikv_jemalloc_sys;
6870
#[cfg(feature = "jemalloc")]
6971
use tikv_jemalloc_sys as jemalloc_sys;
70-
#[cfg(feature = "jemalloc")]
71-
extern crate tikv_jemallocator;
72-
#[cfg(feature = "jemalloc")]
73-
use tikv_jemallocator as jemallocator;
7472

7573
use std::default::Default;
7674
use std::env;
@@ -125,15 +123,9 @@ mod visit;
125123
mod visit_ast;
126124
mod visit_lib;
127125

128-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
129-
// about jemallocator
130-
#[cfg(feature = "jemalloc")]
131-
#[global_allocator]
132-
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
133-
134126
pub fn main() {
135127
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
136-
// about jemalloc-sys
128+
// about jemalloc.
137129
#[cfg(feature = "jemalloc")]
138130
{
139131
use std::os::raw::{c_int, c_void};
@@ -152,10 +144,6 @@ pub fn main() {
152144
#[used]
153145
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
154146

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

0 commit comments

Comments
 (0)