Skip to content

Commit 41f7c2e

Browse files
authored
Use std::thread::available_parallelism for determining the default number of jobs (#1447)
1 parent 654a6c6 commit 41f7c2e

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

src/command_helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl CargoOutput {
4646
warnings: true,
4747
output: OutputKind::Forward,
4848
debug: match std::env::var_os("CC_ENABLE_DEBUG_OUTPUT") {
49-
Some(v) => v != "0" && v != "false" && v != "",
49+
Some(v) => v != "0" && v != "false" && !v.is_empty(),
5050
None => false,
5151
},
5252
checked_dbg_var: Arc::new(AtomicBool::new(false)),

src/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'this> RustcCodegenFlags<'this> {
185185
};
186186

187187
let clang_or_gnu =
188-
matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu { .. });
188+
matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu);
189189

190190
// Flags shared between clang and gnu
191191
if clang_or_gnu {
@@ -315,7 +315,7 @@ impl<'this> RustcCodegenFlags<'this> {
315315
}
316316
}
317317
}
318-
ToolFamily::Gnu { .. } => {}
318+
ToolFamily::Gnu => {}
319319
ToolFamily::Msvc { .. } => {
320320
// https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
321321
if let Some(value) = self.control_flow_guard {

src/parallel/job_token.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mod inherited_jobserver {
8080

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

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

@@ -233,19 +233,14 @@ mod inprocess_jobserver {
233233
impl JobServer {
234234
pub(super) fn new() -> Self {
235235
// Use `NUM_JOBS` if set (it's configured by Cargo) and otherwise
236-
// just fall back to a semi-reasonable number.
237-
//
238-
// Note that we could use `num_cpus` here but it's an extra
239-
// dependency that will almost never be used, so
240-
// it's generally not too worth it.
241-
let mut parallelism = 4;
242-
// TODO: Use std::thread::available_parallelism as an upper bound
243-
// when MSRV is bumped.
244-
if let Ok(amt) = var("NUM_JOBS") {
245-
if let Ok(amt) = amt.parse() {
246-
parallelism = amt;
247-
}
248-
}
236+
// just fall back to the number of cores on the local machine, or a reasonable
237+
// default if that cannot be determined.
238+
239+
let parallelism = var("NUM_JOBS")
240+
.ok()
241+
.and_then(|j| j.parse::<u32>().ok())
242+
.or_else(|| Some(std::thread::available_parallelism().ok()?.get() as u32))
243+
.unwrap_or(4);
249244

250245
Self(AtomicU32::new(parallelism))
251246
}

src/parallel/stderr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![cfg_attr(target_family = "wasm", allow(unused))]
2-
/// Helpers functions for [ChildStderr].
2+
/// Helpers functions for [`ChildStderr`].
33
use std::{convert::TryInto, process::ChildStderr};
44

55
use crate::{Error, ErrorKind};

src/target/parser.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ mod tests {
475475
let (full_arch, _rest) = target.split_once('-').expect("target to have arch");
476476

477477
let mut target = TargetInfo {
478-
full_arch: full_arch.into(),
479-
arch: "invalid-none-set".into(),
480-
vendor: "invalid-none-set".into(),
481-
os: "invalid-none-set".into(),
482-
env: "invalid-none-set".into(),
478+
full_arch,
479+
arch: "invalid-none-set",
480+
vendor: "invalid-none-set",
481+
os: "invalid-none-set",
482+
env: "invalid-none-set",
483483
// Not set in older Rust versions
484-
abi: "".into(),
484+
abi: "",
485485
};
486486

487487
for cfg in cfgs.lines() {

tests/cc_env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn clang_cl() {
117117
for exe_suffix in ["", ".exe"] {
118118
let test = Test::clang();
119119
let bin = format!("clang{exe_suffix}");
120-
env::set_var("CC", &format!("{bin} --driver-mode=cl"));
120+
env::set_var("CC", format!("{bin} --driver-mode=cl"));
121121
let test_compiler = |build: cc::Build| {
122122
let compiler = build.get_compiler();
123123
assert_eq!(compiler.path(), Path::new(&*bin));

tests/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ fn gnu_apple_sysroot() {
532532
test.shim("fake-gcc")
533533
.gcc()
534534
.compiler("fake-gcc")
535-
.target(&target)
536-
.host(&target)
535+
.target(target)
536+
.host(target)
537537
.file("foo.c")
538538
.compile("foo");
539539

0 commit comments

Comments
 (0)