Improve resume queue load performance

This commit is contained in:
Leon Blakey 2025-06-07 10:08:30 -07:00
commit 58a87138cc

View file

@ -189,8 +189,14 @@ void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename)
return; return;
} }
QHash<TorrentID, qsizetype> registeredTorrentsIndexes;
registeredTorrentsIndexes.reserve(m_registeredTorrents.length());
qsizetype i = 0;
for (const TorrentID &torrentID : asConst(m_registeredTorrents))
registeredTorrentsIndexes.insert(torrentID, i++);
const QRegularExpression hashPattern {u"^([A-Fa-f0-9]{40})$"_s}; const QRegularExpression hashPattern {u"^([A-Fa-f0-9]{40})$"_s};
int start = 0; qsizetype start = 0;
while (true) while (true)
{ {
const auto line = QString::fromLatin1(queueFile.readLine(lineMaxLength).trimmed()); const auto line = QString::fromLatin1(queueFile.readLine(lineMaxLength).trimmed());
@ -201,10 +207,13 @@ void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename)
if (rxMatch.hasMatch()) if (rxMatch.hasMatch())
{ {
const auto torrentID = BitTorrent::TorrentID::fromString(rxMatch.captured(1)); const auto torrentID = BitTorrent::TorrentID::fromString(rxMatch.captured(1));
const int pos = m_registeredTorrents.indexOf(torrentID, start); const qsizetype pos = registeredTorrentsIndexes.value(torrentID, -1);
if (pos != -1) if (pos != -1)
{ {
std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]); if (start != pos) {
std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]);
registeredTorrentsIndexes.insert(torrentID, start);
}
++start; ++start;
} }
} }