perf(pacman): cache the read-only queries the views keep re-asking

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 <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-12 22:22:16 +02:00
1 parent 5756704b03
commit 52daeb6d30
2 files changed
+73 -6

No files matched your search

+22
View File
@@ -1,7 +1,9 @@
#pragma once
#include "package-manager.h"
#include <chrono>
#include <map>
#include <mutex>
#include <set>
// 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;
};