@@ -24,12 +24,12 @@ void filter_internal(const std::vector<CandidateString> &candidates,
2424 const Options &options,
2525 size_t max_results,
2626 CandidateScorePriorityQueue &results) {
27+ const auto scoreProvider = options.usePathScoring ? path_scorer_score : scorer_score;
2728 for (size_t i = 0 , len = candidates.size (); i < len; i++) {
2829 const auto &candidate = candidates[i];
2930 if (candidate.empty ()) {
3031 continue ;
3132 }
32- const auto scoreProvider = options.usePathScoring ? path_scorer_score : scorer_score;
3333 auto score = scoreProvider (candidate, query, options);
3434 if (score > 0 ) {
3535 results.emplace (score, start_index + i);
@@ -40,20 +40,14 @@ void filter_internal(const std::vector<CandidateString> &candidates,
4040 }
4141}
4242
43- void thread_worker_filter (const std::vector<CandidateString> &candidates,
44- size_t start_index,
45- const Element &query,
46- const Options &options,
47- size_t max_results,
48- CandidateScorePriorityQueue &results) {
49- filter_internal (candidates, start_index, query, options, max_results, results);
50- }
51-
5243std::vector<CandidateIndex> sort_priority_queue (CandidateScorePriorityQueue &&candidates) {
5344 vector<CandidateScore> sorted;
5445 std::vector<CandidateIndex> ret;
55- sorted.reserve (candidates.size ());
56- ret.reserve (candidates.size ());
46+
47+ const auto initial_candidates_size = candidates.size ();
48+ sorted.reserve (initial_candidates_size);
49+ ret.reserve (initial_candidates_size);
50+
5751 while (!candidates.empty ()) {
5852 sorted.emplace_back (candidates.top ());
5953 candidates.pop ();
@@ -66,6 +60,10 @@ std::vector<CandidateIndex> sort_priority_queue(CandidateScorePriorityQueue &&ca
6660}
6761
6862std::vector<CandidateIndex> filter (const vector<std::vector<CandidateString>> &candidates, const Element &query, const Options &options) {
63+ const auto candidates_size = candidates.size ();
64+
65+ assert (1 <= candidates_size);// TODO handled outside
66+
6967 CandidateScorePriorityQueue top_k;
7068 auto max_results = options.max_results ;
7169 if (max_results == 0u ) {
@@ -74,17 +72,24 @@ std::vector<CandidateIndex> filter(const vector<std::vector<CandidateString>> &c
7472
7573 // Split the dataset and pass down to multiple threads.
7674 vector<thread> threads;
77- vector<CandidateScorePriorityQueue> results (candidates.size ());
75+ threads.reserve (candidates.size ());
76+
77+ auto results = vector<CandidateScorePriorityQueue>(candidates.size ());
78+
7879 size_t start_index = 0 ;
79- for (size_t i = 1 , len = candidates.size (); i < len; i++) {
80- start_index += candidates[i - 1 ].size ();
81- threads.emplace_back (thread_worker_filter, ref (candidates[i]), start_index, ref (query), ref (options), max_results, ref (results[i]));
80+ for (size_t i = 1 ; i < candidates_size; i++) {
81+ assert (1 < i && i < candidates.size () && i < results.size ());
82+ start_index += candidates[i - 1 ].size ();// inbounds
83+ threads.emplace_back (filter_internal, ref (candidates[i]), start_index, ref (query), ref (options), max_results, ref (results[i]));// inbounds
8284 }
85+
86+ assert (threads.size () == candidates.size () && results.size () == candidates.size ());
87+
8388 // Do the work for first thread.
84- filter_internal (candidates[0 ], 0 , query, options, max_results, top_k);
85- // Wait for threads to complete and merge the restuls .
86- for (size_t i = 1 , len = candidates. size () ; i < len ; i++) {
87- threads[i - 1 ].join ();
89+ filter_internal (candidates[0 ], 0 , query, options, max_results, top_k);// inbounds (candidate_size >= 1)
90+ // Wait for threads to complete and merge the results .
91+ for (size_t i = 1 ; i < candidates_size ; i++) {
92+ threads[i - 1 ].join ();// inbounds
8893 while (!results[i].empty ()) {
8994 top_k.emplace (results[i].top ());
9095 results[i].pop ();
0 commit comments