From 52daeb6d303101eac999cb52ccd111de4c53c916 Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Sat, 12 Sep 2026 22:22:16 +0200 Subject: [PATCH] perf(pacman): cache the read-only queries the views keep re-asking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One click used to spawn the same pacman processes several times: a search runs `pacman -Q`, the Installed page runs `-Q`, `-Qi` and `-Sl`, and the dashboard re-runs them on every visit. Their output is now reused for a few seconds and dropped as soon as a transaction changes the package set, so the readings stay correct while a handful of subprocess spawns disappear from each interaction. Generated with Codebuff 🤖 Co-Authored-By: Codebuff --- include/pacman-manager.h | 22 ++++++++++++++++ src/pacman-manager.cpp | 57 +++++++++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/include/pacman-manager.h b/include/pacman-manager.h index 1a12f4d..5939095 100644 --- a/include/pacman-manager.h +++ b/include/pacman-manager.h @@ -1,7 +1,9 @@ #pragma once #include "package-manager.h" +#include #include +#include #include // Native Arch Linux package manager, integrated directly into the application @@ -110,6 +112,26 @@ public: private: bool isPackageInstalled(const std::string& name); + // Cache for the read-only queries several views ask for in a row: a search + // runs `pacman -Q`, the Installed page runs `-Q`, `-Qi` and `-Sl`, and the + // dashboard re-runs them on every visit. Without it that is a dozen + // subprocess spawns for a single click. The window is short enough that a + // package installed outside the app still shows up almost immediately, and + // every mutating operation drops the cache outright. + struct CachedQuery { + std::string output; + std::chrono::steady_clock::time_point fetched{}; + bool valid = false; + }; + + std::string cachedQuery(CachedQuery& slot, const char* command); + void invalidateQueries(); + + std::mutex m_queryMutex; + CachedQuery m_installedQuery; // pacman -Q + CachedQuery m_installedDetailsQuery; // pacman -Qi + CachedQuery m_syncReposQuery; // pacman -Sl + std::string m_password; std::string m_lastError; }; diff --git a/src/pacman-manager.cpp b/src/pacman-manager.cpp index 844445d..3ab41e5 100644 --- a/src/pacman-manager.cpp +++ b/src/pacman-manager.cpp @@ -112,6 +112,12 @@ static int percentFromLine(const std::string& line) { } } +// Read-only pacman output is reused by several views within a few seconds, so +// it is cached for this long. Short enough that a package installed outside the +// app still appears almost immediately, and every mutating operation drops the +// cache outright. +static constexpr std::chrono::seconds kQueryTtl{10}; + // Stream the lines of a command's output (already captured) to the callback — // used by providers that buffer a whole command first. Percent stays unknown. static void streamOutput(const std::string& output, ProgressCallback progressCallback) { @@ -123,6 +129,34 @@ static void streamOutput(const std::string& output, ProgressCallback progressCal } } +std::string PacmanManager::cachedQuery(PacmanManager::CachedQuery& slot, + const char* command) { + const auto now = std::chrono::steady_clock::now(); + { + std::lock_guard lock(m_queryMutex); + if (slot.valid && now - slot.fetched < kQueryTtl) return slot.output; + } + + // Queried outside the lock: a second thread asking for the same query then + // just runs it too, which is cheaper than serialising the whole app on + // pacman. + std::string fresh = exec(command); + { + std::lock_guard lock(m_queryMutex); + slot.output = fresh; + slot.fetched = now; + slot.valid = true; + } + return fresh; +} + +void PacmanManager::invalidateQueries() { + std::lock_guard lock(m_queryMutex); + m_installedQuery.valid = false; + m_installedDetailsQuery.valid = false; + m_syncReposQuery.valid = false; +} + std::vector PacmanManager::search(const std::string& query) { std::vector results; @@ -132,7 +166,8 @@ std::vector PacmanManager::search(const std::string& query) { // 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 installed = parseInstalledNames(exec("pacman -Q 2>&1")); + std::set installed = + parseInstalledNames(cachedQuery(m_installedQuery, "pacman -Q 2>&1")); std::istringstream iss(output); std::string line; @@ -176,6 +211,9 @@ std::vector PacmanManager::search(const std::string& query) { bool PacmanManager::install(const std::string& packageName, ProgressCallback progressCallback) { + // The transaction changes the installed set (and may fail halfway): drop + // the cached reads before it runs rather than after. + invalidateQueries(); if (progressCallback) progressCallback("Installation de " + packageName + "...", -1); std::string output = @@ -199,6 +237,7 @@ bool PacmanManager::install(const std::string& packageName, bool PacmanManager::remove(const std::string& packageName, ProgressCallback progressCallback) { + invalidateQueries(); if (progressCallback) progressCallback("Suppression de " + packageName + "...", -1); std::string output = @@ -220,6 +259,7 @@ bool PacmanManager::remove(const std::string& packageName, bool PacmanManager::update(const std::string& packageName, ProgressCallback progressCallback) { + invalidateQueries(); std::string output; if (packageName.empty()) { if (progressCallback) progressCallback("Mise à jour de tous les paquets...", -1); @@ -245,13 +285,15 @@ bool PacmanManager::update(const std::string& packageName, } std::vector PacmanManager::listInstalled() { - auto packages = parseInstalledOutput(exec("pacman -Q 2>&1")); + auto packages = parseInstalledOutput(cachedQuery(m_installedQuery, "pacman -Q 2>&1")); // Enrich each installed package with its installed size (shown as "Taille") - // and the repository/origin it was installed from. Both come from a single - // subprocess each, which is fast enough to run on page load. - auto sizes = parseInstalledSizes(exec("pacman -Qi 2>&1")); - auto repos = parseSyncRepos(exec("pacman -Sl 2>&1")); + // and the repository/origin it was installed from. `pacman -Qi` answers for + // every package at once, and `-Sl` only changes on a sync, so both are + // cached across the views that need them. + auto sizes = parseInstalledSizes( + cachedQuery(m_installedDetailsQuery, "pacman -Qi 2>&1")); + auto repos = parseSyncRepos(cachedQuery(m_syncReposQuery, "pacman -Sl 2>&1")); for (auto& pkg : packages) { auto sizeIt = sizes.find(pkg.name); @@ -314,6 +356,7 @@ bool PacmanManager::removePackages(const std::vector& packages, const std::string command = removePackagesCommand(packages); if (command.empty()) return true; + invalidateQueries(); if (progressCallback) { progressCallback("Suppression de " + std::to_string(packages.size()) + " paquet(s)...", -1); @@ -371,6 +414,8 @@ bool PacmanManager::cleanCache(ProgressCallback progressCallback) { } bool PacmanManager::refresh(ProgressCallback progressCallback) { + // A sync rewrites the sync databases the `-Sl` cache is built from. + invalidateQueries(); if (progressCallback) progressCallback("Rafraîchissement des bases de données...", -1); std::string output =