mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-25 22:14:32 +00:00
Speed up lookup time
By adding another variable we can get O(1) lookup time instead of O(n). Fix up 5f415c292ddc2ea13015a2bec42f9f6b710ed732.
This commit is contained in:
parent
a2ebd77eac
commit
ff31bb86bc
@ -328,6 +328,7 @@ void PeerListWidget::copySelectedPeers()
|
|||||||
void PeerListWidget::clear()
|
void PeerListWidget::clear()
|
||||||
{
|
{
|
||||||
m_peerItems.clear();
|
m_peerItems.clear();
|
||||||
|
m_itemsByIP.clear();
|
||||||
const int nbrows = m_listModel->rowCount();
|
const int nbrows = m_listModel->rowCount();
|
||||||
if (nbrows > 0)
|
if (nbrows > 0)
|
||||||
m_listModel->removeRows(0, nbrows);
|
m_listModel->removeRows(0, nbrows);
|
||||||
@ -365,7 +366,13 @@ void PeerListWidget::loadPeers(const BitTorrent::TorrentHandle *torrent)
|
|||||||
|
|
||||||
// Remove peers that are gone
|
// Remove peers that are gone
|
||||||
for (const PeerEndpoint &peerEndpoint : asConst(existingPeers)) {
|
for (const PeerEndpoint &peerEndpoint : asConst(existingPeers)) {
|
||||||
const QStandardItem *item = m_peerItems.take(peerEndpoint);
|
QStandardItem *item = m_peerItems.take(peerEndpoint);
|
||||||
|
|
||||||
|
QSet<QStandardItem *> &items = m_itemsByIP[peerEndpoint.address.ip];
|
||||||
|
items.remove(item);
|
||||||
|
if (items.isEmpty())
|
||||||
|
m_itemsByIP.remove(peerEndpoint.address.ip);
|
||||||
|
|
||||||
m_listModel->removeRow(item->row());
|
m_listModel->removeRow(item->row());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -387,6 +394,7 @@ void PeerListWidget::updatePeer(const BitTorrent::TorrentHandle *torrent, const
|
|||||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::IP_HIDDEN), peerIp);
|
m_listModel->setData(m_listModel->index(row, PeerListDelegate::IP_HIDDEN), peerIp);
|
||||||
|
|
||||||
itemIter = m_peerItems.insert(peerEndpoint, m_listModel->item(row, PeerListDelegate::IP));
|
itemIter = m_peerItems.insert(peerEndpoint, m_listModel->item(row, PeerListDelegate::IP));
|
||||||
|
m_itemsByIP[peerEndpoint.address.ip].insert(itemIter.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
const int row = (*itemIter)->row();
|
const int row = (*itemIter)->row();
|
||||||
@ -420,13 +428,9 @@ void PeerListWidget::updatePeer(const BitTorrent::TorrentHandle *torrent, const
|
|||||||
|
|
||||||
void PeerListWidget::handleResolved(const QHostAddress &ip, const QString &hostname) const
|
void PeerListWidget::handleResolved(const QHostAddress &ip, const QString &hostname) const
|
||||||
{
|
{
|
||||||
for (auto iter = m_peerItems.cbegin(); iter != m_peerItems.cend(); ++iter) {
|
const QSet<QStandardItem *> items = m_itemsByIP.value(ip);
|
||||||
if (iter.key().address.ip == ip) {
|
for (QStandardItem *item : items)
|
||||||
QStandardItem *item {iter.value()};
|
item->setData(hostname, Qt::DisplayRole);
|
||||||
item->setData(hostname, Qt::DisplayRole);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerListWidget::handleSortColumnChanged(const int col)
|
void PeerListWidget::handleSortColumnChanged(const int col)
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#define PEERLISTWIDGET_H
|
#define PEERLISTWIDGET_H
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
#include <QSet>
|
||||||
#include <QTreeView>
|
#include <QTreeView>
|
||||||
|
|
||||||
class QHostAddress;
|
class QHostAddress;
|
||||||
@ -85,6 +86,7 @@ private:
|
|||||||
PropertiesWidget *m_properties = nullptr;
|
PropertiesWidget *m_properties = nullptr;
|
||||||
Net::ReverseResolution *m_resolver = nullptr;
|
Net::ReverseResolution *m_resolver = nullptr;
|
||||||
QHash<PeerEndpoint, QStandardItem *> m_peerItems;
|
QHash<PeerEndpoint, QStandardItem *> m_peerItems;
|
||||||
|
QHash<QHostAddress, QSet<QStandardItem *>> m_itemsByIP; // must be kept in sync with `m_peerItems`
|
||||||
bool m_resolveCountries;
|
bool m_resolveCountries;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user