From c4ca0060e39e885770b70302c8ae2f4f7b9f067a Mon Sep 17 00:00:00 2001 From: Kai Koehne <kai.koehne@nokia.com> Date: Thu, 21 Oct 2010 08:30:53 +0200 Subject: [PATCH] QmlProject: Don't use RegExp for simple wildcard matching QRegExp matching is quite expensive, and has to be done for every file in the project directory tree against all possible suffixes. Optimize for the common case that the pattern is "*.suffix" by doing a fileName.endsWidth(suffix) in this case. This speeds up loading of examples/declarative/declarative.qmlproject by about 30%. Reviewed-by: Christian Kamm --- .../fileformat/filefilteritems.cpp | 51 +++++++++++++------ .../fileformat/filefilteritems.h | 3 ++ .../fileformat/tst_fileformat.cpp | 34 ++++++++++++- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/src/plugins/qmlprojectmanager/fileformat/filefilteritems.cpp b/src/plugins/qmlprojectmanager/fileformat/filefilteritems.cpp index d78a3ebac93..95c17189807 100644 --- a/src/plugins/qmlprojectmanager/fileformat/filefilteritems.cpp +++ b/src/plugins/qmlprojectmanager/fileformat/filefilteritems.cpp @@ -47,7 +47,21 @@ void FileFilterBaseItem::setFilter(const QString &filter) m_filter = filter; m_regExpList.clear(); + m_fileSuffixes.clear(); + foreach (const QString &pattern, filter.split(QLatin1Char(';'))) { + if (pattern.isEmpty()) + continue; + // decide if it's a canonical pattern like *.x + if (pattern.startsWith(QLatin1String("*."))) { + const QString suffix = pattern.right(pattern.size() - 1); + if (!suffix.contains(QLatin1Char('*')) + && !suffix.contains(QLatin1Char('?')) + && !suffix.contains(QLatin1Char('['))) { + m_fileSuffixes << suffix; + continue; + } + } m_regExpList << QRegExp(pattern, Qt::CaseInsensitive, QRegExp::Wildcard); } @@ -113,16 +127,9 @@ bool FileFilterBaseItem::matchesFile(const QString &filePath) const return true; } - bool regexMatches = false; const QString &fileName = QFileInfo(filePath).fileName(); - foreach (const QRegExp &exp, m_regExpList) { - if (exp.exactMatch(fileName)) { - regexMatches = true; - break; - } - } - if (!regexMatches) + if (!fileMatches(fileName)) return false; const QDir fileDir = QFileInfo(filePath).absoluteDir(); @@ -164,7 +171,7 @@ void FileFilterBaseItem::updateFileList() foreach (const QString &explicitPath, m_explicitFiles) { newFiles << absolutePath(explicitPath); } - if (!m_regExpList.isEmpty() && m_explicitFiles.isEmpty()) + if ((!m_fileSuffixes.isEmpty() || !m_regExpList.isEmpty()) && m_explicitFiles.isEmpty()) newFiles += filesInSubTree(QDir(m_defaultDir), QDir(projectDir), &dirsToBeWatched); if (newFiles != m_files) { @@ -190,6 +197,23 @@ void FileFilterBaseItem::updateFileList() m_dirWatcher.addDirectories(watchDirs.toList()); } +bool FileFilterBaseItem::fileMatches(const QString &fileName) const +{ + foreach (const QString &suffix, m_fileSuffixes) { + if (fileName.endsWith(suffix, Qt::CaseInsensitive)) { + return true; + } + } + + foreach (const QRegExp &filter, m_regExpList) { + if (filter.exactMatch(fileName)) { + return true; + } + } + + return false; +} + QSet<QString> FileFilterBaseItem::filesInSubTree(const QDir &rootDir, const QDir &dir, QSet<QString> *parsedDirs) { QSet<QString> fileSet; @@ -199,12 +223,9 @@ QSet<QString> FileFilterBaseItem::filesInSubTree(const QDir &rootDir, const QDir foreach (const QFileInfo &file, dir.entryInfoList(QDir::Files)) { const QString fileName = file.fileName(); - foreach (const QRegExp &filter, m_regExpList) { - if (filter.exactMatch(fileName)) { - fileSet.insert(file.absoluteFilePath()); - break; - } - } + + if (fileMatches(fileName)) + fileSet.insert(file.absoluteFilePath()); } if (recursive()) { diff --git a/src/plugins/qmlprojectmanager/fileformat/filefilteritems.h b/src/plugins/qmlprojectmanager/fileformat/filefilteritems.h index ec27e1cd264..2dcd1d557a6 100644 --- a/src/plugins/qmlprojectmanager/fileformat/filefilteritems.h +++ b/src/plugins/qmlprojectmanager/fileformat/filefilteritems.h @@ -54,12 +54,15 @@ private: QString absolutePath(const QString &path) const; QString absoluteDir() const; + bool fileMatches(const QString &fileName) const; QSet<QString> filesInSubTree(const QDir &rootDir, const QDir &dir, QSet<QString> *parsedDirs = 0); QString m_rootDir; QString m_defaultDir; QString m_filter; + // simple "*.png" patterns are stored in m_fileSuffixes, otherwise store in m_regExpList + QList<QString> m_fileSuffixes; QList<QRegExp> m_regExpList; enum RecursiveOption { diff --git a/tests/auto/qml/qmlprojectmanager/fileformat/tst_fileformat.cpp b/tests/auto/qml/qmlprojectmanager/fileformat/tst_fileformat.cpp index ac89ce956ae..c65698f09a9 100644 --- a/tests/auto/qml/qmlprojectmanager/fileformat/tst_fileformat.cpp +++ b/tests/auto/qml/qmlprojectmanager/fileformat/tst_fileformat.cpp @@ -208,6 +208,35 @@ void tst_FileFormat::testFileFilter() qDebug() << project->files().toSet() << expectedFiles.toSet(); QCOMPARE(project->files().toSet(), expectedFiles.toSet()); } + + // + // use wildcards + // + projectFile = QLatin1String( + "import QmlProject 1.0\n" + "Project {\n" + " ImageFiles {\n" + " filter: \"?mage.[gf]if\"\n" + " }\n" + "}\n"); + + { + QDeclarativeEngine engine; + QDeclarativeComponent component(&engine); + component.setData(projectFile.toUtf8(), QUrl()); + if (!component.isReady()) + qDebug() << component.errorString(); + QVERIFY(component.isReady()); + + QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create()); + QVERIFY(project); + + project->setSourceDirectory(testDataDir); + + QStringList expectedFiles(QStringList() << testDataDir + "/image.gif"); + qDebug() << project->files().toSet() << expectedFiles.toSet(); + QCOMPARE(project->files().toSet(), expectedFiles.toSet()); + } } void tst_FileFormat::testMatchesFile() @@ -269,8 +298,9 @@ void tst_FileFormat::testLibraryPaths() project->setSourceDirectory(testDataDir); - QStringList expectedPaths(QStringList() << "../otherLibrary" - << "library"); + QStringList expectedPaths(QStringList() << SRCDIR "/otherLibrary" + << SRCDIR "/data/library"); + qDebug() << expectedPaths << project->importPaths(); QCOMPARE(project->importPaths().toSet(), expectedPaths.toSet()); } } -- GitLab