QMimeProvider: fix quadratic loop

Calling QMutableListIterator::remove() in a loop constitutes quadratic
behavior (O(N) function called O(N) times).

Fix by splitting the loop, simplifying it by sharing conditions, and
using std::remove_if(), which is linear.

Removes one more use of mutable Java iterators.

Change-Id: I88bde414777b50996e546bc8cb238619ea4fb645
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Marc Mutz 2015-12-23 13:26:32 +01:00
parent 8de6752496
commit e83f6aaaa6

View File

@ -217,22 +217,24 @@ bool QMimeBinaryProvider::isValid()
bool QMimeBinaryProvider::CacheFileList::checkCacheChanged()
{
bool somethingChanged = false;
QMutableListIterator<CacheFile *> it(*this);
while (it.hasNext()) {
CacheFile *cacheFile = it.next();
for (CacheFile *cacheFile : qAsConst(*this)) {
QFileInfo fileInfo(cacheFile->file);
if (!fileInfo.exists()) { // This can't happen by just running update-mime-database. But the user could use rm -rf :-)
delete cacheFile;
it.remove();
somethingChanged = true;
} else if (fileInfo.lastModified() > cacheFile->m_mtime) {
if (!cacheFile->reload()) {
delete cacheFile;
it.remove();
}
if (!fileInfo.exists() || fileInfo.lastModified() > cacheFile->m_mtime) {
// Deletion can't happen by just running update-mime-database.
// But the user could use rm -rf :-)
cacheFile->reload(); // will mark itself as invalid on failure
somethingChanged = true;
}
}
if (somethingChanged) {
auto deleteIfNoLongerValid = [](CacheFile *cacheFile) -> bool {
const bool invalid = !cacheFile->isValid();
if (invalid)
delete cacheFile;
return invalid;
};
erase(std::remove_if(begin(), end(), deleteIfNoLongerValid), end());
}
return somethingChanged;
}