From 1756e47f44ebabb02718f78a1cb969c3bd6cdaf6 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Tue, 8 Aug 2023 00:07:42 +0200 Subject: [PATCH] Optimize rayon iterator usage to avoid allocation --- src/worker.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/worker.rs b/src/worker.rs index 73c81b6..2401df9 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -81,7 +81,7 @@ impl Worker { (pool, worker) } - unsafe fn process_new_items(&mut self) { + unsafe fn process_new_items(&mut self, unmatched: &AtomicU32) { let matchers = &self.matchers; let pattern = &self.pattern; self.matches.reserve(self.in_flight.len()); @@ -99,17 +99,20 @@ impl Worker { if new_snapshot.end() != self.last_snapshot { let end = new_snapshot.end(); let in_flight = Mutex::new(&mut self.in_flight); - let items = new_snapshot.filter_map(|(idx, item)| { + let items = new_snapshot.map(|(idx, item)| { let Some(item) = item else { in_flight.lock().push(idx); - return None; + unmatched.fetch_add(1, atomic::Ordering::Relaxed); + return Match { score: 0, idx: u32::MAX }; }; - let score = if self.canceled.load(atomic::Ordering::Relaxed) { - 0 - } else { - pattern.score(item.matcher_columns, matchers.get())? + if self.canceled.load(atomic::Ordering::Relaxed) { + return Match { score: 0, idx }; + } + let Some(score) = pattern.score(item.matcher_columns, matchers.get()) else { + unmatched.fetch_add(1, atomic::Ordering::Relaxed); + return Match { score: 0, idx: u32::MAX }; }; - Some(Match { score, idx }) + Match { score, idx } }); self.matches.par_extend(items); self.last_snapshot = end; @@ -167,7 +170,8 @@ impl Worker { return; } - self.process_new_items(); + let mut unmatched = AtomicU32::new(0); + self.process_new_items(&unmatched); if pattern_status == pattern::Status::Rescore { self.matches.clear(); self.matches @@ -175,7 +179,6 @@ impl Worker { self.remove_in_flight_matches(); } - let mut unmatched = AtomicU32::new(0); let matchers = &self.matchers; let pattern = &self.pattern; if pattern_status != pattern::Status::Unchanged && !self.matches.is_empty() {