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_kqueue.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_kqueue.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemwatcher_kqueue.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/corelib/io/qfilesystemwatcher_kqueue.cpp b/src/corelib/io/qfilesystemwatcher_kqueue.cpp index 423b88cb7f..c2028e3641 100644 --- a/src/corelib/io/qfilesystemwatcher_kqueue.cpp +++ b/src/corelib/io/qfilesystemwatcher_kqueue.cpp @@ -45,6 +45,7 @@ #include <qdebug.h> #include <qfile.h> +#include <qscopeguard.h> #include <qsocketnotifier.h> #include <qvarlengtharray.h> @@ -94,10 +95,9 @@ QStringList QKqueueFileSystemWatcherEngine::addPaths(const QStringList &paths, QStringList *files, QStringList *directories) { - 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);}); int fd; #if defined(O_EVTONLY) fd = qt_safe_open(QFile::encodeName(path), O_EVTONLY); @@ -149,7 +149,8 @@ QStringList QKqueueFileSystemWatcherEngine::addPaths(const QStringList &paths, continue; } - it.remove(); + sg.dismiss(); + if (id < 0) { DEBUG() << "QKqueueFileSystemWatcherEngine: added directory path" << path; directories->append(path); @@ -162,20 +163,19 @@ QStringList QKqueueFileSystemWatcherEngine::addPaths(const QStringList &paths, idToPath.insert(id, path); } - return p; + return unhandled; } QStringList QKqueueFileSystemWatcherEngine::removePaths(const QStringList &paths, QStringList *files, QStringList *directories) { - QStringList p = paths; if (pathToID.isEmpty()) - return p; + return 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);}); int id = pathToID.take(path); QString x = idToPath.take(id); if (x.isEmpty() || x != path) @@ -183,14 +183,15 @@ QStringList QKqueueFileSystemWatcherEngine::removePaths(const QStringList &paths ::close(id < 0 ? -id : id); - it.remove(); + sg.dismiss(); + if (id < 0) directories->removeAll(path); else files->removeAll(path); } - return p; + return unhandled; } void QKqueueFileSystemWatcherEngine::readFromKqueue() |