Skip to content

Commit

Permalink
Merge pull request #1119 from cuviper/fetch_update
Browse files Browse the repository at this point in the history
Use `fetch_update` instead of `compare_exchange_weak`
  • Loading branch information
cuviper authored Jan 26, 2024
2 parents 6cb8246 + a4da641 commit ba8e1a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
30 changes: 11 additions & 19 deletions src/iter/find_first_last/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,17 @@ impl<'p, P: 'p + Fn(&T) -> bool, T> Folder<T> for FindFolder<'p, T, P> {
};

if !found_best_in_range && (self.find_op)(&item) {
// Continuously try to set best_found until we succeed or we
// discover a better match was already found.
let mut current = self.best_found.load(Ordering::Relaxed);
loop {
if better_position(current, self.boundary, self.match_position) {
break;
}
match self.best_found.compare_exchange_weak(
current,
self.boundary,
Ordering::Relaxed,
Ordering::Relaxed,
) {
Ok(_) => {
self.item = Some(item);
break;
}
Err(v) => current = v,
}
// Update the best found index if ours is better.
let update =
self.best_found
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
better_position(self.boundary, current, self.match_position)
.then_some(self.boundary)
});

// Save this item if our index was better or equal.
if update.is_ok() || update == Err(self.boundary) {
self.item = Some(item);
}
}
self
Expand Down
23 changes: 5 additions & 18 deletions src/iter/par_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,11 @@ impl<Iter: Iterator + Send> UnindexedProducer for &IterParallelProducer<'_, Iter
type Item = Iter::Item;

fn split(self) -> (Self, Option<Self>) {
let mut count = self.split_count.load(Ordering::SeqCst);

loop {
// Check if the iterator is exhausted
if let Some(new_count) = count.checked_sub(1) {
match self.split_count.compare_exchange_weak(
count,
new_count,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => return (self, Some(self)),
Err(last_count) => count = last_count,
}
} else {
return (self, None);
}
}
// Check if the iterator is exhausted
let update = self
.split_count
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |c| c.checked_sub(1));
(self, update.is_ok().then_some(self))
}

fn fold_with<F>(self, mut folder: F) -> F
Expand Down

0 comments on commit ba8e1a1

Please sign in to comment.