Skip to content

Commit b27f533

Browse files
committed
Refactor common calls to available_parallelism
(cherry picked from commit dca9bd0)
1 parent e7d8275 commit b27f533

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
@@ -492,13 +492,18 @@ impl<S> ThreadPoolBuilder<S> {
492492
if self.num_threads > 0 {
493493
self.num_threads
494494
} else {
495+
let default = || {
496+
thread::available_parallelism()
497+
.map(|n| n.get())
498+
.unwrap_or(1)
499+
};
500+
495501
match env::var("RAYON_NUM_THREADS")
496502
.ok()
497503
.and_then(|s| usize::from_str(&s).ok())
498504
{
499-
Some(x) if x > 0 => return x,
500-
Some(x) if x == 0 => return thread::available_parallelism()
501-
.map(|n| n.get()).unwrap_or(1),
505+
Some(x @ 1..) => return x,
506+
Some(0) => return default(),
502507
_ => {}
503508
}
504509

@@ -507,8 +512,8 @@ impl<S> ThreadPoolBuilder<S> {
507512
.ok()
508513
.and_then(|s| usize::from_str(&s).ok())
509514
{
510-
Some(x) if x > 0 => x,
511-
_ => thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
515+
Some(x @ 1..) => x,
516+
_ => default(),
512517
}
513518
}
514519
}

0 commit comments

Comments
 (0)