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 <marc.mutz@kdab.com>
Change-Id: Ib154f80f46f8041d9cafd81bed0e1982b21541cf
Reviewed-by: Edward Welbourne <edward.welbourne@theqtcompany.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2016-03-04 10:51:12 +03:00
parent 7094466f7d
commit 35b0ecf5da
4 changed files with 54 additions and 52 deletions

View File

@ -384,25 +384,21 @@ QList<QNetworkConfiguration> QNetworkConfiguration::children() const
if (d->type != QNetworkConfiguration::ServiceNetwork || !d->isValid) if (d->type != QNetworkConfiguration::ServiceNetwork || !d->isValid)
return results; return results;
QMutableMapIterator<unsigned int, QNetworkConfigurationPrivatePointer> i(d->serviceNetworkMembers); for (auto it = d->serviceNetworkMembers.begin(), end = d->serviceNetworkMembers.end(); it != end;) {
while (i.hasNext()) { QNetworkConfigurationPrivatePointer p = it.value();
i.next();
QNetworkConfigurationPrivatePointer p = i.value();
//if we have an invalid member get rid of it -> was deleted earlier on //if we have an invalid member get rid of it -> was deleted earlier on
{ {
QMutexLocker childLocker(&p->mutex); QMutexLocker childLocker(&p->mutex);
if (!p->isValid) { if (!p->isValid) {
i.remove(); it = d->serviceNetworkMembers.erase(it);
continue; continue;
} }
} }
QNetworkConfiguration item; QNetworkConfiguration item;
item.d = p; item.d = p;
results << item; results << item;
++it;
} }
return results; return results;

View File

@ -79,6 +79,22 @@ bool any_of(InputIt first, InputIt last, UnaryPredicate p)
{ {
return std::find_if(first, last, p) != last; return std::find_if(first, last, p) != last;
} }
template <typename InputIt, typename OutputIt1, typename OutputIt2, typename UnaryPredicate>
std::pair<OutputIt1, OutputIt2> 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(); finishedLookups.clear();
} }
if (!postponedLookups.isEmpty()) { auto isAlreadyRunning = [this](QHostInfoRunnable *lookup) {
// try to start the postponed ones return any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(lookup->toBeLookedUp));
};
QMutableListIterator<QHostInfoRunnable*> iterator(postponedLookups); // Transfer any postponed lookups that aren't currently running to the scheduled list, keeping already-running lookups:
while (iterator.hasNext()) { postponedLookups.erase(separate_if(postponedLookups.begin(),
QHostInfoRunnable* postponed = iterator.next(); postponedLookups.end(),
postponedLookups.begin(),
std::front_inserter(scheduledLookups), // prepend! we want to finish it ASAP
isAlreadyRunning).first,
postponedLookups.end());
// check if none of the postponed hostnames is currently running // Unschedule and postpone any that are currently running:
const bool alreadyRunning = any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(postponed->toBeLookedUp)); scheduledLookups.erase(separate_if(scheduledLookups.begin(),
if (!alreadyRunning) { scheduledLookups.end(),
iterator.remove(); std::back_inserter(postponedLookups),
scheduledLookups.prepend(postponed); // prepend! we want to finish it ASAP 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;
} }
} scheduledLookups.erase(scheduledLookups.begin(), it);
if (!scheduledLookups.isEmpty()) {
// try to start the new ones
QMutableListIterator<QHostInfoRunnable*> 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;
}
};
} }
} }

View File

@ -396,12 +396,12 @@ void QSocks5BindStore::timerEvent(QTimerEvent * event)
QMutexLocker lock(&mutex); QMutexLocker lock(&mutex);
if (event->timerId() == sweepTimerId) { if (event->timerId() == sweepTimerId) {
QSOCKS5_DEBUG << "QSocks5BindStore performing sweep"; QSOCKS5_DEBUG << "QSocks5BindStore performing sweep";
QMutableHashIterator<int, QSocks5BindData *> it(store); for (auto it = store.begin(), end = store.end(); it != end;) {
while (it.hasNext()) {
it.next();
if (it.value()->timeStamp.hasExpired(350000)) { if (it.value()->timeStamp.hasExpired(350000)) {
QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ"; QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ";
it.remove(); it = store.erase(it);
} else {
++it;
} }
} }
} }

View File

@ -747,9 +747,8 @@ QList<QSslCertificate> QSslSocketPrivate::systemCaCertificates()
certFiles.insert(it.fileInfo().canonicalFilePath()); certFiles.insert(it.fileInfo().canonicalFilePath());
} }
} }
QSetIterator<QString> it(certFiles); for (const QString& file : qAsConst(certFiles))
while (it.hasNext()) systemCerts.append(QSslCertificate::fromPath(file, platformEncodingFormat));
systemCerts.append(QSslCertificate::fromPath(it.next(), platformEncodingFormat));
# ifndef Q_OS_ANDROID # 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("/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 systemCerts.append(QSslCertificate::fromPath(QLatin1String("/usr/local/share/certs/ca-root-nss.crt"), QSsl::Pem)); // FreeBSD's ca_root_nss