Skip to content

Commit

Permalink
Micro-optimize the WorkerThread::steal loop
Browse files Browse the repository at this point in the history
This lifts up the loop for `Steal::Retry`, so we continue with other
threads before trying again. For most benchmarks, the performance change
is in the noise, but this gives a consistent improvement in most of the
microbenches -- up to 8% faster on `join_microbench::increment_all_max`.
  • Loading branch information
cuviper committed Aug 17, 2020
1 parent 09428ec commit 2e88929
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,32 +758,39 @@ impl WorkerThread {
debug_assert!(self.local_deque_is_empty());

// otherwise, try to steal
let num_threads = self.registry.thread_infos.len();
let thread_infos = &self.registry.thread_infos.as_slice();
let num_threads = thread_infos.len();
if num_threads <= 1 {
return None;
}

let start = self.rng.next_usize(num_threads);
(start..num_threads)
.chain(0..start)
.filter(|&i| i != self.index)
.filter_map(|victim_index| {
let victim = &self.registry.thread_infos[victim_index];
loop {
loop {
let mut retry = false;
let start = self.rng.next_usize(num_threads);
let job = (start..num_threads)
.chain(0..start)
.filter(move |&i| i != self.index)
.find_map(|victim_index| {
let victim = &thread_infos[victim_index];
match victim.stealer.steal() {
Steal::Empty => return None,
Steal::Success(d) => {
Steal::Success(job) => {
self.log(|| JobStolen {
worker: self.index,
victim: victim_index,
});
return Some(d);
Some(job)
}
Steal::Empty => None,
Steal::Retry => {
retry = true;
None
}
Steal::Retry => {}
}
}
})
.next()
});
if job.is_some() || !retry {
return job;
}
}
}
}

Expand Down

0 comments on commit 2e88929

Please sign in to comment.