Convert queue to while loop.

Took 19 seconds
This commit is contained in:
Lukas Brübach 2026-06-14 08:45:47 +02:00
parent bba5b775ec
commit 5c021f0aad

View file

@ -79,65 +79,68 @@ void UserCardArtProvider::processQueue()
return;
}
if (queue.isEmpty()) {
return;
}
while (!queue.isEmpty()) {
const QString key = queue.dequeue();
const QString key = queue.dequeue();
const QStringList parts = key.split(u'|');
if (parts.size() != 2) {
pending.remove(key);
processQueue();
return;
}
const QString userName = parts.at(0);
const QString cardName = parts.at(1);
ExactCard card = CardDatabaseManager::query()->getCard({cardName});
if (!card) {
pending.remove(key);
processQueue();
return;
}
QPixmap fullRes;
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
// If already available, store immediately
if (!fullRes.isNull()) {
insertIntoCache(key, cropCardArt(fullRes));
pending.remove(key);
emit cardArtUpdated(userName);
processQueue();
return;
}
// Otherwise wait for async load
QPointer<UserCardArtProvider> self(this);
connect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, this, [self, key, userName, card]() mutable {
if (!self) {
return;
const QStringList parts = key.split(u'|');
if (parts.size() != 2) {
pending.remove(key);
continue;
}
disconnect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, self, nullptr);
const QString userName = parts.at(0);
const QString cardName = parts.at(1);
ExactCard card = CardDatabaseManager::query()->getCard({cardName});
if (!card) {
pending.remove(key);
continue;
}
QPixmap fullRes;
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
// Synchronous hit (already loaded/on disk)
if (!fullRes.isNull()) {
self->insertIntoCache(key, self->cropCardArt(fullRes));
} else {
self->insertIntoCache(key, QPixmap());
insertIntoCache(key, cropCardArt(fullRes));
pending.remove(key);
emit cardArtUpdated(userName);
continue;
}
self->pending.remove(key);
// Async load required.
QPointer<UserCardArtProvider> self(this);
emit self->cardArtUpdated(userName);
self->processQueue();
});
auto conn = std::make_shared<QMetaObject::Connection>();
*conn = connect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, this,
[self, key, userName, card, conn]() mutable {
if (!self) {
return;
}
QObject::disconnect(*conn);
QPixmap fullRes;
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
if (!fullRes.isNull()) {
self->insertIntoCache(key, self->cropCardArt(fullRes));
} else {
self->insertIntoCache(key, QPixmap());
}
self->pending.remove(key);
emit self->cardArtUpdated(userName);
// Resume processing remaining queued items.
self->processQueue();
});
// Stop here. We'll continue when the async load finishes.
return;
}
}