Skip to content

Commit 267b41b

Browse files
committed
Reduce sorting in TopDocs by not sorting the portion skipped by the offset.
1 parent 6df8dc2 commit 267b41b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/collector/top_collector.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ where T: PartialOrd + Clone
114114
}
115115

116116
Ok(top_collector
117-
.into_sorted_vec()
118-
.into_iter()
119-
.skip(self.offset)
117+
.into_sorted_after(self.offset)
120118
.map(|cdoc| (cdoc.feature, cdoc.doc))
121119
.collect())
122120
}

src/collector/top_score_collector.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,24 @@ where
850850
self.buffer
851851
}
852852

853+
/// Returns the elements between `offset` and `top_n` in sorted order.
854+
pub fn into_sorted_after(
855+
mut self,
856+
offset: usize,
857+
) -> impl Iterator<Item = ComparableDoc<Score, D, R>> {
858+
if self.buffer.len() > self.top_n {
859+
self.truncate_top_n();
860+
}
861+
862+
if offset >= self.buffer.len() {
863+
return vec![].into_iter().skip(0);
864+
}
865+
866+
let (_, _, remainder) = self.buffer.select_nth_unstable(offset);
867+
remainder.sort_unstable();
868+
self.buffer.into_iter().skip(offset)
869+
}
870+
853871
/// Returns the top n elements in stored order.
854872
/// Useful if you do not need the elements in sorted order,
855873
/// for example when merging the results of multiple segments.

0 commit comments

Comments
 (0)