Avoid redundant sort_unstable_by call on merged fuzzy matches

This commit is contained in:
Max Brunsfeld 2021-04-27 14:34:29 -07:00
parent a59b75c839
commit 75b8f7425d

View file

@ -1,8 +1,8 @@
use super::{char_bag::CharBag, EntryKind, Snapshot}; use super::{char_bag::CharBag, EntryKind, Snapshot};
use crate::util;
use gpui::scoped_pool; use gpui::scoped_pool;
use std::{ use std::{
cmp::{max, min, Ordering, Reverse}, cmp::{max, min, Ordering},
collections::BinaryHeap,
path::Path, path::Path,
sync::atomic::{self, AtomicBool}, sync::atomic::{self, AtomicBool},
sync::Arc, sync::Arc,
@ -78,7 +78,9 @@ where
}; };
let segment_size = (path_count + cpus - 1) / cpus; let segment_size = (path_count + cpus - 1) / cpus;
let mut segment_results = (0..cpus).map(|_| BinaryHeap::new()).collect::<Vec<_>>(); let mut segment_results = (0..cpus)
.map(|_| Vec::with_capacity(max_results))
.collect::<Vec<_>>();
pool.scoped(|scope| { pool.scoped(|scope| {
for (segment_idx, results) in segment_results.iter_mut().enumerate() { for (segment_idx, results) in segment_results.iter_mut().enumerate() {
@ -149,13 +151,14 @@ where
} }
}); });
let mut results = segment_results let mut results = Vec::new();
.into_iter() for segment_result in segment_results {
.flatten() if results.is_empty() {
.map(|r| r.0) results = segment_result;
.collect::<Vec<_>>(); } else {
results.sort_unstable_by(|a, b| b.cmp(&a)); util::extend_sorted(&mut results, segment_result, max_results, |a, b| b.cmp(&a));
results.truncate(max_results); }
}
results results
} }
@ -167,7 +170,7 @@ fn match_single_tree_paths<'a>(
lowercase_query: &[char], lowercase_query: &[char],
query_chars: CharBag, query_chars: CharBag,
smart_case: bool, smart_case: bool,
results: &mut BinaryHeap<Reverse<PathMatch>>, results: &mut Vec<PathMatch>,
max_results: usize, max_results: usize,
min_score: &mut f64, min_score: &mut f64,
match_positions: &mut Vec<usize>, match_positions: &mut Vec<usize>,
@ -238,14 +241,22 @@ fn match_single_tree_paths<'a>(
); );
if score > 0.0 { if score > 0.0 {
results.push(Reverse(PathMatch { let mat = PathMatch {
tree_id: snapshot.id, tree_id: snapshot.id,
path: candidate.path.clone(), path: candidate.path.clone(),
score, score,
positions: match_positions.clone(), positions: match_positions.clone(),
})); };
if let Err(i) = results.binary_search_by(|m| mat.cmp(&m)) {
if results.len() < max_results {
results.insert(i, mat);
} else if i < results.len() {
results.pop();
results.insert(i, mat);
}
if results.len() == max_results { if results.len() == max_results {
*min_score = results.peek().unwrap().0.score; *min_score = results.last().unwrap().score;
}
} }
} }
} }
@ -564,7 +575,7 @@ mod tests {
last_positions.resize(query.len(), 0); last_positions.resize(query.len(), 0);
let cancel_flag = AtomicBool::new(false); let cancel_flag = AtomicBool::new(false);
let mut results = BinaryHeap::new(); let mut results = Vec::new();
match_single_tree_paths( match_single_tree_paths(
&Snapshot { &Snapshot {
id: 0, id: 0,
@ -592,15 +603,14 @@ mod tests {
results results
.into_iter() .into_iter()
.rev()
.map(|result| { .map(|result| {
( (
paths paths
.iter() .iter()
.copied() .copied()
.find(|p| result.0.path.as_ref() == Path::new(p)) .find(|p| result.path.as_ref() == Path::new(p))
.unwrap(), .unwrap(),
result.0.positions, result.positions,
) )
}) })
.collect() .collect()