From 35b0ecf5da40feac0e7b43d928b60402d75d95d1 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Fri, 4 Mar 2016 10:51:12 +0300 Subject: QtNetwork: replace Java-style iterators ... with STL-style iterators or with algorithms. Java-style iterators have overhead. Introduce local template separate_if algorithm from kleopatra project to simplify current code. http://api.kde.org/4.3-api/kdepim-apidocs/kleopatra/html Done-with: Marc Mutz Change-Id: Ib154f80f46f8041d9cafd81bed0e1982b21541cf Reviewed-by: Edward Welbourne Reviewed-by: Marc Mutz --- src/network/bearer/qnetworkconfiguration.cpp | 12 ++-- src/network/kernel/qhostinfo.cpp | 85 +++++++++++++++------------- src/network/socket/qsocks5socketengine.cpp | 8 +-- src/network/ssl/qsslsocket_openssl.cpp | 5 +- 4 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/network/bearer/qnetworkconfiguration.cpp b/src/network/bearer/qnetworkconfiguration.cpp index 3a0feb7d13..533a27357c 100644 --- a/src/network/bearer/qnetworkconfiguration.cpp +++ b/src/network/bearer/qnetworkconfiguration.cpp @@ -384,25 +384,21 @@ QList QNetworkConfiguration::children() const if (d->type != QNetworkConfiguration::ServiceNetwork || !d->isValid) return results; - QMutableMapIterator i(d->serviceNetworkMembers); - while (i.hasNext()) { - i.next(); - - QNetworkConfigurationPrivatePointer p = i.value(); - + for (auto it = d->serviceNetworkMembers.begin(), end = d->serviceNetworkMembers.end(); it != end;) { + QNetworkConfigurationPrivatePointer p = it.value(); //if we have an invalid member get rid of it -> was deleted earlier on { QMutexLocker childLocker(&p->mutex); if (!p->isValid) { - i.remove(); + it = d->serviceNetworkMembers.erase(it); continue; } } - QNetworkConfiguration item; item.d = p; results << item; + ++it; } return results; diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp index 293633d6bc..88df65dbcb 100644 --- a/src/network/kernel/qhostinfo.cpp +++ b/src/network/kernel/qhostinfo.cpp @@ -79,6 +79,22 @@ bool any_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if(first, last, p) != last; } + +template +std::pair separate_if(InputIt first, InputIt last, OutputIt1 dest1, OutputIt2 dest2, UnaryPredicate p) +{ + while (first != last) { + if (p(*first)) { + *dest1 = *first; + ++dest1; + } else { + *dest2 = *first; + ++dest2; + } + ++first; + } + return std::make_pair(dest1, dest2); +} } /*! @@ -587,46 +603,37 @@ void QHostInfoLookupManager::work() finishedLookups.clear(); } - if (!postponedLookups.isEmpty()) { - // try to start the postponed ones - - QMutableListIterator iterator(postponedLookups); - while (iterator.hasNext()) { - QHostInfoRunnable* postponed = iterator.next(); - - // check if none of the postponed hostnames is currently running - const bool alreadyRunning = any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(postponed->toBeLookedUp)); - if (!alreadyRunning) { - iterator.remove(); - scheduledLookups.prepend(postponed); // prepend! we want to finish it ASAP - } + auto isAlreadyRunning = [this](QHostInfoRunnable *lookup) { + return any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(lookup->toBeLookedUp)); + }; + + // Transfer any postponed lookups that aren't currently running to the scheduled list, keeping already-running lookups: + postponedLookups.erase(separate_if(postponedLookups.begin(), + postponedLookups.end(), + postponedLookups.begin(), + std::front_inserter(scheduledLookups), // prepend! we want to finish it ASAP + isAlreadyRunning).first, + postponedLookups.end()); + + // Unschedule and postpone any that are currently running: + scheduledLookups.erase(separate_if(scheduledLookups.begin(), + scheduledLookups.end(), + std::back_inserter(postponedLookups), + scheduledLookups.begin(), + isAlreadyRunning).second, + scheduledLookups.end()); + + const int availableThreads = threadPool.maxThreadCount() - currentLookups.size(); + if (availableThreads > 0) { + int readyToStartCount = qMin(availableThreads, scheduledLookups.size()); + auto it = scheduledLookups.begin(); + while (readyToStartCount--) { + // runnable now running in new thread, track this in currentLookups + threadPool.start(*it); + currentLookups.push_back(std::move(*it)); + ++it; } - } - - if (!scheduledLookups.isEmpty()) { - // try to start the new ones - QMutableListIterator iterator(scheduledLookups); - while (iterator.hasNext()) { - QHostInfoRunnable *scheduled = iterator.next(); - - // check if a lookup for this host is already running, then postpone - const bool alreadyRunning = any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(scheduled->toBeLookedUp)); - if (alreadyRunning) { - iterator.remove(); - postponedLookups.append(scheduled); - scheduled = 0; - } - - if (scheduled && currentLookups.size() < threadPool.maxThreadCount()) { - // runnable now running in new thread, track this in currentLookups - threadPool.start(scheduled); - iterator.remove(); - currentLookups.append(scheduled); - } else { - // was postponed, continue iterating - continue; - } - }; + scheduledLookups.erase(scheduledLookups.begin(), it); } } diff --git a/src/network/socket/qsocks5socketengine.cpp b/src/network/socket/qsocks5socketengine.cpp index 0c15810a48..eb9264ba20 100644 --- a/src/network/socket/qsocks5socketengine.cpp +++ b/src/network/socket/qsocks5socketengine.cpp @@ -396,12 +396,12 @@ void QSocks5BindStore::timerEvent(QTimerEvent * event) QMutexLocker lock(&mutex); if (event->timerId() == sweepTimerId) { QSOCKS5_DEBUG << "QSocks5BindStore performing sweep"; - QMutableHashIterator it(store); - while (it.hasNext()) { - it.next(); + for (auto it = store.begin(), end = store.end(); it != end;) { if (it.value()->timeStamp.hasExpired(350000)) { QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ"; - it.remove(); + it = store.erase(it); + } else { + ++it; } } } diff --git a/src/network/ssl/qsslsocket_openssl.cpp b/src/network/ssl/qsslsocket_openssl.cpp index 4f62f53a93..0200f5e70b 100644 --- a/src/network/ssl/qsslsocket_openssl.cpp +++ b/src/network/ssl/qsslsocket_openssl.cpp @@ -747,9 +747,8 @@ QList QSslSocketPrivate::systemCaCertificates() certFiles.insert(it.fileInfo().canonicalFilePath()); } } - QSetIterator it(certFiles); - while (it.hasNext()) - systemCerts.append(QSslCertificate::fromPath(it.next(), platformEncodingFormat)); + for (const QString& file : qAsConst(certFiles)) + systemCerts.append(QSslCertificate::fromPath(file, platformEncodingFormat)); # ifndef Q_OS_ANDROID systemCerts.append(QSslCertificate::fromPath(QLatin1String("/etc/pki/tls/certs/ca-bundle.crt"), QSsl::Pem)); // Fedora, Mandriva systemCerts.append(QSslCertificate::fromPath(QLatin1String("/usr/local/share/certs/ca-root-nss.crt"), QSsl::Pem)); // FreeBSD's ca_root_nss -- cgit v1.2.3