mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:57:45 +00:00
Assistant: Avoid creating unnecessary FileResult objects
A BinaryHeap is now used to keep track of the 6 highest scoring files. This ensures that a FileResult is not created for a result that will never be displayed.
This commit is contained in:
parent
4043c89310
commit
6ecff2ac28
3 changed files with 19 additions and 5 deletions
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Providers.h"
|
#include "Providers.h"
|
||||||
|
#include <AK/BinaryHeap.h>
|
||||||
#include <AK/FuzzyMatch.h>
|
#include <AK/FuzzyMatch.h>
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
|
@ -132,7 +133,7 @@ void FileProvider::query(DeprecatedString const& query, Function<void(Vector<Non
|
||||||
|
|
||||||
m_fuzzy_match_work = Threading::BackgroundAction<Optional<Vector<NonnullRefPtr<Result>>>>::construct(
|
m_fuzzy_match_work = Threading::BackgroundAction<Optional<Vector<NonnullRefPtr<Result>>>>::construct(
|
||||||
[this, query](auto& task) -> Optional<Vector<NonnullRefPtr<Result>>> {
|
[this, query](auto& task) -> Optional<Vector<NonnullRefPtr<Result>>> {
|
||||||
Vector<NonnullRefPtr<Result>> results;
|
BinaryHeap<int, DeprecatedString, MAX_SEARCH_RESULTS> sorted_results;
|
||||||
|
|
||||||
for (auto& path : m_full_path_cache) {
|
for (auto& path : m_full_path_cache) {
|
||||||
if (task.is_canceled())
|
if (task.is_canceled())
|
||||||
|
@ -144,7 +145,20 @@ void FileProvider::query(DeprecatedString const& query, Function<void(Vector<Non
|
||||||
if (match_result.score < 0)
|
if (match_result.score < 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
results.append(adopt_ref(*new FileResult(path, match_result.score)));
|
if (sorted_results.size() < MAX_SEARCH_RESULTS || match_result.score > sorted_results.peek_min_key()) {
|
||||||
|
if (sorted_results.size() == MAX_SEARCH_RESULTS)
|
||||||
|
sorted_results.pop_min();
|
||||||
|
|
||||||
|
sorted_results.insert(match_result.score, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<NonnullRefPtr<Result>> results;
|
||||||
|
results.ensure_capacity(sorted_results.size());
|
||||||
|
while (!sorted_results.is_empty()) {
|
||||||
|
auto score = sorted_results.peek_min_key();
|
||||||
|
auto path = sorted_results.pop_min();
|
||||||
|
results.append(make_ref_counted<FileResult>(path, score));
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
namespace Assistant {
|
namespace Assistant {
|
||||||
|
|
||||||
|
static constexpr size_t MAX_SEARCH_RESULTS = 6;
|
||||||
|
|
||||||
class Result : public RefCounted<Result> {
|
class Result : public RefCounted<Result> {
|
||||||
public:
|
public:
|
||||||
virtual ~Result() = default;
|
virtual ~Result() = default;
|
||||||
|
|
|
@ -144,8 +144,6 @@ private:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr size_t MAX_SEARCH_RESULTS = 6;
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath cpath unix proc exec thread"));
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath cpath unix proc exec thread"));
|
||||||
|
@ -270,7 +268,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
else
|
else
|
||||||
app_state.selected_index = 0;
|
app_state.selected_index = 0;
|
||||||
app_state.results = results;
|
app_state.results = results;
|
||||||
app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS);
|
app_state.visible_result_count = min(results.size(), Assistant::MAX_SEARCH_RESULTS);
|
||||||
|
|
||||||
update_ui_timer->restart();
|
update_ui_timer->restart();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue