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
6use std::process::ExitCode;
7
8// A note about jemalloc: rustc uses jemalloc when built for CI and
9// distribution. The obvious way to do this is with the `#[global_allocator]`
10// mechanism. However, for complicated reasons (see
11// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
12// details) that mechanism doesn't work here. Also, we'd like to use a
13// consistent allocator across the rustc <-> llvm boundary, and
14// `#[global_allocator]` wouldn't provide that.
15//
16// Instead, we use a lower-level mechanism, namely the
17// `"override_allocator_on_supported_platforms"` Cargo feature of jemalloc-sys.
18//
19// This makes jemalloc-sys override the libc/system allocator's implementation
20// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
21// calls `libc::malloc()` et al., is actually calling into jemalloc.
22//
23// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
24// provides an impl of that trait, which is called `Jemalloc`) is that we
25// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
26// It's unclear how much performance is lost because of this.
27//
28// NOTE: Even though Cargo passes `--extern` with `tikv_jemalloc_sys`, we still need to `use` the
29// crate for the compiler to see the `#[used]`, see https://github.com/rust-lang/rust/issues/64402.
30// This is similarly required if we used a crate with `#[global_allocator]`.
31//
32// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
33// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
34// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
35//
36// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
37// to compare their performance, see
38// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
39// for an example of how to do so.
40#[cfg(feature = "jemalloc")]
41use tikv_jemalloc_sys as _;
42
43fn main() -> ExitCode {
44 rustc_driver::main()
45}