From b03385f9cff7acc2b37933f493e3eff2d8bbef59 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 19 May 2019 23:25:12 +0200 Subject: 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 --- src/corelib/io/qfilesystemwatcher_kqueue.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/corelib/io/qfilesystemwatcher_kqueue.cpp') 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 #include +#include #include #include @@ -94,10 +95,9 @@ QStringList QKqueueFileSystemWatcherEngine::addPaths(const QStringList &paths, QStringList *files, QStringList *directories) { - QStringList p = paths; - QMutableListIterator 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 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() -- cgit v1.2.3