perf(ui): fill the tables in a single repaint batch

A search result page inserted its rows one setItem() at a time, so the
viewport repainted per cell and, on the browse page, the row inserted as
"current" fired the pacman queries behind the details panel in the middle of
the fill. The fill now happens with updates disabled and the table's signals
muted, released just before the row the user actually sees is selected.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-12 22:22:05 +02:00
1 parent 5756704b03
commit 3b4c905e7e
3 files changed
+130 -17

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;
};
+57 -11
View File
@@ -39,6 +39,7 @@
#include <algorithm>
#include <functional>
#include <memory>
// Worker threads
SearchWorker::SearchWorker(const std::vector<IPackageManager*>& providers,
@@ -206,6 +207,38 @@ static QString providersLabel(const PluginManager* pm, const PackageInfo& pkg) {
return names.join(", ");
}
// Inserts rows into a table in one repaint batch, with the table's signals
// muted for the duration. Without it every setItem() repaints the viewport, and
// on the browse page each selection change fires the pacman queries behind the
// details panel — a thousand rows meant a thousand repaints and a pacman call
// in the middle of the fill.
class TableFillBatch {
public:
explicit TableFillBatch(QTableWidget* table) : m_table(table) {
if (!m_table) return;
m_table->setUpdatesEnabled(false);
m_blocker = std::make_unique<QSignalBlocker>(m_table);
}
// Called before the caller selects a row: that selection is what refreshes
// the details panel, so it has to be seen by the table's signals. Idempotent,
// which lets the destructor cover an early return too.
void release() {
if (!m_table || !m_blocker) return;
m_blocker.reset();
m_table->setUpdatesEnabled(true);
}
~TableFillBatch() { release(); }
TableFillBatch(const TableFillBatch&) = delete;
TableFillBatch& operator=(const TableFillBatch&) = delete;
private:
QTableWidget* m_table = nullptr;
std::unique_ptr<QSignalBlocker> m_blocker;
};
// Quote a CSV field when it contains the separator, a quote or a newline, so
// exported lists stay importable in a spreadsheet.
static QString csvField(const QString& value) {
@@ -1640,11 +1673,14 @@ void MainWindow::onRecentActivityClicked(QListWidgetItem* item) {
void MainWindow::populatePackageTable(const std::vector<PackageInfo>& results) {
m_currentResults = results;
m_packageTable->setRowCount(0);
m_packageTable->setRowCount(static_cast<int>(results.size()));
{
TableFillBatch batch(m_packageTable);
m_packageTable->setRowCount(0);
m_packageTable->setRowCount(static_cast<int>(results.size()));
for (int i = 0; i < static_cast<int>(results.size()); ++i) {
addPackageRow(i, results[i]);
for (int i = 0; i < static_cast<int>(results.size()); ++i) {
addPackageRow(i, results[i]);
}
}
m_resultCount->setText(QString("%1 paquet(s)").arg(results.size()));
@@ -1675,10 +1711,13 @@ void MainWindow::populatePackageTable(const std::vector<PackageInfo>& results) {
void MainWindow::appendPackageRows(const std::vector<PackageInfo>& results) {
const int startRow = static_cast<int>(m_currentResults.size());
m_currentResults.insert(m_currentResults.end(), results.begin(), results.end());
m_packageTable->setRowCount(static_cast<int>(m_currentResults.size()));
{
TableFillBatch batch(m_packageTable);
m_packageTable->setRowCount(static_cast<int>(m_currentResults.size()));
for (size_t i = 0; i < results.size(); ++i) {
addPackageRow(startRow + static_cast<int>(i), results[i]);
for (size_t i = 0; i < results.size(); ++i) {
addPackageRow(startRow + static_cast<int>(i), results[i]);
}
}
m_resultCount->setText(QString("%1 paquet(s)").arg(m_currentResults.size()));
}
@@ -2031,12 +2070,14 @@ void MainWindow::onCleanupListFinished(std::vector<PackageInfo> results) {
void MainWindow::populateCleanupTable(const std::vector<PackageInfo>& candidates) {
m_cleanupCandidates = candidates;
m_cleanupTable->setRowCount(0);
m_cleanupTable->setRowCount(static_cast<int>(candidates.size()));
QFont nameFont;
nameFont.setWeight(QFont::DemiBold);
TableFillBatch batch(m_cleanupTable);
m_cleanupTable->setRowCount(0);
m_cleanupTable->setRowCount(static_cast<int>(candidates.size()));
int orphans = 0;
for (int i = 0; i < static_cast<int>(candidates.size()); ++i) {
const PackageInfo& pkg = candidates[i];
@@ -2058,6 +2099,7 @@ void MainWindow::populateCleanupTable(const std::vector<PackageInfo>& candidates
m_cleanupTable->setItem(i, 3,
new QTableWidgetItem(providersLabel(m_pluginManager, pkg)));
}
batch.release();
m_cleanupCount->setText(QString("%1 orphelin(s) · %2 legacy")
.arg(orphans).arg(static_cast<int>(candidates.size()) - orphans));
@@ -2411,6 +2453,7 @@ void MainWindow::refreshHistoryTable() {
}
m_historyCount->setText(QString("%1 action(s)").arg(entries.size()));
TableFillBatch batch(m_historyTable);
m_historyTable->setRowCount(static_cast<int>(entries.size()));
for (int i = 0; i < static_cast<int>(entries.size()); ++i) {
@@ -2740,12 +2783,14 @@ void MainWindow::onRescanPlugins() {
void MainWindow::refreshPluginsTable() {
const auto& plugins = m_pluginManager->plugins();
m_pluginTable->setRowCount(0);
m_pluginTable->setRowCount(static_cast<int>(plugins.size()));
QFont nameFont;
nameFont.setWeight(QFont::DemiBold);
TableFillBatch batch(m_pluginTable);
m_pluginTable->setRowCount(0);
m_pluginTable->setRowCount(static_cast<int>(plugins.size()));
for (int i = 0; i < static_cast<int>(plugins.size()); ++i) {
const auto& entry = plugins[i];
@@ -2789,6 +2834,7 @@ void MainWindow::refreshPluginsTable() {
: "alert"));
m_pluginTable->setItem(i, 4, statusItem);
}
batch.release();
if (!plugins.empty()) {
// Keep the previously selected plugin selected when possible.
+51 -6
View File
@@ -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<std::mutex> 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<std::mutex> lock(m_queryMutex);
slot.output = fresh;
slot.fetched = now;
slot.valid = true;
}
return fresh;
}
void PacmanManager::invalidateQueries() {
std::lock_guard<std::mutex> lock(m_queryMutex);
m_installedQuery.valid = false;
m_installedDetailsQuery.valid = false;
m_syncReposQuery.valid = false;
}
std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
std::vector<PackageInfo> results;
@@ -132,7 +166,8 @@ std::vector<PackageInfo> 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<std::string> installed = parseInstalledNames(exec("pacman -Q 2>&1"));
std::set<std::string> installed =
parseInstalledNames(cachedQuery(m_installedQuery, "pacman -Q 2>&1"));
std::istringstream iss(output);
std::string line;
@@ -176,6 +211,9 @@ std::vector<PackageInfo> 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<PackageInfo> 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<std::string>& 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 =