Browse Source

Merge pull request #10 from BotanEgg/master

cache end iterators before loops (optimisation)
adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
7cffd267d8
  1. 6
      src/fs_utils.cpp
  2. 6
      src/properties/peerlistwidget.cpp
  3. 6
      src/properties/trackerlist.cpp
  4. 4
      src/properties/trackersadditiondlg.h
  5. 108
      src/qtlibtorrent/qbtsession.cpp
  6. 11
      src/qtlibtorrent/qtorrenthandle.cpp
  7. 18
      src/qtlibtorrent/torrentmodel.cpp
  8. 6
      src/qtlibtorrent/torrentspeedmonitor.cpp
  9. 11
      src/torrentpersistentdata.h

6
src/fs_utils.cpp

@ -214,8 +214,10 @@ QString fsutils::fixFileNames(const QString& path) @@ -214,8 +214,10 @@ QString fsutils::fixFileNames(const QString& path)
QList<QByteArray> parts = raw_path.split('/');
if (parts.isEmpty()) return path;
QByteArray last_part = parts.takeLast();
QList<QByteArray>::iterator it;
for (it = parts.begin(); it != parts.end(); ++it) {
QList<QByteArray>::iterator it = parts.begin();
QList<QByteArray>::iterator itend = parts.end();
for ( ; it != itend; ++it) {
// Make sure the filename is not too long
if (it->size() > MAX_FILENAME_BYTES) {
qWarning() << "Folder" << *it << "was cut because it was too long";

6
src/properties/peerlistwidget.cpp

@ -310,9 +310,11 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso @@ -310,9 +310,11 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso
boost::system::error_code ec;
std::vector<peer_info> peers;
h.get_peer_info(peers);
std::vector<peer_info>::const_iterator itr;
QSet<QString> old_peers_set = m_peerItems.keys().toSet();
for (itr = peers.begin(); itr != peers.end(); ++itr) {
std::vector<peer_info>::const_iterator itr = peers.begin();
std::vector<peer_info>::const_iterator itrend = peers.end();
for ( ; itr != itrend; ++itr) {
peer_info peer = *itr;
QString peer_ip = misc::toQString(peer.ip.address().to_string(ec));
if (ec) continue;

6
src/properties/trackerlist.cpp

@ -312,8 +312,10 @@ void TrackerList::deleteSelectedTrackers() { @@ -312,8 +312,10 @@ void TrackerList::deleteSelectedTrackers() {
// Iterate of trackers and remove selected ones
std::vector<announce_entry> remaining_trackers;
std::vector<announce_entry> trackers = h.trackers();
std::vector<announce_entry>::iterator it;
for (it = trackers.begin(); it != trackers.end(); ++it) {
std::vector<announce_entry>::iterator it = trackers.begin();
std::vector<announce_entry>::iterator itend = trackers.end();
for ( ; it != itend; ++it) {
if (!urls_to_remove.contains(misc::toQString((*it).url))) {
remaining_trackers.push_back(*it);
}

4
src/properties/trackersadditiondlg.h

@ -87,8 +87,10 @@ public slots: @@ -87,8 +87,10 @@ public slots:
QList<QUrl> existingTrackers;
// Load from torrent handle
std::vector<libtorrent::announce_entry> tor_trackers = h.trackers();
std::vector<libtorrent::announce_entry>::iterator itr = tor_trackers.begin();
while(itr != tor_trackers.end()) {
std::vector<libtorrent::announce_entry>::iterator itrend = tor_trackers.end();
while(itr != itrend) {
existingTrackers << QUrl(misc::toQString(itr->url));
++itr;
}

108
src/qtlibtorrent/qbtsession.cpp

@ -212,8 +212,10 @@ void QBtSession::preAllocateAllFiles(bool b) { @@ -212,8 +212,10 @@ void QBtSession::preAllocateAllFiles(bool b) {
void QBtSession::processBigRatios() {
qDebug("Process big ratios...");
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
const QTorrentHandle h(*torrentIT);
if (!h.is_valid()) continue;
if (h.is_seed()) {
@ -359,8 +361,10 @@ void QBtSession::configureSession() { @@ -359,8 +361,10 @@ void QBtSession::configureSession() {
}
// Update torrent handles
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (h.is_valid())
h.resolve_countries(resolve_countries);
@ -711,8 +715,10 @@ QTorrentHandle QBtSession::getTorrentHandle(const QString &hash) const { @@ -711,8 +715,10 @@ QTorrentHandle QBtSession::getTorrentHandle(const QString &hash) const {
bool QBtSession::hasActiveTorrents() const {
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
const QTorrentHandle h(*torrentIT);
if (h.is_valid() && !h.is_paused() && !h.is_queued())
return true;
@ -722,8 +728,10 @@ bool QBtSession::hasActiveTorrents() const { @@ -722,8 +728,10 @@ bool QBtSession::hasActiveTorrents() const {
bool QBtSession::hasDownloadingTorrents() const {
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
if (torrentIT->is_valid()) {
try {
const torrent_status::state_t state = torrentIT->status().state;
@ -802,8 +810,10 @@ void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) { @@ -802,8 +810,10 @@ void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) {
void QBtSession::pauseAllTorrents() {
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
try {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (!h.is_paused()) {
@ -820,8 +830,10 @@ std::vector<torrent_handle> QBtSession::getTorrents() const { @@ -820,8 +830,10 @@ std::vector<torrent_handle> QBtSession::getTorrents() const {
void QBtSession::resumeAllTorrents() {
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
try {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (h.is_paused()) {
@ -1324,8 +1336,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torren @@ -1324,8 +1336,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torren
const QStringList old_urlseeds = h_ex.url_seeds();
#if LIBTORRENT_VERSION_MINOR > 15
std::vector<web_seed_entry> new_urlseeds = t->web_seeds();
std::vector<web_seed_entry>::iterator it;
for (it = new_urlseeds.begin(); it != new_urlseeds.end(); it++) {
std::vector<web_seed_entry>::iterator it = new_urlseeds.begin();
std::vector<web_seed_entry>::iterator itend = new_urlseeds.end();
for ( ; it != itend; ++it) {
const QString new_url = misc::toQString(it->url.c_str());
if (!old_urlseeds.contains(new_url)) {
urlseeds_added = true;
@ -1334,8 +1348,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torren @@ -1334,8 +1348,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torren
}
#else
std::vector<std::string> new_urlseeds = t->url_seeds();
std::vector<std::string>::iterator it;
for (it = new_urlseeds.begin(); it != new_urlseeds.end(); ++it) {
std::vector<std::string>::iterator it = new_urlseeds.begin();
std::vector<std::string>::iterator itend = new_urlseeds.end();
for ( ; it != itend; ++it) {
const QString new_url = misc::toQString(it->c_str());
if (!old_urlseeds.contains(new_url)) {
urlseeds_added = true;
@ -1358,8 +1374,10 @@ void QBtSession::exportTorrentFiles(QString path) { @@ -1358,8 +1374,10 @@ void QBtSession::exportTorrentFiles(QString path) {
}
QDir torrentBackup(fsutils::BTBackupLocation());
std::vector<torrent_handle> handles = s->get_torrents();
std::vector<torrent_handle>::iterator itr;
for (itr=handles.begin(); itr != handles.end(); ++itr) {
std::vector<torrent_handle>::iterator itr=handles.begin();
std::vector<torrent_handle>::iterator itrend=handles.end();
for ( ; itr != itrend; ++itr) {
const QTorrentHandle h(*itr);
if (!h.is_valid()) {
std::cerr << "Torrent Export: torrent is invalid, skipping..." << std::endl;
@ -1399,8 +1417,10 @@ void QBtSession::setMaxConnectionsPerTorrent(int max) { @@ -1399,8 +1417,10 @@ void QBtSession::setMaxConnectionsPerTorrent(int max) {
qDebug() << Q_FUNC_INFO << max;
// Apply this to all session torrents
std::vector<torrent_handle> handles = s->get_torrents();
std::vector<torrent_handle>::const_iterator it;
for (it = handles.begin(); it != handles.end(); ++it) {
std::vector<torrent_handle>::const_iterator it = handles.begin();
std::vector<torrent_handle>::const_iterator itend = handles.end();
for ( ; it != itend; ++it) {
if (!it->is_valid())
continue;
try {
@ -1413,8 +1433,10 @@ void QBtSession::setMaxUploadsPerTorrent(int max) { @@ -1413,8 +1433,10 @@ void QBtSession::setMaxUploadsPerTorrent(int max) {
qDebug() << Q_FUNC_INFO << max;
// Apply this to all session torrents
std::vector<torrent_handle> handles = s->get_torrents();
std::vector<torrent_handle>::const_iterator it;
for (it = handles.begin(); it != handles.end(); ++it) {
std::vector<torrent_handle>::const_iterator it = handles.begin();
std::vector<torrent_handle>::const_iterator itend = handles.end();
for ( ; it != itend; ++it) {
if (!it->is_valid())
continue;
try {
@ -1564,8 +1586,10 @@ qreal QBtSession::getRealRatio(const QString &hash) const { @@ -1564,8 +1586,10 @@ qreal QBtSession::getRealRatio(const QString &hash) const {
// Called periodically
void QBtSession::saveTempFastResumeData() {
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
try {
if (!h.is_valid() || !h.has_metadata() /*|| h.is_seed() || h.is_paused()*/) continue;
@ -1590,8 +1614,10 @@ void QBtSession::saveFastResumeData() { @@ -1590,8 +1614,10 @@ void QBtSession::saveFastResumeData() {
// Pause session
s->pause();
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (!h.is_valid() || !h.has_metadata()) continue;
try {
@ -1726,8 +1752,10 @@ void QBtSession::setDefaultTempPath(QString temppath) { @@ -1726,8 +1752,10 @@ void QBtSession::setDefaultTempPath(QString temppath) {
// Disabling temp dir
// Moving all torrents to their destination folder
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (!h.is_valid()) continue;
h.move_storage(getSavePath(h.hash()));
@ -1736,8 +1764,10 @@ void QBtSession::setDefaultTempPath(QString temppath) { @@ -1736,8 +1764,10 @@ void QBtSession::setDefaultTempPath(QString temppath) {
qDebug("Enabling default temp path...");
// Moving all downloading torrents to temporary save path
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if (!h.is_valid()) continue;
if (!h.is_seed()) {
@ -1811,8 +1841,10 @@ void QBtSession::setAppendLabelToSavePath(bool append) { @@ -1811,8 +1841,10 @@ void QBtSession::setAppendLabelToSavePath(bool append) {
if (appendLabelToSavePath) {
// Move torrents storage to sub folder with label name
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
appendLabelToTorrentSavePath(h);
}
@ -1825,8 +1857,10 @@ void QBtSession::setAppendqBExtension(bool append) { @@ -1825,8 +1857,10 @@ void QBtSession::setAppendqBExtension(bool append) {
appendqBExtension = !appendqBExtension;
// append or remove .!qB extension for incomplete files
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator torrentIT;
for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) {
std::vector<torrent_handle>::iterator torrentIT = torrents.begin();
std::vector<torrent_handle>::iterator torrentITend = torrents.end();
for ( ; torrentIT != torrentITend; ++torrentIT) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
appendqBextensionToTorrent(h, appendqBExtension);
}
@ -2473,8 +2507,10 @@ void QBtSession::readAlerts() { @@ -2473,8 +2507,10 @@ void QBtSession::readAlerts() {
qDebug() << "Sucessfully listening on" << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port();
// Force reannounce on all torrents because some trackers blacklist some ports
std::vector<torrent_handle> torrents = s->get_torrents();
std::vector<torrent_handle>::iterator it;
for (it = torrents.begin(); it != torrents.end(); ++it) {
std::vector<torrent_handle>::iterator it = torrents.begin();
std::vector<torrent_handle>::iterator itend = torrents.end();
for ( ; it != itend; ++it) {
it->force_reannounce();
}
emit listenSucceeded();

11
src/qtlibtorrent/qtorrenthandle.cpp

@ -281,8 +281,10 @@ QStringList QTorrentHandle::url_seeds() const { @@ -281,8 +281,10 @@ QStringList QTorrentHandle::url_seeds() const {
QStringList res;
try {
const std::set<std::string> existing_seeds = torrent_handle::url_seeds();
std::set<std::string>::const_iterator it;
for (it = existing_seeds.begin(); it != existing_seeds.end(); ++it) {
std::set<std::string>::const_iterator it = existing_seeds.begin();
std::set<std::string>::const_iterator itend = existing_seeds.end();
for ( ; it != itend; ++it) {
qDebug("URL Seed: %s", it->c_str());
res << misc::toQString(*it);
}
@ -582,7 +584,10 @@ QString QTorrentHandle::error() const { @@ -582,7 +584,10 @@ QString QTorrentHandle::error() const {
void QTorrentHandle::downloading_pieces(bitfield &bf) const {
std::vector<partial_piece_info> queue;
torrent_handle::get_download_queue(queue);
for (std::vector<partial_piece_info>::const_iterator it=queue.begin(); it!= queue.end(); ++it) {
std::vector<partial_piece_info>::const_iterator it = queue.begin();
std::vector<partial_piece_info>::const_iterator itend = queue.end();
for ( ; it!= itend; ++it) {
bf.set_bit(it->piece_index);
}
return;

18
src/qtlibtorrent/torrentmodel.cpp

@ -205,8 +205,10 @@ TorrentModel::TorrentModel(QObject *parent) : @@ -205,8 +205,10 @@ TorrentModel::TorrentModel(QObject *parent) :
void TorrentModel::populate() {
// Load the torrents
std::vector<torrent_handle> torrents = QBtSession::instance()->getSession()->get_torrents();
std::vector<torrent_handle>::const_iterator it;
for (it = torrents.begin(); it != torrents.end(); ++it) {
std::vector<torrent_handle>::const_iterator it = torrents.begin();
std::vector<torrent_handle>::const_iterator itend = torrents.end();
for ( ; it != itend; ++it) {
addTorrent(QTorrentHandle(*it));
}
// Refresh timer
@ -313,9 +315,11 @@ bool TorrentModel::setData(const QModelIndex &index, const QVariant &value, int @@ -313,9 +315,11 @@ bool TorrentModel::setData(const QModelIndex &index, const QVariant &value, int
int TorrentModel::torrentRow(const QString &hash) const
{
QList<TorrentModelItem*>::const_iterator it;
int row = 0;
for (it = m_torrents.constBegin(); it != m_torrents.constEnd(); ++it) {
QList<TorrentModelItem*>::const_iterator it = m_torrents.constBegin();
QList<TorrentModelItem*>::const_iterator itend = m_torrents.constEnd();
for ( ; it != itend; ++it) {
if ((*it)->hash() == hash) return row;
++row;
}
@ -395,8 +399,10 @@ void TorrentModel::forceModelRefresh() @@ -395,8 +399,10 @@ void TorrentModel::forceModelRefresh()
TorrentStatusReport TorrentModel::getTorrentStatusReport() const
{
TorrentStatusReport report;
QList<TorrentModelItem*>::const_iterator it;
for (it = m_torrents.constBegin(); it != m_torrents.constEnd(); ++it) {
QList<TorrentModelItem*>::const_iterator it = m_torrents.constBegin();
QList<TorrentModelItem*>::const_iterator itend = m_torrents.constEnd();
for ( ; it != itend; ++it) {
switch((*it)->data(TorrentModelItem::TR_STATUS).toInt()) {
case TorrentModelItem::STATE_DOWNLOADING:
++report.nb_active;

6
src/qtlibtorrent/torrentspeedmonitor.cpp

@ -122,8 +122,10 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const @@ -122,8 +122,10 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const
void TorrentSpeedMonitor::getSamples()
{
const std::vector<torrent_handle> torrents = m_session->getSession()->get_torrents();
std::vector<torrent_handle>::const_iterator it;
for (it = torrents.begin(); it != torrents.end(); ++it) {
std::vector<torrent_handle>::const_iterator it = torrents.begin();
std::vector<torrent_handle>::const_iterator itend = torrents.end();
for ( ; it != itend; ++it) {
try {
#if LIBTORRENT_VERSION_MINOR > 15
torrent_status st = it->status(0x0);

11
src/torrentpersistentdata.h

@ -63,9 +63,11 @@ public: @@ -63,9 +63,11 @@ public:
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
QHash<QString, QVariant> all_data = settings.value("torrents-tmp").toHash();
QHash<QString, QVariant> data = all_data.value(hash).toHash();
std::vector<int>::const_iterator pp_it = pp.begin();
QStringList pieces_priority;
while(pp_it != pp.end()) {
std::vector<int>::const_iterator pp_it = pp.begin();
std::vector<int>::const_iterator pp_itend = pp.end();
while(pp_it != pp_itend) {
pieces_priority << QString::number(*pp_it);
++pp_it;
}
@ -206,8 +208,9 @@ public: @@ -206,8 +208,9 @@ public:
static bool hasPerTorrentRatioLimit() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume"));
const QHash<QString, QVariant> all_data = settings.value("torrents").toHash();
QHash<QString, QVariant>::ConstIterator it;
for (it = all_data.constBegin(); it != all_data.constEnd(); it++) {
QHash<QString, QVariant>::ConstIterator it = all_data.constBegin();
QHash<QString, QVariant>::ConstIterator itend = all_data.constEnd();
for ( ; it != itend; ++it) {
if (it.value().toHash().value("max_ratio", USE_GLOBAL_RATIO).toReal() >= 0) {
return true;
}

Loading…
Cancel
Save