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
4use std::process::ExitCode;
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, that would not affect LLVM's C / C++ allocations and we also want
9// to use a single allocator in the process to reduce memory usage.
10//
11// Instead, we use a lower-level mechanism, namely the
12// `"override_allocator_on_supported_platforms"` Cargo feature of jemalloc-sys.
13//
14// This makes jemalloc-sys override the libc/system allocator's implementation
15// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
16// calls `libc::malloc()` et al., is actually calling into jemalloc.
17//
18// This override happens for the entire process, ensuring that there's no mixup
19// of C allocators across dylibs / binaries, notably the rustc <-> llvm boundary.
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: if you are reading this comment because you want to set a custom `global_allocator` for
27// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
28// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
29//
30// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
31// to compare their performance, see
32// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
33// for an example of how to do so.
34rustc_driver::override_c_allocator_in_binary!();
35
36fn main() -> ExitCode {
37 rustc_driver::main()
38}