@@ -25,6 +25,7 @@ void filter_internal(const std::vector<CandidateString> &candidates,
2525 size_t max_results,
2626 CandidateScorePriorityQueue &results) {
2727 const auto scoreProvider = options.usePathScoring ? path_scorer_score : scorer_score;
28+ auto results_size = results.size ();
2829 for (size_t i = 0 , len = candidates.size (); i < len; i++) {
2930 const auto &candidate = candidates[i];
3031 if (candidate.empty ()) {
@@ -33,8 +34,10 @@ void filter_internal(const std::vector<CandidateString> &candidates,
3334 auto score = scoreProvider (candidate, query, options);
3435 if (score > 0 ) {
3536 results.emplace (score, start_index + i);
36- if (results.size () > max_results) {
37+ ++results_size;// maintain size manually rather than calling results.size() every time
38+ if (results_size > max_results) {
3739 results.pop ();
40+ --results_size;
3841 }
3942 }
4043 }
@@ -86,13 +89,18 @@ std::vector<CandidateIndex> filter(const vector<std::vector<CandidateString>> &c
8689 // Do the work for first thread.
8790 filter_internal (candidates[0 ], 0 , query, options, max_results, top_k);// inbounds (candidate_size >= 1)
8891 // Wait for threads to complete and merge the results.
92+
93+ auto top_k_size = 0u ;// maintain size manually rather than calling top_k.size() every time
8994 for (size_t i = 1 ; i < candidates_size; i++) {
9095 threads[i - 1 ].join ();// inbounds
9196 while (!results[i].empty ()) {
9297 top_k.emplace (results[i].top ());
9398 results[i].pop ();
94- if (top_k.size () > max_results) {
99+
100+ ++top_k_size;
101+ if (top_k_size > max_results) {
95102 top_k.pop ();
103+ --top_k_size;
96104 }
97105 }
98106 }
0 commit comments