Skip to content

Commit 9759ef4

Browse files
committed
wip tcmalloc
1 parent a2db928 commit 9759ef4

File tree

10 files changed

+190
-126
lines changed

10 files changed

+190
-126
lines changed

Cargo.lock

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,10 @@ checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
493493
dependencies = [
494494
"android-tzdata",
495495
"iana-time-zone",
496+
"js-sys",
496497
"num-traits",
497498
"serde",
499+
"wasm-bindgen",
498500
"windows-link",
499501
]
500502

@@ -583,6 +585,7 @@ dependencies = [
583585
"declare_clippy_lint",
584586
"filetime",
585587
"itertools",
588+
"libtcmalloc-sys",
586589
"pulldown-cmark",
587590
"regex",
588591
"rustc_tools_util 0.4.2",
@@ -1194,6 +1197,15 @@ version = "1.0.10"
11941197
source = "registry+https://github.com/rust-lang/crates.io-index"
11951198
checksum = "8975ffdaa0ef3661bfe02dbdcc06c9f829dfafe6a3c474de366a8d5e44276921"
11961199

1200+
[[package]]
1201+
name = "document-features"
1202+
version = "0.2.11"
1203+
source = "registry+https://github.com/rust-lang/crates.io-index"
1204+
checksum = "95249b50c6c185bee49034bcb378a49dc2b5dff0be90ff6616d31d64febab05d"
1205+
dependencies = [
1206+
"litrs",
1207+
]
1208+
11971209
[[package]]
11981210
name = "dyn-clone"
11991211
version = "1.0.19"
@@ -2161,6 +2173,18 @@ dependencies = [
21612173
"redox_syscall",
21622174
]
21632175

2176+
[[package]]
2177+
name = "libtcmalloc-sys"
2178+
version = "0.1.14"
2179+
source = "git+https://github.com/0xdeafbeef/tcmalloc-better.git?rev=fcec43f8e8c5#fcec43f8e8c50e1b3e04553294deb7c86aa0bef7"
2180+
dependencies = [
2181+
"cc",
2182+
"document-features",
2183+
"libc",
2184+
"patch",
2185+
"strum 0.27.2",
2186+
]
2187+
21642188
[[package]]
21652189
name = "libz-sys"
21662190
version = "1.1.22"
@@ -2213,6 +2237,12 @@ version = "0.8.0"
22132237
source = "registry+https://github.com/rust-lang/crates.io-index"
22142238
checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956"
22152239

2240+
[[package]]
2241+
name = "litrs"
2242+
version = "0.4.2"
2243+
source = "registry+https://github.com/rust-lang/crates.io-index"
2244+
checksum = "f5e54036fe321fd421e10d732f155734c4e4afd610dd556d9a82833ab3ee0bed"
2245+
22162246
[[package]]
22172247
name = "lld-wrapper"
22182248
version = "0.1.0"
@@ -2392,6 +2422,7 @@ dependencies = [
23922422
"libc",
23932423
"libffi",
23942424
"libloading",
2425+
"libtcmalloc-sys",
23952426
"measureme",
23962427
"nix",
23972428
"rand 0.9.2",
@@ -2401,7 +2432,6 @@ dependencies = [
24012432
"serde_json",
24022433
"smallvec",
24032434
"tempfile",
2404-
"tikv-jemalloc-sys",
24052435
"ui_test",
24062436
]
24072437

@@ -2437,6 +2467,17 @@ dependencies = [
24372467
"minimal-lexical",
24382468
]
24392469

2470+
[[package]]
2471+
name = "nom_locate"
2472+
version = "4.2.0"
2473+
source = "registry+https://github.com/rust-lang/crates.io-index"
2474+
checksum = "1e3c83c053b0713da60c5b8de47fe8e494fe3ece5267b2f23090a07a053ba8f3"
2475+
dependencies = [
2476+
"bytecount",
2477+
"memchr",
2478+
"nom",
2479+
]
2480+
24402481
[[package]]
24412482
name = "normpath"
24422483
version = "1.3.0"
@@ -2752,6 +2793,17 @@ dependencies = [
27522793
"windows-targets 0.52.6",
27532794
]
27542795

2796+
[[package]]
2797+
name = "patch"
2798+
version = "0.7.0"
2799+
source = "registry+https://github.com/rust-lang/crates.io-index"
2800+
checksum = "15c07fdcdd8b05bdcf2a25bc195b6c34cbd52762ada9dba88bf81e7686d14e7a"
2801+
dependencies = [
2802+
"chrono",
2803+
"nom",
2804+
"nom_locate",
2805+
]
2806+
27552807
[[package]]
27562808
name = "pathdiff"
27572809
version = "0.2.3"
@@ -3285,13 +3337,13 @@ checksum = "e4ee29da77c5a54f42697493cd4c9b9f31b74df666a6c04dfc4fde77abe0438b"
32853337
name = "rustc-main"
32863338
version = "0.0.0"
32873339
dependencies = [
3340+
"libtcmalloc-sys",
32883341
"rustc_codegen_ssa",
32893342
"rustc_driver",
32903343
"rustc_driver_impl",
32913344
"rustc_public",
32923345
"rustc_public_bridge",
32933346
"rustc_windows_rc",
3294-
"tikv-jemalloc-sys",
32953347
]
32963348

32973349
[[package]]
@@ -4781,6 +4833,7 @@ dependencies = [
47814833
"expect-test",
47824834
"indexmap",
47834835
"itertools",
4836+
"libtcmalloc-sys",
47844837
"minifier",
47854838
"pulldown-cmark-escape",
47864839
"regex",
@@ -5183,8 +5236,8 @@ dependencies = [
51835236
"nom",
51845237
"serde",
51855238
"spdx-expression",
5186-
"strum",
5187-
"strum_macros",
5239+
"strum 0.24.1",
5240+
"strum_macros 0.24.3",
51885241
"thiserror 1.0.69",
51895242
"uuid",
51905243
]
@@ -5260,6 +5313,15 @@ version = "0.24.1"
52605313
source = "registry+https://github.com/rust-lang/crates.io-index"
52615314
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
52625315

5316+
[[package]]
5317+
name = "strum"
5318+
version = "0.27.2"
5319+
source = "registry+https://github.com/rust-lang/crates.io-index"
5320+
checksum = "af23d6f6c1a224baef9d3f61e287d2761385a5b88fdab4eb4c6f11aeb54c4bcf"
5321+
dependencies = [
5322+
"strum_macros 0.27.2",
5323+
]
5324+
52635325
[[package]]
52645326
name = "strum_macros"
52655327
version = "0.24.3"
@@ -5273,6 +5335,18 @@ dependencies = [
52735335
"syn 1.0.109",
52745336
]
52755337

5338+
[[package]]
5339+
name = "strum_macros"
5340+
version = "0.27.2"
5341+
source = "registry+https://github.com/rust-lang/crates.io-index"
5342+
checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7"
5343+
dependencies = [
5344+
"heck 0.5.0",
5345+
"proc-macro2",
5346+
"quote",
5347+
"syn 2.0.106",
5348+
]
5349+
52765350
[[package]]
52775351
name = "syn"
52785352
version = "1.0.109"
@@ -5503,16 +5577,6 @@ dependencies = [
55035577
name = "tier-check"
55045578
version = "0.1.0"
55055579

5506-
[[package]]
5507-
name = "tikv-jemalloc-sys"
5508-
version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
5509-
source = "registry+https://github.com/rust-lang/crates.io-index"
5510-
checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d"
5511-
dependencies = [
5512-
"cc",
5513-
"libc",
5514-
]
5515-
55165580
[[package]]
55175581
name = "tinystr"
55185582
version = "0.8.1"

compiler/rustc/Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ rustc_public = { path = "../rustc_public" }
2020
rustc_public_bridge = { path = "../rustc_public_bridge" }
2121
# tidy-alphabetical-end
2222

23-
[dependencies.tikv-jemalloc-sys]
24-
version = "0.6.0"
25-
optional = true
26-
features = ['unprefixed_malloc_on_supported_platforms']
23+
24+
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
25+
libtcmalloc-sys = { git = "https://github.com/0xdeafbeef/tcmalloc-better.git", rev = "fcec43f8e8c5", default-features = false, features = ["unprefixed_malloc_on_supported_platforms", "32k_pages", "override_cpp_operators"] }
2726

2827
[features]
2928
# tidy-alphabetical-start
3029
check_only = ['rustc_driver_impl/check_only']
31-
jemalloc = ['dep:tikv-jemalloc-sys']
30+
jemalloc = []
3231
llvm = ['rustc_driver_impl/llvm']
3332
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
3433
max_level_info = ['rustc_driver_impl/max_level_info']

compiler/rustc/src/main.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,32 @@
3939

4040
fn main() {
4141
// See the comment at the top of this file for an explanation of this.
42-
#[cfg(feature = "jemalloc")]
42+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
4343
{
4444
use std::os::raw::{c_int, c_void};
4545

46-
use tikv_jemalloc_sys as jemalloc_sys;
47-
4846
#[used]
49-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
47+
static _TCMALLOC_BRIDGE_ALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void =
48+
libtcmalloc_sys::aligned_alloc;
5049
#[used]
51-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
52-
jemalloc_sys::posix_memalign;
50+
static CALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void = libtcmalloc_sys::calloc;
5351
#[used]
54-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
52+
static TCMALLOC_POSIX_MEMALIGN: unsafe extern "C" fn(
53+
*mut *mut c_void,
54+
usize,
55+
usize,
56+
) -> c_int = libtcmalloc_sys::posix_memalign;
5557
#[used]
56-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
58+
static _TCMALLOC_ALIGNED_ALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void =
59+
libtcmalloc_sys::aligned_alloc;
5760
#[used]
58-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
61+
static _TCMALLOC_MALLOC: unsafe extern "C" fn(usize) -> *mut c_void =
62+
libtcmalloc_sys::malloc;
5963
#[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-
}
64+
static _TCMALLOC_REALLOC: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
65+
libtcmalloc_sys::realloc;
66+
#[used]
67+
static _TCMALLOC_FREE: unsafe extern "C" fn(*mut c_void) = libtcmalloc_sys::free;
7568
}
7669

7770
rustc_driver::main()

src/librustdoc/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ tracing-tree = "0.3.0"
2929
unicode-segmentation = "1.9"
3030
# tidy-alphabetical-end
3131

32+
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
33+
libtcmalloc-sys = { git = "https://github.com/0xdeafbeef/tcmalloc-better.git", rev = "fcec43f8e8c5", default-features = false, features = ["unprefixed_malloc_on_supported_platforms", "32k_pages", "override_cpp_operators"] }
34+
3235
[dependencies.tracing-subscriber]
3336
version = "0.3.3"
3437
default-features = false
@@ -41,8 +44,5 @@ minifier = { version = "0.3.2", default-features = false }
4144
[dev-dependencies]
4245
expect-test = "1.4.0"
4346

44-
[features]
45-
jemalloc = []
46-
4747
[package.metadata.rust-analyzer]
4848
rustc_private = true

src/librustdoc/lib.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ extern crate rustc_target;
6262
extern crate rustc_trait_selection;
6363
extern crate test;
6464

65-
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
66-
// about jemalloc.
67-
#[cfg(feature = "jemalloc")]
68-
extern crate tikv_jemalloc_sys as jemalloc_sys;
69-
7065
use std::env::{self, VarError};
7166
use std::io::{self, IsTerminal};
7267
use std::path::Path;
@@ -126,34 +121,44 @@ mod visit_lib;
126121

127122
pub fn main() {
128123
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
129-
// about jemalloc.
130-
#[cfg(feature = "jemalloc")]
124+
// about tcmalloc.
125+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
131126
{
132127
use std::os::raw::{c_int, c_void};
133128

129+
unsafe extern "C" {
130+
fn calloc(count: usize, size: usize) -> *mut c_void;
131+
fn posix_memalign(ptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
132+
fn aligned_alloc(alignment: usize, size: usize) -> *mut c_void;
133+
fn malloc(size: usize) -> *mut c_void;
134+
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
135+
fn free(ptr: *mut c_void);
136+
}
137+
134138
#[used]
135-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
139+
static _TCMALLOC_BRIDGE_ALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void =
140+
libtcmalloc_sys::BridgeTCMallocInternalNewAlignedNothrow;
136141
#[used]
137-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
138-
jemalloc_sys::posix_memalign;
142+
static _TCMALLOC_BRIDGE_DELETE_ALIGNED: unsafe extern "C" fn(*mut c_void, usize) =
143+
libtcmalloc_sys::TCMallocInternalDeleteAligned;
144+
139145
#[used]
140-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
146+
static _TCMALLOC_CALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void = calloc;
141147
#[used]
142-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
148+
static _TCMALLOC_POSIX_MEMALIGN: unsafe extern "C" fn(
149+
*mut *mut c_void,
150+
usize,
151+
usize,
152+
) -> c_int = posix_memalign;
143153
#[used]
144-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
154+
static _TCMALLOC_ALIGNED_ALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void =
155+
aligned_alloc;
145156
#[used]
146-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
147-
148-
#[cfg(target_os = "macos")]
149-
{
150-
unsafe extern "C" {
151-
fn _rjem_je_zone_register();
152-
}
153-
154-
#[used]
155-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
156-
}
157+
static _TCMALLOC_MALLOC: unsafe extern "C" fn(usize) -> *mut c_void = malloc;
158+
#[used]
159+
static _TCMALLOC_REALLOC: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = realloc;
160+
#[used]
161+
static _TCMALLOC_FREE: unsafe extern "C" fn(*mut c_void) = free;
157162
}
158163

159164
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());

src/tools/clippy/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ termize = "0.2"
3232
color-print = "0.3.4"
3333
anstream = "0.6.18"
3434

35+
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
36+
libtcmalloc-sys = { git = "https://github.com/0xdeafbeef/tcmalloc-better.git", rev = "fcec43f8e8c5", default-features = false, features = ["unprefixed_malloc_on_supported_platforms", "32k_pages", "override_cpp_operators"] }
37+
3538
[dev-dependencies]
3639
cargo_metadata = "0.18.1"
3740
ui_test = "0.30.2"
@@ -51,7 +54,6 @@ rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }
5154
[features]
5255
integration = ["dep:tempfile"]
5356
internal = ["dep:clippy_lints_internal", "dep:tempfile"]
54-
jemalloc = []
5557

5658
[package.metadata.rust-analyzer]
5759
# This package uses #[feature(rustc_private)]

0 commit comments

Comments
 (0)