1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-25 22:14:32 +00:00

Avoid redundant data conversions and lookups

This commit is contained in:
Chocobo1 2019-07-31 12:40:26 +08:00
parent c0fc4e6d90
commit 25a94ee60a
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C

View File

@ -113,29 +113,31 @@ namespace
QVariantMap getTranserInfo() QVariantMap getTranserInfo()
{ {
QVariantMap map; QVariantMap map;
const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status(); const auto *session = BitTorrent::Session::instance();
const BitTorrent::CacheStatus &cacheStatus = BitTorrent::Session::instance()->cacheStatus();
const BitTorrent::SessionStatus &sessionStatus = session->status();
const BitTorrent::CacheStatus &cacheStatus = session->cacheStatus();
map[KEY_TRANSFER_DLSPEED] = sessionStatus.payloadDownloadRate; map[KEY_TRANSFER_DLSPEED] = sessionStatus.payloadDownloadRate;
map[KEY_TRANSFER_DLDATA] = sessionStatus.totalPayloadDownload; map[KEY_TRANSFER_DLDATA] = sessionStatus.totalPayloadDownload;
map[KEY_TRANSFER_UPSPEED] = sessionStatus.payloadUploadRate; map[KEY_TRANSFER_UPSPEED] = sessionStatus.payloadUploadRate;
map[KEY_TRANSFER_UPDATA] = sessionStatus.totalPayloadUpload; map[KEY_TRANSFER_UPDATA] = sessionStatus.totalPayloadUpload;
map[KEY_TRANSFER_DLRATELIMIT] = BitTorrent::Session::instance()->downloadSpeedLimit(); map[KEY_TRANSFER_DLRATELIMIT] = session->downloadSpeedLimit();
map[KEY_TRANSFER_UPRATELIMIT] = BitTorrent::Session::instance()->uploadSpeedLimit(); map[KEY_TRANSFER_UPRATELIMIT] = session->uploadSpeedLimit();
quint64 atd = BitTorrent::Session::instance()->getAlltimeDL(); const quint64 atd = session->getAlltimeDL();
quint64 atu = BitTorrent::Session::instance()->getAlltimeUL(); const quint64 atu = session->getAlltimeUL();
map[KEY_TRANSFER_ALLTIME_DL] = atd; map[KEY_TRANSFER_ALLTIME_DL] = atd;
map[KEY_TRANSFER_ALLTIME_UL] = atu; map[KEY_TRANSFER_ALLTIME_UL] = atu;
map[KEY_TRANSFER_TOTAL_WASTE_SESSION] = sessionStatus.totalWasted; map[KEY_TRANSFER_TOTAL_WASTE_SESSION] = sessionStatus.totalWasted;
map[KEY_TRANSFER_GLOBAL_RATIO] = ((atd > 0) && (atu > 0)) ? Utils::String::fromDouble(static_cast<qreal>(atu) / atd, 2) : "-"; map[KEY_TRANSFER_GLOBAL_RATIO] = ((atd > 0) && (atu > 0)) ? Utils::String::fromDouble(static_cast<qreal>(atu) / atd, 2) : "-";
map[KEY_TRANSFER_TOTAL_PEER_CONNECTIONS] = sessionStatus.peersCount; map[KEY_TRANSFER_TOTAL_PEER_CONNECTIONS] = sessionStatus.peersCount;
qreal readRatio = cacheStatus.readRatio; const qreal readRatio = cacheStatus.readRatio;
map[KEY_TRANSFER_READ_CACHE_HITS] = (readRatio > 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "0"; map[KEY_TRANSFER_READ_CACHE_HITS] = (readRatio > 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "0";
map[KEY_TRANSFER_TOTAL_BUFFERS_SIZE] = cacheStatus.totalUsedBuffers * 16 * 1024; map[KEY_TRANSFER_TOTAL_BUFFERS_SIZE] = cacheStatus.totalUsedBuffers * 16 * 1024;
// num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake)
const auto torrents = BitTorrent::Session::instance()->torrents(); const auto torrents = session->torrents();
const quint32 peers = std::accumulate(torrents.cbegin(), torrents.cend(), 0, [](const quint32 acc, const BitTorrent::TorrentHandle *torrent) const quint32 peers = std::accumulate(torrents.cbegin(), torrents.cend(), 0, [](const quint32 acc, const BitTorrent::TorrentHandle *torrent)
{ {
return (acc + torrent->peersCount()); return (acc + torrent->peersCount());
@ -149,10 +151,10 @@ namespace
map[KEY_TRANSFER_TOTAL_QUEUED_SIZE] = cacheStatus.queuedBytes; map[KEY_TRANSFER_TOTAL_QUEUED_SIZE] = cacheStatus.queuedBytes;
map[KEY_TRANSFER_DHT_NODES] = sessionStatus.dhtNodes; map[KEY_TRANSFER_DHT_NODES] = sessionStatus.dhtNodes;
if (!BitTorrent::Session::instance()->isListening()) map[KEY_TRANSFER_CONNECTION_STATUS] = session->isListening()
map[KEY_TRANSFER_CONNECTION_STATUS] = "disconnected"; ? (sessionStatus.hasIncomingConnections ? "connected" : "firewalled")
else : "disconnected";
map[KEY_TRANSFER_CONNECTION_STATUS] = sessionStatus.hasIncomingConnections ? "connected" : "firewalled";
return map; return map;
} }
@ -163,11 +165,10 @@ namespace
// initialize output variable // initialize output variable
syncData.clear(); syncData.clear();
QVariantList removedItems;
for (auto i = data.cbegin(); i != data.cend(); ++i) { for (auto i = data.cbegin(); i != data.cend(); ++i) {
const QString &key = i.key(); const QString &key = i.key();
const QVariant &value = i.value(); const QVariant &value = i.value();
removedItems.clear(); QVariantList removedItems;
switch (static_cast<QMetaType::Type>(value.type())) { switch (static_cast<QMetaType::Type>(value.type())) {
case QMetaType::QVariantMap: { case QMetaType::QVariantMap: {
@ -315,7 +316,7 @@ namespace
syncData[KEY_FULL_UPDATE] = true; syncData[KEY_FULL_UPDATE] = true;
} }
lastResponseId = lastResponseId % 1000000 + 1; // cycle between 1 and 1000000 lastResponseId = (lastResponseId % 1000000) + 1; // cycle between 1 and 1000000
lastData = data; lastData = data;
lastData[KEY_RESPONSE_ID] = lastResponseId; lastData[KEY_RESPONSE_ID] = lastResponseId;
syncData[KEY_RESPONSE_ID] = lastResponseId; syncData[KEY_RESPONSE_ID] = lastResponseId;
@ -407,48 +408,57 @@ SyncController::~SyncController()
// - rid (int): last response id // - rid (int): last response id
void SyncController::maindataAction() void SyncController::maindataAction()
{ {
auto lastResponse = sessionManager()->session()->getData(QLatin1String("syncMainDataLastResponse")).toMap(); const auto *session = BitTorrent::Session::instance();
auto lastAcceptedResponse = sessionManager()->session()->getData(QLatin1String("syncMainDataLastAcceptedResponse")).toMap();
QVariantMap data; QVariantMap data;
QVariantMap lastResponse = sessionManager()->session()->getData(QLatin1String("syncMainDataLastResponse")).toMap();
QVariantMap lastAcceptedResponse = sessionManager()->session()->getData(QLatin1String("syncMainDataLastAcceptedResponse")).toMap();
QVariantHash torrents; QVariantHash torrents;
for (const BitTorrent::TorrentHandle *torrent : asConst(session->torrents())) {
const BitTorrent::InfoHash torrentHash = torrent->hash();
BitTorrent::Session *const session = BitTorrent::Session::instance();
for (BitTorrent::TorrentHandle *const torrent : asConst(session->torrents())) {
QVariantMap map = serialize(*torrent); QVariantMap map = serialize(*torrent);
map.remove(KEY_TORRENT_HASH); map.remove(KEY_TORRENT_HASH);
// Calculated last activity time can differ from actual value by up to 10 seconds (this is a libtorrent issue). // Calculated last activity time can differ from actual value by up to 10 seconds (this is a libtorrent issue).
// So we don't need unnecessary updates of last activity time in response. // So we don't need unnecessary updates of last activity time in response.
if (lastResponse.contains("torrents") && lastResponse["torrents"].toHash().contains(torrent->hash()) && const auto iterTorrents = lastResponse.find("torrents");
lastResponse["torrents"].toHash()[torrent->hash()].toMap().contains(KEY_TORRENT_LAST_ACTIVITY_TIME)) { if (iterTorrents != lastResponse.end()) {
uint lastValue = lastResponse["torrents"].toHash()[torrent->hash()].toMap()[KEY_TORRENT_LAST_ACTIVITY_TIME].toUInt(); const QVariantHash lastResponseTorrents = iterTorrents->toHash();
if (qAbs(static_cast<int>(lastValue - map[KEY_TORRENT_LAST_ACTIVITY_TIME].toUInt())) < 15) const auto iterHash = lastResponseTorrents.find(torrentHash);
if (iterHash != lastResponseTorrents.end()) {
const QVariantMap torrentData = iterHash->toMap();
const auto iterLastActivity = torrentData.find(KEY_TORRENT_LAST_ACTIVITY_TIME);
if (iterLastActivity != torrentData.end()) {
const int lastValue = iterLastActivity->toInt();
if (qAbs(lastValue - map[KEY_TORRENT_LAST_ACTIVITY_TIME].toInt()) < 15)
map[KEY_TORRENT_LAST_ACTIVITY_TIME] = lastValue; map[KEY_TORRENT_LAST_ACTIVITY_TIME] = lastValue;
} }
}
torrents[torrent->hash()] = map;
} }
torrents[torrentHash] = map;
}
data["torrents"] = torrents; data["torrents"] = torrents;
QVariantHash categories; QVariantHash categories;
const auto &categoriesList = session->categories(); const auto &categoriesList = session->categories();
for (auto it = categoriesList.cbegin(); it != categoriesList.cend(); ++it) { for (auto it = categoriesList.cbegin(); it != categoriesList.cend(); ++it) {
const auto &key = it.key(); const QString &key = it.key();
categories[key] = QVariantMap { categories[key] = QVariantMap {
{"name", key}, {"name", key},
{"savePath", it.value()} {"savePath", it.value()}
}; };
} }
data["categories"] = categories; data["categories"] = categories;
QVariantList tags; QVariantList tags;
for (const QString &tag : asConst(session->tags())) for (const QString &tag : asConst(session->tags()))
tags << tag; tags << tag;
data["tags"] = tags; data["tags"] = tags;
QVariantMap serverState = getTranserInfo(); QVariantMap serverState = getTranserInfo();
@ -474,13 +484,14 @@ void SyncController::torrentPeersAction()
auto lastAcceptedResponse = sessionManager()->session()->getData(QLatin1String("syncTorrentPeersLastAcceptedResponse")).toMap(); auto lastAcceptedResponse = sessionManager()->session()->getData(QLatin1String("syncTorrentPeersLastAcceptedResponse")).toMap();
const QString hash {params()["hash"]}; const QString hash {params()["hash"]};
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash); const BitTorrent::TorrentHandle *torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) if (!torrent)
throw APIError(APIErrorType::NotFound); throw APIError(APIErrorType::NotFound);
QVariantMap data; QVariantMap data;
QVariantHash peers; QVariantHash peers;
const QList<BitTorrent::PeerInfo> peersList = torrent->peers(); const QList<BitTorrent::PeerInfo> peersList = torrent->peers();
#ifndef DISABLE_COUNTRIES_RESOLUTION #ifndef DISABLE_COUNTRIES_RESOLUTION
bool resolvePeerCountries = Preferences::instance()->resolvePeerCountries(); bool resolvePeerCountries = Preferences::instance()->resolvePeerCountries();
#else #else
@ -491,6 +502,7 @@ void SyncController::torrentPeersAction()
for (const BitTorrent::PeerInfo &pi : peersList) { for (const BitTorrent::PeerInfo &pi : peersList) {
if (pi.address().ip.isNull()) continue; if (pi.address().ip.isNull()) continue;
QVariantMap peer; QVariantMap peer;
#ifndef DISABLE_COUNTRIES_RESOLUTION #ifndef DISABLE_COUNTRIES_RESOLUTION
if (resolvePeerCountries) { if (resolvePeerCountries) {
@ -514,7 +526,6 @@ void SyncController::torrentPeersAction()
peers[pi.address().ip.toString() + ':' + QString::number(pi.address().port)] = peer; peers[pi.address().ip.toString() + ':' + QString::number(pi.address().port)] = peer;
} }
data["peers"] = peers; data["peers"] = peers;
const int acceptedResponseId {params()["rid"].toInt()}; const int acceptedResponseId {params()["rid"].toInt()};