Skip to content

Commit b94051a

Browse files
committed
Use std::thread::available_parallelism for determining the default number of jobs
The function was stabilized in 1.59, while the current MSRV is 1.63.
1 parent 59578ad commit b94051a

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/parallel/job_token.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
237+
238+
let num_jobs = var("NUM_JOBS").ok().and_then(|j| j.parse::<u32>().ok());
239+
let parallelism = num_jobs.unwrap_or_else(|| {
240+
std::thread::available_parallelism()
241+
.map(|v| usize::from(v) as u32)
242+
.unwrap_or(4)
243+
});
249244

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

0 commit comments

Comments
 (0)