Skip to content

Commit c23ed3e

Browse files
committed
Auto merge of #148925 - madsmtm:jemalloc-perf, r=Kobzol
Simplify `jemalloc` setup (without perf regression) Reland #146627 after fixing [the performance regression](#148851 (comment)) that caused it to be reverted in #148896. This avoids 65f0b7a (second commit in the initial PR), and adds a comment explaining why `extern crate` is needed here instead of `use` (we need to load `tikv_jemalloc_sys` from the sysroot because of rust-lang/cc-rs#1613). r? Kobzol
2 parents e9acbd9 + 73cecf3 commit c23ed3e

File tree

6 files changed

+37
-161
lines changed

6 files changed

+37
-161
lines changed

compiler/rustc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ wasi = "=0.14.2"
2828

2929

3030
[dependencies.tikv-jemalloc-sys]
31-
version = "0.6.0"
31+
version = "0.6.1"
3232
optional = true
33-
features = ['unprefixed_malloc_on_supported_platforms']
33+
features = ['override_allocator_on_supported_platforms']
3434

3535
[features]
3636
# 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: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ 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/HEAD/compiler/rustc/src/main.rs
65-
// about jemalloc.
64+
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
65+
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this.
66+
///
67+
/// FIXME(madsmtm): This is loaded from the sysroot that was built with the other `rustc` crates
68+
/// above, instead of via Cargo as you'd normally do. This is currently needed for LTO due to
69+
/// https://github.com/rust-lang/cc-rs/issues/1613.
6670
#[cfg(feature = "jemalloc")]
67-
extern crate tikv_jemalloc_sys as jemalloc_sys;
71+
extern crate tikv_jemalloc_sys as _;
6872

6973
use std::env::{self, VarError};
7074
use std::io::{self, IsTerminal};
@@ -124,37 +128,6 @@ mod visit_ast;
124128
mod visit_lib;
125129

126130
pub fn main() {
127-
// See docs in https://github.com/rust-lang/rust/blob/HEAD/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-
158131
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
159132

160133
rustc_driver::install_ice_hook(

src/tools/clippy/src/driver.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ 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+
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this.
18+
///
19+
/// FIXME(madsmtm): This is loaded from the sysroot that was built with the other `rustc` crates
20+
/// above, instead of via Cargo as you'd normally do. This is currently needed for LTO due to
21+
/// https://github.com/rust-lang/cc-rs/issues/1613.
1822
#[cfg(feature = "jemalloc")]
19-
extern crate tikv_jemalloc_sys as jemalloc_sys;
23+
extern crate tikv_jemalloc_sys as _;
2024

2125
use clippy_utils::sym;
2226
use declare_clippy_lint::LintListBuilder;
@@ -189,36 +193,6 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/ne
189193

190194
#[expect(clippy::too_many_lines)]
191195
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-
222196
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
223197

224198
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
@@ -20,6 +20,11 @@ extern crate rustc_middle;
2020
extern crate rustc_session;
2121
extern crate rustc_span;
2222

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

2530
use std::env;
@@ -395,48 +400,7 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
395400
Ok(from..to)
396401
}
397402

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

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

0 commit comments

Comments
 (0)