Skip to content

Commit

Permalink
Assistant: Use fstatat() while building FileProvider path cache
Browse files Browse the repository at this point in the history
Using fstatat() allows the kernel to do relative path resolution as
opposed to absolute path resolution, which is significantly faster
and allows us to build the path cache sooner. :^)
  • Loading branch information
awesomekling committed Jul 3, 2021
1 parent 801f743 commit 4fce72a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Userland/Applications/Assistant/Providers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#include "Providers.h"
#include "FuzzyMatch.h"
#include <AK/LexicalPath.h>
#include <AK/URL.h>
#include <LibCore/DirIterator.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibDesktop/Launcher.h>
Expand All @@ -18,6 +20,7 @@
#include <LibJS/Parser.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <errno.h>
#include <fcntl.h>
#include <serenity.h>
#include <spawn.h>
#include <unistd.h>
Expand Down Expand Up @@ -151,28 +154,34 @@ void FileProvider::build_filesystem_cache()
m_work_queue.enqueue("/");

Threading::BackgroundAction<int>::create([this](auto&) {
String slash = "/";
Core::ElapsedTimer timer;
timer.start();
while (!m_work_queue.is_empty()) {
auto start = m_work_queue.dequeue();
Core::DirIterator di(start, Core::DirIterator::SkipDots);
auto base_directory = m_work_queue.dequeue();
Core::DirIterator di(base_directory, Core::DirIterator::SkipDots);

while (di.has_next()) {
auto path = di.next_full_path();
auto path = di.next_path();
struct stat st = {};
if (lstat(path.characters(), &st) < 0) {
perror("stat");
if (fstatat(di.fd(), path.characters(), &st, AT_SYMLINK_) < 0) {
perror("fstatat");
continue;
}

if (S_ISLNK(st.st_mode))
continue;

m_full_path_cache.append(path);
auto full_path = LexicalPath::join(slash, base_directory, path).string();

m_full_path_cache.append(full_path);

if (S_ISDIR(st.st_mode)) {
m_work_queue.enqueue(path);
m_work_queue.enqueue(full_path);
}
}
}
dbgln("Built cache in {} ms", timer.elapsed());

return 0; }, [this](auto) { m_building_cache = false; });
}
Expand Down

0 comments on commit 4fce72a

Please sign in to comment.