From 96c21b0e366870b66de81c7a1786c0d032aa585a Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 7 Sep 2023 15:38:17 +0200 Subject: [PATCH] CppEditor: optimize CppModelManager projectPartForFile Calling FilePath::canonicalPath is expensive on Windows, so only call it if we cannot find the filePath in the cache and save the result to the cache again. This reduces the time to parse the auto test for the Qt Creator repository from 10s to 2s here. It also improves the performance of various quickfixes and follow symbol of the built-in code model by a similar factor. Change-Id: I7025828e1b91f492825b10753e3b97e1c917fbfd Reviewed-by: Christian Kandeler --- src/plugins/cppeditor/cppmodelmanager.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/plugins/cppeditor/cppmodelmanager.cpp b/src/plugins/cppeditor/cppmodelmanager.cpp index a2f93635fea..231740899be 100644 --- a/src/plugins/cppeditor/cppmodelmanager.cpp +++ b/src/plugins/cppeditor/cppmodelmanager.cpp @@ -1465,8 +1465,11 @@ void CppModelManager::recalculateProjectPartMappings() for (const ProjectData &projectData : std::as_const(d->m_projectData)) { for (const ProjectPart::ConstPtr &projectPart : projectData.projectInfo->projectParts()) { d->m_projectPartIdToProjectProjectPart[projectPart->id()] = projectPart; - for (const ProjectFile &cxxFile : projectPart->files) - d->m_fileToProjectParts[cxxFile.path.canonicalPath()].append(projectPart); + for (const ProjectFile &cxxFile : projectPart->files) { + d->m_fileToProjectParts[cxxFile.path].append(projectPart); + if (FilePath canonical = cxxFile.path.canonicalPath(); canonical != cxxFile.path) + d->m_fileToProjectParts[canonical].append(projectPart); + } } } @@ -1639,8 +1642,16 @@ ProjectPart::ConstPtr CppModelManager::projectPartForId(const QString &projectPa QList CppModelManager::projectPart(const FilePath &fileName) { - QReadLocker locker(&d->m_projectLock); - return d->m_fileToProjectParts.value(fileName.canonicalPath()); + { + QReadLocker locker(&d->m_projectLock); + auto it = d->m_fileToProjectParts.find(fileName); + if (it != d->m_fileToProjectParts.end()) + return it.value(); + } + const FilePath canonicalPath = fileName.canonicalPath(); + QWriteLocker locker(&d->m_projectLock); + auto it = d->m_fileToProjectParts.insert(fileName, d->m_fileToProjectParts.value(canonicalPath)); + return it.value(); } QList CppModelManager::projectPartFromDependencies( @@ -1651,7 +1662,7 @@ QList CppModelManager::projectPartFromDependencies( QReadLocker locker(&d->m_projectLock); for (const FilePath &dep : deps) - parts.unite(Utils::toSet(d->m_fileToProjectParts.value(dep.canonicalPath()))); + parts.unite(Utils::toSet(projectPart(dep))); return parts.values(); }