diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-12-23 13:26:32 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-08-25 04:14:24 +0000 |
commit | e83f6aaaa6b1caa59e4dd2c304439e1fb2572d71 (patch) | |
tree | d68b489779a124484407537e9b180c0c3dc4845a /src/corelib/mimetypes/qmimeprovider.cpp | |
parent | 8de67524961d2ebae3777dbfc54fe8cea7300cbc (diff) |
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>
Diffstat (limited to 'src/corelib/mimetypes/qmimeprovider.cpp')
-rw-r--r-- | src/corelib/mimetypes/qmimeprovider.cpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp index e72f9a30d0..e25eb4272d 100644 --- a/src/corelib/mimetypes/qmimeprovider.cpp +++ b/src/corelib/mimetypes/qmimeprovider.cpp @@ -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; } |