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:
parent
8de6752496
commit
e83f6aaaa6
@ -217,22 +217,24 @@ bool QMimeBinaryProvider::isValid()
|
|||||||
bool QMimeBinaryProvider::CacheFileList::checkCacheChanged()
|
bool QMimeBinaryProvider::CacheFileList::checkCacheChanged()
|
||||||
{
|
{
|
||||||
bool somethingChanged = false;
|
bool somethingChanged = false;
|
||||||
QMutableListIterator<CacheFile *> it(*this);
|
for (CacheFile *cacheFile : qAsConst(*this)) {
|
||||||
while (it.hasNext()) {
|
|
||||||
CacheFile *cacheFile = it.next();
|
|
||||||
QFileInfo fileInfo(cacheFile->file);
|
QFileInfo fileInfo(cacheFile->file);
|
||||||
if (!fileInfo.exists()) { // This can't happen by just running update-mime-database. But the user could use rm -rf :-)
|
if (!fileInfo.exists() || fileInfo.lastModified() > cacheFile->m_mtime) {
|
||||||
delete cacheFile;
|
// Deletion can't happen by just running update-mime-database.
|
||||||
it.remove();
|
// But the user could use rm -rf :-)
|
||||||
somethingChanged = true;
|
cacheFile->reload(); // will mark itself as invalid on failure
|
||||||
} else if (fileInfo.lastModified() > cacheFile->m_mtime) {
|
|
||||||
if (!cacheFile->reload()) {
|
|
||||||
delete cacheFile;
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
somethingChanged = true;
|
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;
|
return somethingChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user