Skip to content

Use jemalloc for Clippy #142286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ macro_rules! tool_extended {
tool_name: $tool_name:expr,
stable: $stable:expr
$( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
$( , add_features: $add_features:expr )?
$( , )?
}
) => {
Expand Down Expand Up @@ -1156,6 +1157,7 @@ macro_rules! tool_extended {
$tool_name,
$path,
None $( .or(Some(&$add_bins_to_sysroot)) )?,
None $( .or(Some($add_features)) )?,
)
}
}
Expand Down Expand Up @@ -1193,15 +1195,21 @@ fn run_tool_build_step(
tool_name: &'static str,
path: &'static str,
add_bins_to_sysroot: Option<&[&str]>,
add_features: Option<fn(&Builder<'_>, TargetSelection, &mut Vec<String>)>,
) -> ToolBuildResult {
let mut extra_features = Vec::new();
if let Some(func) = add_features {
func(builder, target, &mut extra_features);
}

let ToolBuildResult { tool_path, build_compiler, target_compiler } =
builder.ensure(ToolBuild {
compiler,
target,
tool: tool_name,
mode: Mode::ToolRustc,
path,
extra_features: vec![],
extra_features,
source_type: SourceType::InTree,
allow_features: "",
cargo_args: vec![],
Expand Down Expand Up @@ -1244,7 +1252,12 @@ tool_extended!(Clippy {
path: "src/tools/clippy",
tool_name: "clippy-driver",
stable: true,
add_bins_to_sysroot: ["clippy-driver"]
add_bins_to_sysroot: ["clippy-driver"],
add_features: |builder, target, features| {
if builder.config.jemalloc(target) {
features.push("jemalloc".to_string());
}
}
});
tool_extended!(Miri {
path: "src/tools/miri",
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }
[features]
integration = ["dep:tempfile"]
internal = ["dep:clippy_lints_internal", "dep:tempfile"]
jemalloc = []

[package.metadata.rust-analyzer]
# This package uses #[feature(rustc_private)]
Expand Down
35 changes: 35 additions & 0 deletions src/tools/clippy/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
// about jemalloc.
#[cfg(feature = "jemalloc")]
extern crate tikv_jemalloc_sys as jemalloc_sys;

use clippy_utils::sym;
use rustc_interface::interface;
use rustc_session::EarlyDiagCtxt;
Expand Down Expand Up @@ -181,6 +186,36 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/ne
#[allow(clippy::too_many_lines)]
#[allow(clippy::ignored_unit_patterns)]
pub fn main() {
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
// about jemalloc.
#[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;

#[cfg(target_os = "macos")]
{
unsafe extern "C" {
fn _rjem_je_zone_register();
}

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

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

rustc_driver::init_rustc_env_logger(&early_dcx);
Expand Down
Loading