Skip to content

Commit d604727

Browse files
committed
Simplify work stealing code
1 parent 2d586e1 commit d604727

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/util/thread.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ impl<'a, T> Iterator for ParIter<'a, T> {
8888
// Steal from another worker, [spinlocking](https://en.wikipedia.org/wiki/Spinlock)
8989
// until we acquire new items to process or there's nothing left to do.
9090
loop {
91-
// Find worker with the most remaining items.
92-
let available = self
91+
// Find worker with the most remaining items, breaking out of the loop
92+
// and returning `None` if there is no work remaining.
93+
let (other, current, size) = self
9394
.workers
9495
.iter()
9596
.filter_map(|other| {
@@ -99,25 +100,20 @@ impl<'a, T> Iterator for ParIter<'a, T> {
99100

100101
(size > 0).then_some((other, current, size))
101102
})
102-
.max_by_key(|&(_, _, size)| size);
103-
104-
if let Some((other, current, size)) = available {
105-
// Split the work items into two roughly equal piles.
106-
let (start, end) = unpack(current);
107-
let middle = start + size.div_ceil(2);
108-
109-
let next = pack(middle, end);
110-
let stolen = pack(start + 1, middle);
111-
112-
// We could be preempted by another thread stealing or by the owning worker
113-
// thread finishing an item, so check indices are still unmodified.
114-
if other.compare_exchange(current, next) {
115-
worker.store(stolen);
116-
break Some(&self.items[start]);
117-
}
118-
} else {
119-
// No work remaining.
120-
break None;
103+
.max_by_key(|&(_, _, size)| size)?;
104+
105+
// Split the work items into two roughly equal piles.
106+
let (start, end) = unpack(current);
107+
let middle = start + size.div_ceil(2);
108+
109+
let next = pack(middle, end);
110+
let stolen = pack(start + 1, middle);
111+
112+
// We could be preempted by another thread stealing or by the owning worker
113+
// thread finishing an item, so check indices are still unmodified.
114+
if other.compare_exchange(current, next) {
115+
worker.store(stolen);
116+
break Some(&self.items[start]);
121117
}
122118
}
123119
}

0 commit comments

Comments
 (0)