C++: Do not freeze Qt Creator while checking file sizes

Before actually indexing files, the C++ model checks the files against a
file size limit (if set, which is the default).

Do not iterate over all files and check their size in the main thread.
If the files are on a device, this operation is not fast. Move the
filtering to the parsing thread itself.

Change-Id: I2202cc44c28f38159ca593db2399dde30f95f9bd
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Eike Ziller
2024-05-03 11:26:29 +02:00
parent effb365ab3
commit ca3646daba
3 changed files with 41 additions and 26 deletions

View File

@@ -219,23 +219,22 @@ static void index(QPromise<void> &promise, const ParseParams params)
qCDebug(indexerLog) << "Indexing finished.";
}
static void parse(QPromise<void> &promise, const ParseParams &params)
static void parse(
QPromise<void> &promise,
const std::function<QSet<QString>()> &sourceFiles,
const ProjectExplorer::HeaderPaths &headerPaths,
const WorkingCopy &workingCopy)
{
const QSet<QString> &files = params.sourceFiles;
if (files.isEmpty()) {
CppModelManager::finishedRefreshingSourceFiles(files);
return;
}
promise.setProgressRange(0, files.size());
ParseParams params{headerPaths, workingCopy, sourceFiles()};
promise.setProgressRange(0, params.sourceFiles.size());
if (CppIndexingSupport::isFindErrorsIndexingActive())
indexFindErrors(promise, params);
else
index(promise, params);
promise.setProgressValue(files.size());
CppModelManager::finishedRefreshingSourceFiles(files);
promise.setProgressValue(params.sourceFiles.size());
CppModelManager::finishedRefreshingSourceFiles(params.sourceFiles);
}
} // anonymous namespace
@@ -302,18 +301,19 @@ bool CppIndexingSupport::isFindErrorsIndexingActive()
return Utils::qtcEnvironmentVariable("QTC_FIND_ERRORS_INDEXING") == "1";
}
QFuture<void> CppIndexingSupport::refreshSourceFiles(const QSet<QString> &sourceFiles,
CppModelManager::ProgressNotificationMode mode)
QFuture<void> CppIndexingSupport::refreshSourceFiles(
const std::function<QSet<QString>()> &sourceFiles,
CppModelManager::ProgressNotificationMode mode)
{
ParseParams params;
params.headerPaths = CppModelManager::headerPaths();
params.workingCopy = CppModelManager::workingCopy();
params.sourceFiles = sourceFiles;
QFuture<void> result = Utils::asyncRun(CppModelManager::sharedThreadPool(), parse, params);
QFuture<void> result = Utils::asyncRun(
CppModelManager::sharedThreadPool(),
parse,
sourceFiles,
CppModelManager::headerPaths(),
CppModelManager::workingCopy());
m_synchronizer.addFuture(result);
if (mode == CppModelManager::ForcedProgressNotification || sourceFiles.count() > 1) {
if (mode == CppModelManager::ForcedProgressNotification) {
Core::ProgressManager::addTask(result, Tr::tr("Parsing C/C++ Files"),
CppEditor::Constants::TASK_INDEX);
}

View File

@@ -11,6 +11,8 @@
#include <QFuture>
#include <functional>
namespace Utils { class SearchResultItem; }
namespace CppEditor {
@@ -59,8 +61,10 @@ class CPPEDITOR_EXPORT CppIndexingSupport
public:
static bool isFindErrorsIndexingActive();
QFuture<void> refreshSourceFiles(const QSet<QString> &sourceFiles,
CppModelManager::ProgressNotificationMode mode);
QFuture<void> refreshSourceFiles(
const std::function<QSet<QString>()> &sourceFiles,
CppModelManager::ProgressNotificationMode mode);
private:
Utils::FutureSynchronizer m_synchronizer;
};

View File

@@ -90,6 +90,7 @@
#include <QWriteLocker>
#include <memory>
#include <unordered_map>
#if defined(QTCREATOR_WITH_DUMP_AST) && defined(Q_CC_GNU)
#define WITH_AST_DUMP
@@ -1353,15 +1354,25 @@ QFuture<void> CppModelManager::updateSourceFiles(const QSet<FilePath> &sourceFil
if (sourceFiles.isEmpty() || !d->m_indexerEnabled)
return QFuture<void>();
QHash<Project *, QSet<QString>> sourcesPerProject; // TODO: Work with QList from here on?
std::unordered_map<Project *, QSet<QString>> sourcesPerProject;
for (const FilePath &fp : sourceFiles)
sourcesPerProject[ProjectManager::projectForFile(fp)] << fp.toString();
QSet<QString> filteredFiles;
for (auto it = sourcesPerProject.cbegin(); it != sourcesPerProject.cend(); ++it) {
filteredFiles.unite(
filteredFilesRemoved(it.value(), CppCodeModelSettings::settingsForProject(it.key())));
std::vector<std::pair<QSet<QString>, CppCodeModelSettings>> sourcesAndSettings;
for (const auto &it : sourcesPerProject) {
sourcesAndSettings
.emplace_back(it.second, CppCodeModelSettings::settingsForProject(it.first));
}
const auto filteredFiles = [sourcesAndSettings = std::move(sourcesAndSettings)] {
QSet<QString> result;
for (const auto &it : sourcesAndSettings)
result.unite(filteredFilesRemoved(it.first, it.second));
return result;
};
// "ReservedProgressNotification" should be shown if there is more than one source file.
if (sourceFiles.size() > 1)
mode = ForcedProgressNotification;
return d->m_internalIndexingSupport->refreshSourceFiles(filteredFiles, mode);
}