perf(search): mark results as installed via a single pacman -Q call
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:
BlackAngelTV committed 2026-09-09 20:01:25 +02:00
1 parent 44a7e5b396
commit 3fa306c815
3 files changed
+59 -1

No files matched your search

+6
View File
@@ -2,6 +2,7 @@
#include "package-manager.h"
#include <map>
#include <set>
// Native Arch Linux package manager, integrated directly into the application
class PacmanManager : public IPackageManager {
@@ -29,6 +30,11 @@ public:
// Exposed as static so the parsing can be unit-tested without invoking pacman.
static std::vector<PackageInfo> parseInstalledOutput(const std::string& output);
// Parse `pacman -Q` output into a set of installed package names, used to
// mark search results as installed without one subprocess per result.
// Exposed as static so the parsing can be unit-tested without invoking pacman.
static std::set<std::string> parseInstalledNames(const std::string& output);
// Parse `pacman -Qi` output (one block per package) into a name -> installed size map.
// Exposed as static so the parsing can be unit-tested without invoking pacman.
static std::map<std::string, std::string> parseInstalledSizes(const std::string& output);
+26 -1
View File
@@ -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;
+27
View File
@@ -36,6 +36,29 @@ static void testParseInstalledOutput_emptyInput() {
assert(pkgs.empty());
}
static void testParseInstalledNames_parsesNames() {
std::string output = "firefox 128.0\npython-pip 24.0-1\nvim 9.1.0000\n";
auto names = PacmanManager::parseInstalledNames(output);
assert(names.size() == 3);
assert(names.count("firefox") == 1);
assert(names.count("python-pip") == 1);
assert(names.count("vim") == 1);
}
static void testParseInstalledNames_skipsEmptyLines() {
std::string output = "firefox 128.0\n\nvim 9.1.0000\n";
auto names = PacmanManager::parseInstalledNames(output);
assert(names.size() == 2);
assert(names.count("firefox") == 1);
assert(names.count("vim") == 1);
}
static void testParseInstalledNames_emptyInput() {
assert(PacmanManager::parseInstalledNames("").empty());
}
static void testParseInstalledSizes_parsesBlocks() {
std::string output =
"Name : firefox\n"
@@ -96,6 +119,10 @@ int main() {
testParseInstalledOutput_skipsEmptyLines();
testParseInstalledOutput_emptyInput();
testParseInstalledNames_parsesNames();
testParseInstalledNames_skipsEmptyLines();
testParseInstalledNames_emptyInput();
testParseInstalledSizes_parsesBlocks();
testParseInstalledSizes_emptyInput();
testParseSyncRepos_skipsHeader();