Skip to content

Commit 5067c64

Browse files
committed
perf(search): latest tweaks to boost search performance
1 parent 15ddfe2 commit 5067c64

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/python/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use pyo3::create_exception;
88
use pyo3::prelude::*;
99
use std::sync::Arc;
1010

11-
// TODO: Lazily lumped all errors here but maybe more specific errors can done? like
12-
// FeatureExtractionError and the likes
1311
create_exception!(simstring_rust, SearchError, pyo3::exceptions::PyValueError);
1412

1513
// Wrapper for FeatureExtractor trait as I can't find any direct translation.
@@ -246,7 +244,6 @@ impl PySearcher {
246244
SearchError::new_err(format!("Invalid threshold: {val}"))
247245
}
248246
})?;
249-
// TODO: Explore if the python bindings can handle returning references
250247
Ok(results.into_iter().map(|s| s.to_string()).collect())
251248
}
252249

src/search.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,28 @@ impl<'db, M: Measure> Searcher<'db, M> {
104104
let min_feat_size = self.measure.min_feature_size(query_size, alpha);
105105
let max_feat_size = self.measure.max_feature_size(query_size, alpha, self.db);
106106

107-
let mut all_candidates: Vec<StringId> = (min_feat_size..=max_feat_size)
107+
(min_feat_size..=max_feat_size)
108108
.into_par_iter()
109-
.flat_map(|candidate_size| {
109+
.map(|candidate_size| {
110110
let tau =
111111
self.measure
112112
.minimum_common_feature_count(query_size, candidate_size, alpha);
113-
self.overlap_join(query_features, tau, candidate_size)
114-
})
115-
.collect();
116113

117-
all_candidates.sort_unstable();
118-
all_candidates.dedup();
114+
if tau == 0 || tau > query_size {
115+
return FxHashSet::default();
116+
}
119117

120-
all_candidates.into_iter().collect()
118+
self.overlap_join(query_features, tau, candidate_size)
119+
.into_iter()
120+
.collect::<FxHashSet<StringId>>()
121+
})
122+
.reduce(
123+
FxHashSet::default,
124+
|mut acc, set| {
125+
acc.extend(set);
126+
acc
127+
},
128+
)
121129
}
122130

123131
fn overlap_join(
@@ -136,6 +144,11 @@ impl<'db, M: Measure> Searcher<'db, M> {
136144
.map(|&feature| self.db.lookup_strings(candidate_size, feature))
137145
.collect();
138146

147+
let available_features = feature_sets.iter().filter(|set| set.is_some()).count();
148+
if available_features < tau {
149+
return Vec::new();
150+
}
151+
139152
let mut feature_indices: Vec<usize> = (0..query_features.len()).collect();
140153
feature_indices.sort_unstable_by_key(|&i| feature_sets[i].map_or(usize::MAX, |s| s.len()));
141154

0 commit comments

Comments
 (0)