From 2429fe725ccf50dba351bc567a82697ccf6a1733 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Thu, 30 Nov 2023 14:07:00 +0000 Subject: [PATCH] perf: move to filter map from map and filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of doing two passes of all candidates using map then a filter, this uses `filter_map` so we are only doing one pass for the candidates. This alone is quite a significant improvement of ~7% Output of `./scripts/bench 0.x` Benchmark 1: 0.x Time (mean ± σ): 2.373 s ± 0.138 s [User: 10.617 s, System: 1.697 s] Range (min … max): 2.124 s … 2.577 s 10 runs Benchmark 2: HEAD Time (mean ± σ): 2.206 s ± 0.133 s [User: 10.061 s, System: 1.811 s] Range (min … max): 1.940 s … 2.433 s 10 runs Summary HEAD ran 1.08 ± 0.09 times faster than 0.x ------------------------------------- The percentage difference is -7.00% ------------------------------------- --- rust/matcher.rs | 3 +-- rust/sorter.rs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rust/matcher.rs b/rust/matcher.rs index cbbf6d7..59a15b1 100644 --- a/rust/matcher.rs +++ b/rust/matcher.rs @@ -17,8 +17,7 @@ impl Matcher { pub fn score(&self, text: &str) -> i64 { self.matcher - .fuzzy_indices(text, &self.pattern) - .map(|(score, _indices)| score) + .fuzzy_match(text, &self.pattern) .unwrap_or_default() } } diff --git a/rust/sorter.rs b/rust/sorter.rs index f81f622..b89e99c 100644 --- a/rust/sorter.rs +++ b/rust/sorter.rs @@ -25,12 +25,19 @@ pub fn sort_strings(options: Options, strings: Vec) -> Vec { let mut matches = strings .into_par_iter() - .map(|candidate| Match { - score: matcher.score(candidate.as_str()), - content: candidate, + .filter_map(|candidate| { + let score = matcher.score(candidate.as_str()); + if score < options.minimum_score { + None + } else { + Some(Match { + score, + content: candidate, + }) + } }) - .filter(|m| m.score > options.minimum_score) .collect::>(); + matches.par_sort_unstable_by(|a, b| a.score.cmp(&b.score)); matches }