From 91ce4d1721765cb5862f6cf531fea0d2021021f0 Mon Sep 17 00:00:00 2001 From: Tamo Date: Mon, 13 Sep 2021 12:42:06 +0200 Subject: [PATCH] Stop iterating through the whole list of points We stop when there is no possible candidates left --- milli/src/search/criteria/geo.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/milli/src/search/criteria/geo.rs b/milli/src/search/criteria/geo.rs index c9dff307b..9629a4a15 100644 --- a/milli/src/search/criteria/geo.rs +++ b/milli/src/search/criteria/geo.rs @@ -104,14 +104,18 @@ impl Criterion for Geo<'_> { fn geo_point( rtree: &RTree, - candidates: RoaringBitmap, + mut candidates: RoaringBitmap, point: [f64; 2], ) -> Box> { - let results = rtree - .nearest_neighbor_iter(&point) - .filter_map(move |point| candidates.contains(point.data).then(|| point.data)) - .map(|id| iter::once(id).collect::()) - .collect::>(); + let mut results = Vec::new(); + for point in rtree.nearest_neighbor_iter(&point) { + if candidates.remove(point.data) { + results.push(std::iter::once(point.data).collect()); + if candidates.is_empty() { + break; + } + } + } Box::new(results.into_iter()) }