forked from qt-creator/qt-creator
Utils: Avoid watching directories of watched files
Do not watch directories unconditionally, but only if they contain
removed files to check whether those files are readded. This should
reduce the number of needlesly watched directories to a minimum and fix
performance regressions introduced by
61598eca15.
Fixes: QTCREATORBUG-28957
Change-Id: I8fe387e7de32b0fb585074330c7f6ca7eae44730
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -275,15 +275,19 @@ void FileSystemWatcher::addFiles(const QStringList &files, WatchMode wm)
|
||||
const int count = ++d->m_staticData->m_fileCount[file];
|
||||
Q_ASSERT(count > 0);
|
||||
|
||||
if (count == 1)
|
||||
if (count == 1) {
|
||||
toAdd << file;
|
||||
|
||||
const QString directory = QFileInfo(file).path();
|
||||
const int dirCount = ++d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount > 0);
|
||||
QFileInfo fi(file);
|
||||
if (!fi.exists()) {
|
||||
const QString directory = fi.path();
|
||||
const int dirCount = ++d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount > 0);
|
||||
|
||||
if (dirCount == 1)
|
||||
toAdd << directory;
|
||||
if (dirCount == 1)
|
||||
toAdd << directory;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!toAdd.isEmpty())
|
||||
@@ -311,15 +315,19 @@ void FileSystemWatcher::removeFiles(const QStringList &files)
|
||||
const int count = --(d->m_staticData->m_fileCount[file]);
|
||||
Q_ASSERT(count >= 0);
|
||||
|
||||
if (!count)
|
||||
if (!count) {
|
||||
toRemove << file;
|
||||
|
||||
const QString directory = QFileInfo(file).path();
|
||||
const int dirCount = --d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount >= 0);
|
||||
QFileInfo fi(file);
|
||||
if (!fi.exists()) {
|
||||
const QString directory = fi.path();
|
||||
const int dirCount = --d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount >= 0);
|
||||
|
||||
if (!dirCount)
|
||||
toRemove << directory;
|
||||
if (!dirCount)
|
||||
toRemove << directory;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!toRemove.isEmpty())
|
||||
@@ -418,13 +426,27 @@ QStringList FileSystemWatcher::directories() const
|
||||
void FileSystemWatcher::slotFileChanged(const QString &path)
|
||||
{
|
||||
const auto it = d->m_files.find(path);
|
||||
QStringList toAdd;
|
||||
if (it != d->m_files.end() && it.value().trigger(path)) {
|
||||
if (debug)
|
||||
qDebug() << this << "triggers on file " << path
|
||||
<< it.value().watchMode
|
||||
<< it.value().modifiedTime.toString(Qt::ISODate);
|
||||
d->fileChanged(path);
|
||||
|
||||
QFileInfo fi(path);
|
||||
if (!fi.exists()) {
|
||||
const QString directory = fi.path();
|
||||
const int dirCount = ++d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount > 0);
|
||||
|
||||
if (dirCount == 1)
|
||||
toAdd << directory;
|
||||
}
|
||||
}
|
||||
|
||||
if (!toAdd.isEmpty())
|
||||
d->m_staticData->m_watcher->addPaths(toAdd);
|
||||
}
|
||||
|
||||
void FileSystemWatcher::slotDirectoryChanged(const QString &path)
|
||||
@@ -450,9 +472,20 @@ void FileSystemWatcher::slotDirectoryChanged(const QString &path)
|
||||
for (const QString &rejected : d->m_staticData->m_watcher->addPaths(toReadd))
|
||||
toReadd.removeOne(rejected);
|
||||
|
||||
QStringList toRemove;
|
||||
// If we've successfully added the file, that means it was deleted and replaced.
|
||||
for (const QString &reAdded : std::as_const(toReadd))
|
||||
for (const QString &reAdded : std::as_const(toReadd)) {
|
||||
d->fileChanged(reAdded);
|
||||
const QString directory = QFileInfo(reAdded).path();
|
||||
const int dirCount = --d->m_staticData->m_directoryCount[directory];
|
||||
Q_ASSERT(dirCount >= 0);
|
||||
|
||||
if (!dirCount)
|
||||
toRemove << directory;
|
||||
}
|
||||
|
||||
if (!toRemove.isEmpty())
|
||||
d->m_staticData->m_watcher->removePaths(toRemove);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user