perf(search): mark results as installed via a single pacman -Q call
tests / build-test (push) Has been cancelled
tests / build-test (push) Has been cancelled
Search previously ran one 'pacman -Qi <name>' subprocess per result, making queries with many matches (e.g. 'discord') take minutes. It now loads the installed package names once into an in-memory set and uses that set for the installed lookup.
This commit is contained in:
3 files changed
+59
-1
No files matched your search
+26
-1
@@ -6,6 +6,7 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
|
||||
// Helper to run shell commands and capture output
|
||||
static std::string exec(const char* cmd) {
|
||||
@@ -105,6 +106,11 @@ std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
|
||||
std::string cmd = "pacman -Ss " + query + " 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
// Load the installed packages once, so marking search results as
|
||||
// installed is an in-memory lookup instead of one `pacman -Qi` subprocess
|
||||
// per result (which made searches with many matches take minutes).
|
||||
std::set<std::string> installed = parseInstalledNames(exec("pacman -Q 2>&1"));
|
||||
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
@@ -127,7 +133,7 @@ std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
|
||||
}
|
||||
}
|
||||
|
||||
pkg.installed = isPackageInstalled(pkg.name);
|
||||
pkg.installed = installed.count(pkg.name) > 0;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
// Description line (starts with spaces)
|
||||
@@ -292,6 +298,25 @@ std::vector<PackageInfo> PacmanManager::parseInstalledOutput(const std::string&
|
||||
return results;
|
||||
}
|
||||
|
||||
std::set<std::string> PacmanManager::parseInstalledNames(const std::string& output) {
|
||||
std::set<std::string> names;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
|
||||
// Same split as parseInstalledOutput: the first space separates the
|
||||
// name from the version (hyphens in names are kept intact).
|
||||
size_t spacePos = line.find(' ');
|
||||
if (spacePos == std::string::npos) continue;
|
||||
|
||||
names.insert(line.substr(0, spacePos));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
bool PacmanManager::isPackageInstalled(const std::string& name) {
|
||||
std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
|
||||
Reference in new issue
Block a user