Skip to content
Myr

ignore: Avoid contention on num_pending

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Previously, every worker would increment the shared num_pending count on every new work item, and decrement it after finishing them, leading to lots of contention. Now, we only track the number of workers actively running, so there is no contention except when workers go to sleep or wake up. Closes #2642

authored by Tavian Barnes · committed by Andrew Gallant · parent af55fc2 · Browse files · 2 files+22 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+22 added−25 removed

  • other:+22 added−25 removed

2 files+22 added−25 removed

modified:crates/ignore/src/walk.rs+20 added-25 removed
1277 unmodified lines
1278 }1279 }1280 // Create the workers and then wait for them to finish.1281 let quit_now = Arc::new(AtomicBool::new(false));1282 let num_pending = Arc::new(AtomicUsize::new(stack.len()));1282 let active_workers = Arc::new(AtomicUsize::new(threads));1283 let stacks = Stack::new_for_each_thread(threads, stack);1284 std::thread::scope(|s| {1285 let handles: Vec<_> = stacks1286 .into_iter()1287 .map(|stack| Worker {1288 visitor: builder.build(),1289 stack,1290 quit_now: quit_now.clone(),1291 num_pending: num_pending.clone(),1291 active_workers: active_workers.clone(),1292 max_depth: self.max_depth,1293 max_filesize: self.max_filesize,1294 follow_links: self.follow_links,1295 skip: self.skip.clone(),
174 unmodified lines
1470 /// Whether all workers should terminate at the next opportunity. Note1471 /// that we need this because we don't want other `Work` to be done after1472 /// we quit. We wouldn't need this if have a priority channel.1473 quit_now: Arc<AtomicBool>,1474 /// The number of outstanding work items.1475 num_pending: Arc<AtomicUsize>,1474 /// The number of currently active workers.1475 active_workers: Arc<AtomicUsize>,1476 /// The maximum depth of directories to descend. A value of `0` means no1477 /// descension at all.1478 max_depth: Option<usize>,1479 /// The maximum size a searched file can be (in bytes). If a file exceeds
19 unmodified lines
1499 while let Some(work) = self.get_work() {1500 if let WalkState::Quit = self.run_one(work) {1501 self.quit_now();1502 }1503 self.work_done();1503 }1504 }15051506 fn run_one(&mut self, mut work: Work) -> WalkState {
173 unmodified lines
1680 self.send_quit();1681 return None;1682 }1683 None => {1685 // Once num_pending reaches 0, it is impossible for it to1686 // ever increase again. Namely, it only reaches 0 once1687 // all jobs have run such that no jobs have produced more1688 // work. We have this guarantee because num_pending is1689 // always incremented before each job is submitted and only1690 // decremented once each job is completely finished.1691 // Therefore, if this reaches zero, then there can be no1692 // other job running.1693 if self.num_pending() == 0 {1694 // Every other thread is blocked at the next recv().1695 // Send the initial quit message and quit.1684 if self.deactivate_worker() == 0 {1685 // If deactivate_worker() returns 0, every worker thread1686 // is currently within the critical section between the1687 // acquire in deactivate_worker() and the release in1688 // activate_worker() below. For this to happen, every1689 // worker's local deque must be simultaneously empty,1690 // meaning there is no more work left at all.1691 self.send_quit();1692 return None;1693 }1694 // Wait for next `Work` or `Quit` message.1695 loop {1696 if let Some(v) = self.recv() {1697 self.activate_worker();1698 value = Some(v);1699 break;1700 }1701 // Our stack isn't blocking. Instead of burning the
17 unmodified lines
1719 fn is_quit_now(&self) -> bool {1720 self.quit_now.load(AtomicOrdering::SeqCst)1721 }17221727 /// Returns the number of pending jobs.1728 fn num_pending(&self) -> usize {1729 self.num_pending.load(AtomicOrdering::SeqCst)1730 }17311723 /// Send work.1724 fn send(&self, work: Work) {1734 self.num_pending.fetch_add(1, AtomicOrdering::SeqCst);1725 self.stack.push(Message::Work(work));1726 }17271728 /// Send a quit message.
5 unmodified lines
1734 fn recv(&self) -> Option<Message> {1735 self.stack.pop()1736 }17371748 /// Signal that work has been finished.1749 fn work_done(&self) {1750 self.num_pending.fetch_sub(1, AtomicOrdering::SeqCst);1738 /// Deactivates a worker and returns the number of currently active workers.1739 fn deactivate_worker(&self) -> usize {1740 self.active_workers.fetch_sub(1, AtomicOrdering::Acquire) - 11741 }17421743 /// Reactivates a worker.1744 fn activate_worker(&self) {1745 self.active_workers.fetch_add(1, AtomicOrdering::Release);1746 }1747}17481749fn check_symlink_loop(1750 ig_parent: &Ignore,
560 unmodified lines
modified:CHANGELOG.md+2 added
21 unmodified lines
22* [PERF #1760](https://github.com/BurntSushi/ripgrep/issues/1760):23 Make most searches with `\b` look-arounds (among others) much faster.24* [PERF #2591](https://github.com/BurntSushi/ripgrep/pull/2591):25 Parallel directory traversal now uses work stealing for faster searches.26* [PERF #2642](https://github.com/BurntSushi/ripgrep/pull/2642):27 Parallel directory traversal has some contention reduced.2829Feature enhancements:3031* Added or improved file type filtering for Ada, DITA, Elixir, Fuchsia, Gentoo,
1533 unmodified lines