appdb: cache favourite status during sort and avoid double sort

This commit is contained in:
2 * r + 2 * t 2026-03-12 22:08:43 +11:00
parent c14db315f6
commit ca06d13d27

View file

@ -212,10 +212,9 @@ void AppDb::incrementFrequency(const QString& id) {
auto* app = m_apps.value(id); auto* app = m_apps.value(id);
if (app) { if (app) {
const auto before = getSortedApps(); const auto before = getSortedApps();
app->incrementFrequency(); app->incrementFrequency();
getSortedApps();
if (before != getSortedApps()) { if (before != m_sortedApps) {
emit appsChanged(); emit appsChanged();
} }
} else { } else {
@ -225,15 +224,22 @@ void AppDb::incrementFrequency(const QString& id) {
QList<AppEntry*>& AppDb::getSortedApps() const { QList<AppEntry*>& AppDb::getSortedApps() const {
m_sortedApps = m_apps.values(); m_sortedApps = m_apps.values();
std::sort(m_sortedApps.begin(), m_sortedApps.end(), [this](AppEntry* a, AppEntry* b) {
bool aIsFav = isFavourite(a); // Pre-compute favourite status to avoid repeated regex matching during sort
bool bIsFav = isFavourite(b); QSet<QString> favSet;
if (aIsFav != bIsFav) { favSet.reserve(m_sortedApps.size());
for (const auto* app : std::as_const(m_sortedApps)) {
if (isFavourite(app))
favSet.insert(app->id());
}
std::sort(m_sortedApps.begin(), m_sortedApps.end(), [&favSet](AppEntry* a, AppEntry* b) {
const bool aIsFav = favSet.contains(a->id());
const bool bIsFav = favSet.contains(b->id());
if (aIsFav != bIsFav)
return aIsFav; return aIsFav;
} if (a->frequency() != b->frequency())
if (a->frequency() != b->frequency()) {
return a->frequency() > b->frequency(); return a->frequency() > b->frequency();
}
return a->name().localeAwareCompare(b->name()) < 0; return a->name().localeAwareCompare(b->name()) < 0;
}); });
return m_sortedApps; return m_sortedApps;
@ -269,7 +275,8 @@ void AppDb::updateAppFrequencies() {
app->setFrequency(getFrequency(app->id())); app->setFrequency(getFrequency(app->id()));
} }
if (before != getSortedApps()) { getSortedApps();
if (before != m_sortedApps) {
emit appsChanged(); emit appsChanged();
} }
} }