Skip to content

Commit

Permalink
Auto merge of #83152 - guswynn:jemallocator_part2, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Use tikv-jemallocator in rustc/rustdoc in addition to jemalloc-sys when enabled.

In #81782 it was mentioned that one reason rustc may benefit from minimalloc is it doesn't use the `sdallocx` api from jemalloc.

Currently, on unix, rust uses jemalloc by importing its symbols to use them with the default, System (libc) global allocator.
This PR switches its global alloc to `tikv-jemallocator`, which correctly uses sized deallocation (https://docs.rs/tikv-jemallocator/0.4.1/src/tikv_jemallocator/lib.rs.html#121-126). `tikv-jemallocator`, as far as I can tell, is a more up-to-date set of bindings to jemalloc than `jemallocator`

The perf results of this pr are in large part due to the version upgrade of jemalloc, but sized deallocation has a non-trivial improvement, particularly to rustdoc.

This pr also includes changes to bootstrap to correctly pass the jemalloc feature through to the rustdoc build
  • Loading branch information
bors committed Apr 5, 2021
2 parents d203fce + 3965773 commit d322385
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 17 deletions.
35 changes: 23 additions & 12 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1732,17 +1732,6 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"

[[package]]
name = "jemalloc-sys"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45"
dependencies = [
"cc",
"fs_extra",
"libc",
]

[[package]]
name = "jobserver"
version = "0.1.21"
Expand Down Expand Up @@ -3586,9 +3575,10 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
name = "rustc-main"
version = "0.0.0"
dependencies = [
"jemalloc-sys",
"rustc_codegen_ssa",
"rustc_driver",
"tikv-jemalloc-sys",
"tikv-jemallocator",
]

[[package]]
Expand Down Expand Up @@ -5318,6 +5308,27 @@ dependencies = [
name = "tier-check"
version = "0.1.0"

[[package]]
name = "tikv-jemalloc-sys"
version = "0.4.1+5.2.1-patched"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a26331b05179d4cb505c8d6814a7e18d298972f0a551b0e3cefccff927f86d3"
dependencies = [
"cc",
"fs_extra",
"libc",
]

[[package]]
name = "tikv-jemallocator"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c14a5a604eb8715bc5785018a37d00739b180bcf609916ddf4393d33d49ccdf"
dependencies = [
"libc",
"tikv-jemalloc-sys",
]

[[package]]
name = "time"
version = "0.1.43"
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ rustc_driver = { path = "../rustc_driver" }
# crate is intended to be used by codegen backends, which may not be in-tree.
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }

[dependencies.jemalloc-sys]
version = '0.3.0'
[dependencies.tikv-jemalloc-sys]
version = '0.4.0'
optional = true
features = ['unprefixed_malloc_on_supported_platforms']

[dependencies.tikv-jemallocator]
version = '0.4.0'
optional = true

[features]
jemalloc = ['jemalloc-sys']
jemalloc = ['tikv-jemalloc-sys', 'tikv-jemallocator']
llvm = ['rustc_driver/llvm']
max_level_info = ['rustc_driver/max_level_info']
15 changes: 14 additions & 1 deletion compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Configure jemalloc as the `global_allocator` when configured. This is
// so that we use the sized deallocation apis jemalloc provides
// (namely `sdallocx`).
//
// The symbol overrides documented below are also performed so that we can
// ensure that we use a consistent allocator across the rustc <-> llvm boundary
#[cfg(feature = "jemalloc")]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[cfg(feature = "tikv-jemalloc-sys")]
use tikv_jemalloc_sys as jemalloc_sys;

fn main() {
// Pull in jemalloc when enabled.
//
Expand All @@ -7,7 +20,7 @@ fn main() {
// dynamic libraries. That means to pull in jemalloc we actually need to
// reference allocation symbols one way or another (as this file is the only
// object code in the rustc executable).
#[cfg(feature = "jemalloc-sys")]
#[cfg(feature = "tikv-jemalloc-sys")]
{
use std::os::raw::{c_int, c_void};

Expand Down
7 changes: 6 additions & 1 deletion src/bootstrap/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ impl Step for Rustdoc {
// they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
// libraries here. The intuition here is that If we've built a compiler, we should be able
// to build rustdoc.
//
let mut features = Vec::new();
if builder.config.jemalloc {
features.push("jemalloc".to_string());
}

let cargo = prepare_tool_cargo(
builder,
Expand All @@ -541,7 +546,7 @@ impl Step for Rustdoc {
"build",
"src/tools/rustdoc",
SourceType::InTree,
&[],
features.as_slice(),
);

builder.info(&format!(
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
[dev-dependencies]
expect-test = "1.0"

[features]
jemalloc = []

[package.metadata.rust-analyzer]
rustc_private = true
51 changes: 51 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern crate tracing;
// So if `rustc` was specified in Cargo.toml, this would spuriously rebuild crates.
//
// Dependencies listed in Cargo.toml do not need `extern crate`.

extern crate rustc_ast;
extern crate rustc_ast_lowering;
extern crate rustc_ast_pretty;
Expand Down Expand Up @@ -60,6 +61,15 @@ extern crate rustc_trait_selection;
extern crate rustc_typeck;
extern crate test as testing;

#[cfg(feature = "jemalloc")]
extern crate tikv_jemalloc_sys;
#[cfg(feature = "jemalloc")]
use tikv_jemalloc_sys as jemalloc_sys;
#[cfg(feature = "jemalloc")]
extern crate tikv_jemallocator;
#[cfg(feature = "jemalloc")]
use tikv_jemallocator as jemallocator;

use std::default::Default;
use std::env;
use std::process;
Expand Down Expand Up @@ -113,7 +123,48 @@ mod theme;
mod visit_ast;
mod visit_lib;

// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
// about jemallocator
#[cfg(feature = "jemalloc")]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

pub fn main() {
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
// about jemalloc-sys
#[cfg(feature = "jemalloc")]
{
use std::os::raw::{c_int, c_void};

#[used]
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
#[used]
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
jemalloc_sys::posix_memalign;
#[used]
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
#[used]
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
#[used]
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
#[used]
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;

// On OSX, jemalloc doesn't directly override malloc/free, but instead
// registers itself with the allocator's zone APIs in a ctor. However,
// the linker doesn't seem to consider ctors as "used" when statically
// linking, so we need to explicitly depend on the function.
#[cfg(target_os = "macos")]
{
extern "C" {
fn _rjem_je_zone_register();
}

#[used]
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
}
}

rustc_driver::set_sigpipe_handler();
rustc_driver::install_ice_hook();

Expand Down
3 changes: 3 additions & 0 deletions src/tools/rustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ path = "main.rs"

[dependencies]
rustdoc = { path = "../../librustdoc" }

[features]
jemalloc = ['rustdoc/jemalloc']

0 comments on commit d322385

Please sign in to comment.