diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-05-19 23:25:12 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-05-21 21:57:20 +0200 |
commit | b03385f9cff7acc2b37933f493e3eff2d8bbef59 (patch) | |
tree | be519e92d527bc3878c2b58d3d006f099019dd83 /src/corelib/io/qfilesystemwatcher_win.cpp | |
parent | 4e026a2076dd6f612871e30fdcf49312b4647846 (diff) |
QFileSystemWatcherEngines: port some Java-style iterators to ranged-for
Java-style iterators are scheduled to be deprecated.
The general pattern used in the patch is that instead of copying an
input list, then iterating over the copy with some calls to
it.remove() (which leads to quadratic-complexity loops), we simply
copy conditionally (a la remove_copy_if instead of remove_if).
To make clearer what's going on, rename the outgoing list to
'unhandled'.
To avoid having to touch too much of the loops' structure, which
sometimes is quite convoluted, use qScopeGuard to do the append to
'unhandled', unless the original code removed the element.
Saves a surprising almost 5KiB in text size on GCC 9.1 optimized
AMD64 Linux builds.
Change-Id: Ifd861de9aa48d66b420858606998dd08a8401e03
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'src/corelib/io/qfilesystemwatcher_win.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemwatcher_win.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/corelib/io/qfilesystemwatcher_win.cpp b/src/corelib/io/qfilesystemwatcher_win.cpp index eb626fd541..aadfe7963d 100644 --- a/src/corelib/io/qfilesystemwatcher_win.cpp +++ b/src/corelib/io/qfilesystemwatcher_win.cpp @@ -44,6 +44,7 @@ #include <qfileinfo.h> #include <qstringlist.h> #include <qset.h> +#include <qscopeguard.h> #include <qdatetime.h> #include <qdir.h> #include <qtextstream.h> @@ -377,10 +378,9 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, QStringList *directories) { DEBUG() << "Adding" << paths.count() << "to existing" << (files->count() + directories->count()) << "watchers"; - QStringList p = paths; - QMutableListIterator<QString> it(p); - while (it.hasNext()) { - QString path = it.next(); + QStringList unhandled; + for (const QString &path : paths) { + auto sg = qScopeGuard([&] { unhandled.push_back(path); }); QString normalPath = path; if ((normalPath.endsWith(QLatin1Char('/')) && !normalPath.endsWith(QLatin1String(":/"))) || (normalPath.endsWith(QLatin1Char('\\')) && !normalPath.endsWith(QLatin1String(":\\")))) { @@ -463,7 +463,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, else files->append(path); } - it.remove(); + sg.dismiss(); thread->wakeup(); break; } @@ -493,7 +493,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, else files->append(path); - it.remove(); + sg.dismiss(); found = true; thread->wakeup(); break; @@ -519,7 +519,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, thread->msg = '@'; thread->start(); threads.append(thread); - it.remove(); + sg.dismiss(); } } } @@ -527,12 +527,12 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, #ifndef Q_OS_WINRT if (Q_LIKELY(m_driveListener)) { for (const QString &path : paths) { - if (!p.contains(path)) + if (!unhandled.contains(path)) m_driveListener->addPath(path); } } #endif // !Q_OS_WINRT - return p; + return unhandled; } QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &paths, @@ -540,10 +540,9 @@ QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &path QStringList *directories) { DEBUG() << "removePaths" << paths; - QStringList p = paths; - QMutableListIterator<QString> it(p); - while (it.hasNext()) { - QString path = it.next(); + QStringList unhandled; + for (const QString &path : paths) { + auto sg = qScopeGuard([&] { unhandled.push_back(path); }); QString normalPath = path; if (normalPath.endsWith(QLatin1Char('/')) || normalPath.endsWith(QLatin1Char('\\'))) normalPath.chop(1); @@ -572,7 +571,7 @@ QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &path // ### files->removeAll(path); directories->removeAll(path); - it.remove(); + sg.dismiss(); if (h.isEmpty()) { DEBUG() << "Closing handle" << handle.handle; @@ -613,7 +612,7 @@ QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &path } threads.removeAll(0); - return p; + return unhandled; } /////////// |