From 20974711a1346482f88f20cccf5a76629aee5288 Mon Sep 17 00:00:00 2001 From: yah01 Date: Mon, 20 Mar 2023 10:17:56 +0800 Subject: [PATCH] Optimize range search for nq > 1 (#22831) Signed-off-by: yah01 --- .../core/src/common/RangeSearchHelper.cpp | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/internal/core/src/common/RangeSearchHelper.cpp b/internal/core/src/common/RangeSearchHelper.cpp index 58ece0c179..126de23d6a 100644 --- a/internal/core/src/common/RangeSearchHelper.cpp +++ b/internal/core/src/common/RangeSearchHelper.cpp @@ -55,30 +55,28 @@ SortRangeSearchResult(DatasetPtr data_set, auto p_dist = new float[topk * nq]; std::fill_n(p_dist, topk * nq, std::numeric_limits::max()); - // cnt means the subscript of p_id and p_dist - int cnt = 0; - - for (int i = 0; i < nq; i++) { - // if RangeSearch answer size of one nq is less than topk, set the capacity to size - int size = lims[i + 1] - lims[i]; - int capacity = topk > size ? size : topk; - /* + /* * get result for one nq * IP: 1.0 range_filter radius * |------------+---------------| min_heap descending_order * L2: 0.0 range_filter radius * |------------+---------------| max_heap ascending_order * - */ - std::function cmp = - std::less>(); - if (IsMetricType(metric_type, knowhere::metric::IP)) { - cmp = std::greater>(); - } - std::priority_queue, - std::vector>, - decltype(cmp)> - sub_result(cmp); + */ + std::function cmp = + std::less<>(); + if (IsMetricType(metric_type, knowhere::metric::IP)) { + cmp = std::greater<>(); + } + std::priority_queue, decltype(cmp)> + sub_result(cmp); + + // The subscript of p_id and p_dist + int cnt = 0; + for (int i = 0; i < nq; i++) { + // if RangeSearch answer size of one nq is less than topk, set the capacity to size + int size = lims[i + 1] - lims[i]; + int capacity = topk > size ? size : topk; for (int j = lims[i]; j < lims[i + 1]; j++) { auto current = ResultPair(dist[j], id[j]);