From ca3646daba6dd82e5da18080f3d242a6e7256a31 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 3 May 2024 11:26:29 +0200 Subject: [PATCH] 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 Reviewed-by: Marcus Tillmanns Reviewed-by: --- src/plugins/cppeditor/cppindexingsupport.cpp | 38 ++++++++++---------- src/plugins/cppeditor/cppindexingsupport.h | 8 +++-- src/plugins/cppeditor/cppmodelmanager.cpp | 21 ++++++++--- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/plugins/cppeditor/cppindexingsupport.cpp b/src/plugins/cppeditor/cppindexingsupport.cpp index b6edade0e27..73e3fafff32 100644 --- a/src/plugins/cppeditor/cppindexingsupport.cpp +++ b/src/plugins/cppeditor/cppindexingsupport.cpp @@ -219,23 +219,22 @@ static void index(QPromise &promise, const ParseParams params) qCDebug(indexerLog) << "Indexing finished."; } -static void parse(QPromise &promise, const ParseParams ¶ms) +static void parse( + QPromise &promise, + const std::function()> &sourceFiles, + const ProjectExplorer::HeaderPaths &headerPaths, + const WorkingCopy &workingCopy) { - const QSet &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 CppIndexingSupport::refreshSourceFiles(const QSet &sourceFiles, - CppModelManager::ProgressNotificationMode mode) +QFuture CppIndexingSupport::refreshSourceFiles( + const std::function()> &sourceFiles, + CppModelManager::ProgressNotificationMode mode) { - ParseParams params; - params.headerPaths = CppModelManager::headerPaths(); - params.workingCopy = CppModelManager::workingCopy(); - params.sourceFiles = sourceFiles; - - QFuture result = Utils::asyncRun(CppModelManager::sharedThreadPool(), parse, params); + QFuture 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); } diff --git a/src/plugins/cppeditor/cppindexingsupport.h b/src/plugins/cppeditor/cppindexingsupport.h index c04eb8ba76f..f32498fa5e4 100644 --- a/src/plugins/cppeditor/cppindexingsupport.h +++ b/src/plugins/cppeditor/cppindexingsupport.h @@ -11,6 +11,8 @@ #include +#include + namespace Utils { class SearchResultItem; } namespace CppEditor { @@ -59,8 +61,10 @@ class CPPEDITOR_EXPORT CppIndexingSupport public: static bool isFindErrorsIndexingActive(); - QFuture refreshSourceFiles(const QSet &sourceFiles, - CppModelManager::ProgressNotificationMode mode); + QFuture refreshSourceFiles( + const std::function()> &sourceFiles, + CppModelManager::ProgressNotificationMode mode); + private: Utils::FutureSynchronizer m_synchronizer; }; diff --git a/src/plugins/cppeditor/cppmodelmanager.cpp b/src/plugins/cppeditor/cppmodelmanager.cpp index 725d5632d56..fbb727eb725 100644 --- a/src/plugins/cppeditor/cppmodelmanager.cpp +++ b/src/plugins/cppeditor/cppmodelmanager.cpp @@ -90,6 +90,7 @@ #include #include +#include #if defined(QTCREATOR_WITH_DUMP_AST) && defined(Q_CC_GNU) #define WITH_AST_DUMP @@ -1353,15 +1354,25 @@ QFuture CppModelManager::updateSourceFiles(const QSet &sourceFil if (sourceFiles.isEmpty() || !d->m_indexerEnabled) return QFuture(); - QHash> sourcesPerProject; // TODO: Work with QList from here on? + std::unordered_map> sourcesPerProject; for (const FilePath &fp : sourceFiles) sourcesPerProject[ProjectManager::projectForFile(fp)] << fp.toString(); - QSet filteredFiles; - for (auto it = sourcesPerProject.cbegin(); it != sourcesPerProject.cend(); ++it) { - filteredFiles.unite( - filteredFilesRemoved(it.value(), CppCodeModelSettings::settingsForProject(it.key()))); + std::vector, CppCodeModelSettings>> sourcesAndSettings; + for (const auto &it : sourcesPerProject) { + sourcesAndSettings + .emplace_back(it.second, CppCodeModelSettings::settingsForProject(it.first)); } + const auto filteredFiles = [sourcesAndSettings = std::move(sourcesAndSettings)] { + QSet 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); }