feat: reduce mutex waits in parallel finder
parent
ba460cc00a
commit
1ab711f81c
|
@ -27,13 +27,17 @@ ParallelFinder::find_prefix(string_view search_term) const {
|
||||||
[](const vector<string> &word_list, const string_view &search_term,
|
[](const vector<string> &word_list, const string_view &search_term,
|
||||||
forward_list<const string *> &result, size_t start_index,
|
forward_list<const string *> &result, size_t start_index,
|
||||||
size_t end_index, mutex &result_mutex) {
|
size_t end_index, mutex &result_mutex) {
|
||||||
|
forward_list<const string *> thread_results;
|
||||||
for (size_t index = start_index; index < end_index; ++index) {
|
for (size_t index = start_index; index < end_index; ++index) {
|
||||||
const auto ¤t_word = word_list[index];
|
const auto ¤t_word = word_list[index];
|
||||||
if (current_word.starts_with(search_term)) {
|
if (current_word.starts_with(search_term)) {
|
||||||
const lock_guard<mutex> lock(result_mutex);
|
thread_results.push_front(¤t_word);
|
||||||
result.push_front(¤t_word);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!thread_results.empty()) {
|
||||||
|
const lock_guard<mutex> lock(result_mutex);
|
||||||
|
result.merge(thread_results);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
cref(word_list_), cref(search_term), ref(result), first_word_index,
|
cref(word_list_), cref(search_term), ref(result), first_word_index,
|
||||||
last_word_index, ref(result_mutex));
|
last_word_index, ref(result_mutex));
|
||||||
|
|
Loading…
Reference in New Issue