rustc_main/main.rs
1// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
2#![feature(rustc_private)]
3// Several crates are depended upon but unused so that they are present in the sysroot
4#![expect(unused_crate_dependencies)]
5
6// A note about jemalloc: rustc uses jemalloc when built for CI and
7// distribution. The obvious way to do this is with the `#[global_allocator]`
8// mechanism. However, for complicated reasons (see
9// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
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.
13//
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.
20//
21// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
22// provides an impl of that trait, which is called `Jemalloc`) is that we
23// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
24// It's unclear how much performance is lost because of this.
25//
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]`.
29//
30// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
31// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
32// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
33//
34// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
35// to compare their performance, see
36// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
37// for an example of how to do so.
38#[cfg(feature = "jemalloc")]
39use tikv_jemalloc_sys as _;
40
41fn main() {
42 rustc_driver::main()
43}