Skip to content

Commit 15a8fbc

Browse files
committed
Simplify jemalloc setup
1 parent 8a1b399 commit 15a8fbc

File tree

6 files changed

+25
-164
lines changed

6 files changed

+25
-164
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5490,8 +5490,6 @@ version = "0.1.0"
54905490
[[package]]
54915491
name = "tikv-jemalloc-sys"
54925492
version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
5493-
source = "registry+https://github.com/rust-lang/crates.io-index"
5494-
checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d"
54955493
dependencies = [
54965494
"cc",
54975495
"libc",

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ codegen-units = 1
9191

9292
# If you want to use a crate with local modifications, you can set a path or git dependency here.
9393
# For git dependencies, also add your source to ALLOWED_SOURCES in src/tools/tidy/src/extdeps.rs.
94-
#[patch.crates-io]
95-
94+
[patch.crates-io]
95+
tikv-jemalloc-sys = { path = "../jemallocator/jemalloc-sys" }

compiler/rustc/src/main.rs

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@
77
// distribution. The obvious way to do this is with the `#[global_allocator]`
88
// mechanism. However, for complicated reasons (see
99
// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
10-
// details) that mechanism doesn't work here. Also, we must use a consistent
11-
// allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
12-
// wouldn't provide that.
10+
// details) that mechanism doesn't work here. Also, we'd like to use a
11+
// consistent allocator across the rustc <-> llvm boundary, and
12+
// `#[global_allocator]` wouldn't provide that.
1313
//
14-
// Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a
15-
// way such that jemalloc's implementation of `malloc`, `free`, etc., override
16-
// the libc allocator's implementation. This means that Rust's `System`
17-
// allocator, which calls `libc::malloc()` et al., is actually calling into
18-
// jemalloc.
14+
// Instead, we use a lower-level mechanism, namely the
15+
// `"unprefixed_malloc_on_supported_platforms"` Cargo feature of jemalloc-sys.
16+
//
17+
// This makes jemalloc-sys override the libc/system allocator's implementation
18+
// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
19+
// calls `libc::malloc()` et al., is actually calling into jemalloc.
1920
//
2021
// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
2122
// provides an impl of that trait, which is called `Jemalloc`) is that we
2223
// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
2324
// It's unclear how much performance is lost because of this.
2425
//
25-
// As for the symbol overrides in `main` below: we're pulling in a static copy
26-
// of jemalloc. We need to actually reference its symbols for it to get linked.
27-
// The two crates we link to here, `std` and `rustc_driver`, are both dynamic
28-
// libraries. So we must reference jemalloc symbols one way or another, because
29-
// this file is the only object code in the rustc executable.
30-
//
3126
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
3227
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
3328
// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
@@ -36,43 +31,9 @@
3631
// to compare their performance, see
3732
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
3833
// for an example of how to do so.
34+
#[cfg(feature = "jemalloc")]
35+
use tikv_jemalloc_sys as _;
3936

4037
fn main() {
41-
// See the comment at the top of this file for an explanation of this.
42-
#[cfg(feature = "jemalloc")]
43-
{
44-
use std::os::raw::{c_int, c_void};
45-
46-
use tikv_jemalloc_sys as jemalloc_sys;
47-
48-
#[used]
49-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
50-
#[used]
51-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
52-
jemalloc_sys::posix_memalign;
53-
#[used]
54-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
55-
#[used]
56-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
57-
#[used]
58-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
59-
#[used]
60-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
61-
62-
// On OSX, jemalloc doesn't directly override malloc/free, but instead
63-
// registers itself with the allocator's zone APIs in a ctor. However,
64-
// the linker doesn't seem to consider ctors as "used" when statically
65-
// linking, so we need to explicitly depend on the function.
66-
#[cfg(target_os = "macos")]
67-
{
68-
unsafe extern "C" {
69-
fn _rjem_je_zone_register();
70-
}
71-
72-
#[used]
73-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
74-
}
75-
}
76-
7738
rustc_driver::main()
7839
}

src/librustdoc/lib.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ extern crate rustc_target;
6161
extern crate rustc_trait_selection;
6262
extern crate test;
6363

64-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
65-
// about jemalloc.
66-
#[cfg(feature = "jemalloc")]
67-
extern crate tikv_jemalloc_sys as jemalloc_sys;
68-
6964
use std::env::{self, VarError};
7065
use std::io::{self, IsTerminal};
7166
use std::path::Path;
@@ -77,6 +72,10 @@ use rustc_interface::interface;
7772
use rustc_middle::ty::TyCtxt;
7873
use rustc_session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option};
7974
use rustc_session::{EarlyDiagCtxt, getopts};
75+
/// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
76+
/// for why we need this `use` statement.
77+
#[cfg(feature = "jemalloc")]
78+
use tikv_jemalloc_sys as _;
8079
use tracing::info;
8180

