Skip to content

Commit dca9bd0

Browse files
committed
Refactor common calls to available_parallelism
1 parent 936093f commit dca9bd0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

rayon-core/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,18 @@ impl<S> ThreadPoolBuilder<S> {
459459
if self.num_threads > 0 {
460460
self.num_threads
461461
} else {
462+
let default = || {
463+
thread::available_parallelism()
464+
.map(|n| n.get())
465+
.unwrap_or(1)
466+
};
467+
462468
match env::var("RAYON_NUM_THREADS")
463469
.ok()
464470
.and_then(|s| usize::from_str(&s).ok())
465471
{
466-
Some(x) if x > 0 => return x,
467-
Some(x) if x == 0 => return thread::available_parallelism()
468-
.map(|n| n.get()).unwrap_or(1),
472+
Some(x @ 1..) => return x,
473+
Some(0) => return default(),
469474
_ => {}
470475
}
471476

@@ -474,8 +479,8 @@ impl<S> ThreadPoolBuilder<S> {
474479
.ok()
475480
.and_then(|s| usize::from_str(&s).ok())
476481
{
477-
Some(x) if x > 0 => x,
478-
_ => thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
482+
Some(x @ 1..) => x,
483+
_ => default(),
479484
}
480485
}
481486
}

0 commit comments

Comments
 (0)