Skip to content

Commit 551e285

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 79966ae commit 551e285

File tree

7 files changed

+29
-166
lines changed

7 files changed

+29
-166
lines changed

Cargo.lock

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

55285528
[[package]]
55295529
name = "tikv-jemalloc-sys"
5530-
version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
5530+
version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
55315531
source = "registry+https://github.com/rust-lang/crates.io-index"
5532-
checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d"
5532+
checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b"
55335533
dependencies = [
55345534
"cc",
55355535
"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: 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+
// `"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.
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
@@ -59,11 +59,6 @@ extern crate rustc_target;
5959
extern crate rustc_trait_selection;
6060
extern crate test;
6161

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

8079
use crate::clean::utils::DOC_RUST_LANG_ORG_VERSION;
@@ -122,37 +121,6 @@ mod visit_ast;
122121
mod visit_lib;
123122

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

158126
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;
@@ -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/master/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/master/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)