Skip to content

Use std::thread::available_parallelism for determining the default number of jobs #1447

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
Apr 3, 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
2 changes: 1 addition & 1 deletion src/command_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CargoOutput {
warnings: true,
output: OutputKind::Forward,
debug: match std::env::var_os("CC_ENABLE_DEBUG_OUTPUT") {
Some(v) => v != "0" && v != "false" && v != "",
Some(v) => v != "0" && v != "false" && !v.is_empty(),
None => false,
},
checked_dbg_var: Arc::new(AtomicBool::new(false)),
Expand Down
4 changes: 2 additions & 2 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl<'this> RustcCodegenFlags<'this> {
};

let clang_or_gnu =
matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu { .. });
matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu);

// Flags shared between clang and gnu
if clang_or_gnu {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<'this> RustcCodegenFlags<'this> {
}
}
}
ToolFamily::Gnu { .. } => {}
ToolFamily::Gnu => {}
ToolFamily::Msvc { .. } => {
// https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
if let Some(value) = self.control_flow_guard {
Expand Down
25 changes: 10 additions & 15 deletions src/parallel/job_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod inherited_jobserver {

pub(super) struct JobServer {
/// Implicit token for this process which is obtained and will be
/// released in parent. Since JobTokens only give back what they got,
/// released in parent. Since `JobTokens` only give back what they got,
/// there should be at most one global implicit token in the wild.
///
/// Since Rust does not execute any `Drop` for global variables,
Expand Down Expand Up @@ -164,7 +164,7 @@ mod inherited_jobserver {
helper_thread: Option<HelperThread>,
}

impl<'a> ActiveJobServer<'a> {
impl ActiveJobServer<'_> {
pub(super) async fn acquire(&mut self) -> Result<JobToken, Error> {
let mut has_requested_token = false;

Expand Down Expand Up @@ -233,19 +233,14 @@ mod inprocess_jobserver {
impl JobServer {
pub(super) fn new() -> Self {
// Use `NUM_JOBS` if set (it's configured by Cargo) and otherwise
// just fall back to a semi-reasonable number.
//
// Note that we could use `num_cpus` here but it's an extra
// dependency that will almost never be used, so
// it's generally not too worth it.
let mut parallelism = 4;
// TODO: Use std::thread::available_parallelism as an upper bound
// when MSRV is bumped.
if let Ok(amt) = var("NUM_JOBS") {
if let Ok(amt) = amt.parse() {
parallelism = amt;
}
}
// just fall back to the number of cores on the local machine, or a reasonable
// default if that cannot be determined.

let parallelism = var("NUM_JOBS")
.ok()
.and_then(|j| j.parse::<u32>().ok())
.or_else(|| Some(std::thread::available_parallelism().ok()?.get() as u32))
.unwrap_or(4);

Self(AtomicU32::new(parallelism))
}
Expand Down
2 changes: 1 addition & 1 deletion src/parallel/stderr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg_attr(target_family = "wasm", allow(unused))]
/// Helpers functions for [ChildStderr].
/// Helpers functions for [`ChildStderr`].
use std::{convert::TryInto, process::ChildStderr};

use crate::{Error, ErrorKind};
Expand Down
12 changes: 6 additions & 6 deletions src/target/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,13 @@ mod tests {
let (full_arch, _rest) = target.split_once('-').expect("target to have arch");

let mut target = TargetInfo {
full_arch: full_arch.into(),
arch: "invalid-none-set".into(),
vendor: "invalid-none-set".into(),
os: "invalid-none-set".into(),
env: "invalid-none-set".into(),
full_arch,
arch: "invalid-none-set",
vendor: "invalid-none-set",
os: "invalid-none-set",
env: "invalid-none-set",
// Not set in older Rust versions
abi: "".into(),
abi: "",
};

for cfg in cfgs.lines() {
Expand Down
2 changes: 1 addition & 1 deletion tests/cc_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn clang_cl() {
for exe_suffix in ["", ".exe"] {
let test = Test::clang();
let bin = format!("clang{exe_suffix}");
env::set_var("CC", &format!("{bin} --driver-mode=cl"));
env::set_var("CC", format!("{bin} --driver-mode=cl"));
let test_compiler = |build: cc::Build| {
let compiler = build.get_compiler();
assert_eq!(compiler.path(), Path::new(&*bin));
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ fn gnu_apple_sysroot() {
test.shim("fake-gcc")
.gcc()
.compiler("fake-gcc")
.target(&target)
.host(&target)
.target(target)
.host(target)
.file("foo.c")
.compile("foo");

Expand Down
Loading