8281
use crate::clean::utils::DOC_RUST_LANG_ORG_VERSION;
@@ -124,37 +123,6 @@ mod visit_ast;
124123
mod visit_lib;
125124

126125
pub fn main() {
127-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
128-
// about jemalloc.
129-
#[cfg(feature = "jemalloc")]
130-
{
131-
use std::os::raw::{c_int, c_void};
132-
133-
#[used]
134-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
135-
#[used]
136-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
137-
jemalloc_sys::posix_memalign;
138-
#[used]
139-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
140-
#[used]
141-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
142-
#[used]
143-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
144-
#[used]
145-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
146-
147-
#[cfg(target_os = "macos")]
148-
{
149-
unsafe extern "C" {
150-
fn _rjem_je_zone_register();
151-
}
152-
153-
#[used]
154-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
155-
}
156-
}
157-
158126
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
159127

160128
rustc_driver::install_ice_hook(

src/tools/clippy/src/driver.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ extern crate rustc_interface;
1313
extern crate rustc_session;
1414
extern crate rustc_span;
1515

16-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
17-
// about jemalloc.
16+
/// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
17+
/// for why we need this `use` statement.
1818
#[cfg(feature = "jemalloc")]
19-
extern crate tikv_jemalloc_sys as jemalloc_sys;
19+
use tikv_jemalloc_sys as _;
2020

2121
use clippy_utils::sym;
2222
use declare_clippy_lint::LintListBuilder;
@@ -192,36 +192,6 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/ne
192192
#[allow(clippy::too_many_lines)]
193193
#[allow(clippy::ignored_unit_patterns)]
194194
pub fn main() {
195-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
196-
// about jemalloc.
197-
#[cfg(feature = "jemalloc")]
198-
{
199-
use std::os::raw::{c_int, c_void};
200-
201-
#[used]
202-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
203-
#[used]
204-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = jemalloc_sys::posix_memalign;
205-
#[used]
206-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
207-
#[used]
208-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
209-
#[used]
210-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
211-
#[used]
212-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
213-
214-
#[cfg(target_os = "macos")]
215-
{
216-
unsafe extern "C" {
217-
fn _rjem_je_zone_register();
218-
}
219-
220-
#[used]
221-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
222-
}
223-
}
224-
225195
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
226196

227197
rustc_driver::init_rustc_env_logger(&early_dcx);

src/tools/miri/src/bin/miri.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ extern crate rustc_middle;
2626
extern crate rustc_session;
2727
extern crate rustc_span;
2828

29+
/// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
30+
/// for why we need this `use` statement.
31+
#[cfg(any(target_os = "linux", target_os = "macos"))]
32+
use tikv_jemalloc_sys as _;
33+
2934
mod log;
3035

3136
use std::env;
@@ -389,48 +394,7 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
389394
Ok(from..to)
390395
}
391396

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

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

0 commit comments

Comments
 (0)