1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:57:45 +00:00

Assistant: Avoid copying the result vectors when providers finish

Just move() them instead to cut down on the copying. :^)
This commit is contained in:
Andreas Kling 2021-07-03 20:49:59 +02:00
parent e4199beccc
commit 94def5ae9d

View file

@ -81,7 +81,7 @@ void AppProvider::query(String const& query, Function<void(NonnullRefPtrVector<R
results.append(adopt_ref(*new AppResult(icon.bitmap_for_size(16), app_file->name(), {}, app_file, match_result.score))); results.append(adopt_ref(*new AppResult(icon.bitmap_for_size(16), app_file->name(), {}, app_file, match_result.score)));
}); });
on_complete(results); on_complete(move(results));
} }
void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
@ -112,7 +112,7 @@ void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrV
NonnullRefPtrVector<Result> results; NonnullRefPtrVector<Result> results;
results.append(adopt_ref(*new CalculatorResult(calculation))); results.append(adopt_ref(*new CalculatorResult(calculation)));
on_complete(results); on_complete(move(results));
} }
Gfx::Bitmap const* FileResult::bitmap() const Gfx::Bitmap const* FileResult::bitmap() const
@ -132,22 +132,27 @@ void FileProvider::query(const String& query, Function<void(NonnullRefPtrVector<
if (m_fuzzy_match_work) if (m_fuzzy_match_work)
m_fuzzy_match_work->cancel(); m_fuzzy_match_work->cancel();
m_fuzzy_match_work = Threading::BackgroundAction<NonnullRefPtrVector<Result>>::create([this, query](auto& task) { m_fuzzy_match_work = Threading::BackgroundAction<NonnullRefPtrVector<Result>>::create(
NonnullRefPtrVector<Result> results; [this, query](auto& task) {
NonnullRefPtrVector<Result> results;
for (auto& path : m_full_path_cache) { for (auto& path : m_full_path_cache) {
if (task.is_cancelled()) if (task.is_cancelled())
return results; return results;
auto match_result = fuzzy_match(query, path); auto match_result = fuzzy_match(query, path);
if (!match_result.matched) if (!match_result.matched)
continue; continue;
if (match_result.score < 0) if (match_result.score < 0)
continue; continue;
results.append(adopt_ref(*new FileResult(path, match_result.score))); results.append(adopt_ref(*new FileResult(path, match_result.score)));
} }
return results; }, [on_complete = move(on_complete)](auto results) { on_complete(results); }); return results;
},
[on_complete = move(on_complete)](auto results) {
on_complete(move(results));
});
} }
void FileProvider::build_filesystem_cache() void FileProvider::build_filesystem_cache()
@ -158,37 +163,41 @@ void FileProvider::build_filesystem_cache()
m_building_cache = true; m_building_cache = true;
m_work_queue.enqueue("/"); m_work_queue.enqueue("/");
Threading::BackgroundAction<int>::create([this](auto&) { Threading::BackgroundAction<int>::create(
String slash = "/"; [this](auto&) {
Core::ElapsedTimer timer; String slash = "/";
timer.start(); Core::ElapsedTimer timer;
while (!m_work_queue.is_empty()) { timer.start();
auto base_directory = m_work_queue.dequeue(); while (!m_work_queue.is_empty()) {
Core::DirIterator di(base_directory, Core::DirIterator::SkipDots); auto base_directory = m_work_queue.dequeue();
Core::DirIterator di(base_directory, Core::DirIterator::SkipDots);
while (di.has_next()) { while (di.has_next()) {
auto path = di.next_path(); auto path = di.next_path();
struct stat st = {}; struct stat st = {};
if (fstatat(di.fd(), path.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0) { if (fstatat(di.fd(), path.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0) {
perror("fstatat"); perror("fstatat");
continue; continue;
} }
if (S_ISLNK(st.st_mode)) if (S_ISLNK(st.st_mode))
continue; continue;
auto full_path = LexicalPath::join(slash, base_directory, path).string(); auto full_path = LexicalPath::join(slash, base_directory, path).string();
m_full_path_cache.append(full_path); m_full_path_cache.append(full_path);
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
m_work_queue.enqueue(full_path); m_work_queue.enqueue(full_path);
}
} }
} }
} dbgln("Built cache in {} ms", timer.elapsed());
dbgln("Built cache in {} ms", timer.elapsed()); return 0;
},
return 0; }, [this](auto) { m_building_cache = false; }); [this](auto) {
m_building_cache = false;
});
} }
void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
@ -200,7 +209,7 @@ void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVec
NonnullRefPtrVector<Result> results; NonnullRefPtrVector<Result> results;
results.append(adopt_ref(*new TerminalResult(move(command)))); results.append(adopt_ref(*new TerminalResult(move(command))));
on_complete(results); on_complete(move(results));
} }
void URLProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) void URLProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)