Skip to content

Commit ba24be5

Browse files
committed
Simplify jemalloc setup
Using the new `override_allocator_on_supported_platforms` feature in `tikv-jemalloc-sys v0.6.1` we can avoid the manual statics.
1 parent 35ebdf9 commit ba24be5

File tree

7 files changed

+32
-165
lines changed

7 files changed

+32
-165
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5537,9 +5537,9 @@ version = "0.1.0"
55375537

55385538
[[package]]
55395539
name = "tikv-jemalloc-sys"
5540-
version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
5540+
version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
55415541
source = "registry+https://github.com/rust-lang/crates.io-index"
5542-
checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d"
5542+
checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b"
55435543
dependencies = [
55445544
"cc",
55455545
"libc",

compiler/rustc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ rustc_public_bridge = { path = "../rustc_public_bridge" }
2121
# tidy-alphabetical-end
2222

2323
[dependencies.tikv-jemalloc-sys]
24-
version = "0.6.0"
24+
version = "0.6.1"
2525
optional = true
26-
features = ['unprefixed_malloc_on_supported_platforms']
26+
features = ['override_allocator_on_supported_platforms']
2727

2828
[features]
2929
# tidy-alphabetical-start

compiler/rustc/src/main.rs

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@
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+
// `"override_allocator_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.
26+
// NOTE: Even though Cargo passes `--extern` with `tikv_jemalloc_sys`, we still need to `use` the
27+
// crate for the compiler to see the `#[used]`, see https://github.com/rust-lang/rust/issues/64402.
28+
// This is similarly required if we used a crate with `#[global_allocator]`.
3029
//
3130
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
3231
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
@@ -36,43 +35,9 @@
3635
// to compare their performance, see
3736
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
3837
// for an example of how to do so.
38+
#[cfg(feature = "jemalloc")]
39+
use tikv_jemalloc_sys as _;
3940

4041
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-
7742
rustc_driver::main()
7843
}

src/librustdoc/lib.rs

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

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

8180
use crate::clean::utils::DOC_RUST_LANG_ORG_VERSION;
@@ -123,37 +122,6 @@ mod visit_ast;
123122
mod visit_lib;
124123

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

159127
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/HEAD/compiler/rustc/src/main.rs
17-
// about jemalloc.
16+
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/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;
@@ -189,36 +189,6 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/ne
189189

190190
#[expect(clippy::too_many_lines)]
191191
pub fn main() {
192-
// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
193-
// about jemalloc.
194-
#[cfg(feature = "jemalloc")]
195-
{
196-
use std::os::raw::{c_int, c_void};
197-
198-
#[used]
199-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
200-
#[used]
201-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = jemalloc_sys::posix_memalign;
202-
#[used]
203-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
204-
#[used]
205-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
206-
#[used]
207-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
208-
#[used]
209-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
210-
211-
#[cfg(target_os = "macos")]
212-
{
213-
unsafe extern "C" {
214-
fn _rjem_je_zone_register();
215-
}
216-
217-
#[used]
218-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
219-
}
220-
}
221-
222192
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
223193

224194
rustc_driver::init_rustc_env_logger(&early_dcx);

src/tools/miri/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.0"
37-
features = ['unprefixed_malloc_on_supported_platforms']
36+
version = "0.6.1"
37+
features = ['override_allocator_on_supported_platforms']
3838

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

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

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

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

2631
use std::env;
@@ -398,48 +403,7 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
398403
Ok(from..to)
399404
}
400405

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

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

0 commit comments

Comments
 (0)