From 072382fa61ff75ad7c08e08de3711cb8b63cc924 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 7 Sep 2020 22:38:49 +0200 Subject: [PATCH] Sort the word docids to make intersections much faster --- src/search.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/search.rs b/src/search.rs index a545ab77d..ffbb9328d 100644 --- a/src/search.rs +++ b/src/search.rs @@ -122,6 +122,10 @@ impl<'a> Search<'a> { derived_words: &[(HashMap, RoaringBitmap)], ) -> RoaringBitmap { + // We sort the derived words by inverse popularity, this way intersections are faster. + let mut derived_words: Vec<_> = derived_words.iter().collect(); + derived_words.sort_unstable_by_key(|(_, docids)| docids.len()); + // we do a union between all the docids of each of the derived words, // we got N unions (the number of original query words), we then intersect them. let mut candidates = RoaringBitmap::new();