QFileSystemModel: cache the name filters regexps
In order to filter out file names based on the user's settings, QFileSystemModel used to have a loop that tested if a given file name matched one of the filters. The problem is that each filter (a wildcard) was converted to a QRegularExpression _inside_ the loop. This causes a quadratic behavior (number of files * number of filters). Instead, build the regexps once when the filters are set (or the case sensitivity is changed, as that affects the filtering), and simply _use_ them in the loop. Simplify and correct some related code as a drive by. Done-with: Jean-Michaël Celerier Fixes: QTBUG-95383 Change-Id: I6bc336364c145bb05793a8f867545d7715d35832 Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> (cherry picked from commit 07057188e3e42246cf006b43963d0bdcdaa159f5) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
216488231e
commit
bc904c8524
@ -1594,9 +1594,11 @@ void QFileSystemModel::setFilter(QDir::Filters filters)
|
|||||||
Q_D(QFileSystemModel);
|
Q_D(QFileSystemModel);
|
||||||
if (d->filters == filters)
|
if (d->filters == filters)
|
||||||
return;
|
return;
|
||||||
|
const bool changingCaseSensitivity =
|
||||||
|
filters.testFlag(QDir::CaseSensitive) != d->filters.testFlag(QDir::CaseSensitive);
|
||||||
d->filters = filters;
|
d->filters = filters;
|
||||||
// CaseSensitivity might have changed
|
if (changingCaseSensitivity)
|
||||||
setNameFilters(nameFilters());
|
d->rebuildNameFilterRegexps();
|
||||||
d->forceSort = true;
|
d->forceSort = true;
|
||||||
d->delayedSort();
|
d->delayedSort();
|
||||||
}
|
}
|
||||||
@ -1693,7 +1695,6 @@ bool QFileSystemModel::nameFilterDisables() const
|
|||||||
*/
|
*/
|
||||||
void QFileSystemModel::setNameFilters(const QStringList &filters)
|
void QFileSystemModel::setNameFilters(const QStringList &filters)
|
||||||
{
|
{
|
||||||
// Prep the regexp's ahead of time
|
|
||||||
#if QT_CONFIG(regularexpression)
|
#if QT_CONFIG(regularexpression)
|
||||||
Q_D(QFileSystemModel);
|
Q_D(QFileSystemModel);
|
||||||
|
|
||||||
@ -1716,6 +1717,7 @@ void QFileSystemModel::setNameFilters(const QStringList &filters)
|
|||||||
}
|
}
|
||||||
|
|
||||||
d->nameFilters = filters;
|
d->nameFilters = filters;
|
||||||
|
d->rebuildNameFilterRegexps();
|
||||||
d->forceSort = true;
|
d->forceSort = true;
|
||||||
d->delayedSort();
|
d->delayedSort();
|
||||||
#else
|
#else
|
||||||
@ -2152,15 +2154,13 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
|
|||||||
|
|
||||||
// Check the name regularexpression filters
|
// Check the name regularexpression filters
|
||||||
if (!(node->isDir() && (filters & QDir::AllDirs))) {
|
if (!(node->isDir() && (filters & QDir::AllDirs))) {
|
||||||
auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
const auto matchesNodeFileName = [node](const QRegularExpression &re)
|
||||||
|
{
|
||||||
for (const auto &nameFilter : nameFilters) {
|
return node->fileName.contains(re);
|
||||||
auto rx = QRegularExpression::fromWildcard(nameFilter, cs);
|
};
|
||||||
QRegularExpressionMatch match = rx.match(node->fileName);
|
return std::any_of(nameFiltersRegexps.begin(),
|
||||||
if (match.hasMatch())
|
nameFiltersRegexps.end(),
|
||||||
return true;
|
matchesNodeFileName);
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
Q_UNUSED(node);
|
Q_UNUSED(node);
|
||||||
@ -2168,6 +2168,23 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if QT_CONFIG(regularexpression)
|
||||||
|
void QFileSystemModelPrivate::rebuildNameFilterRegexps()
|
||||||
|
{
|
||||||
|
nameFiltersRegexps.clear();
|
||||||
|
nameFiltersRegexps.reserve(nameFilters.size());
|
||||||
|
const auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
||||||
|
const auto convertWildcardToRegexp = [cs](const QString &nameFilter)
|
||||||
|
{
|
||||||
|
return QRegularExpression::fromWildcard(nameFilter, cs);
|
||||||
|
};
|
||||||
|
std::transform(nameFilters.constBegin(),
|
||||||
|
nameFilters.constEnd(),
|
||||||
|
std::back_inserter(nameFiltersRegexps),
|
||||||
|
convertWildcardToRegexp);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
#include "moc_qfilesystemmodel.cpp"
|
#include "moc_qfilesystemmodel.cpp"
|
||||||
|
@ -64,6 +64,8 @@
|
|||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
#include <qhash.h>
|
#include <qhash.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
QT_REQUIRE_CONFIG(filesystemmodel);
|
QT_REQUIRE_CONFIG(filesystemmodel);
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -291,6 +293,8 @@ public:
|
|||||||
QHash<const QFileSystemNode*, bool> bypassFilters;
|
QHash<const QFileSystemNode*, bool> bypassFilters;
|
||||||
#if QT_CONFIG(regularexpression)
|
#if QT_CONFIG(regularexpression)
|
||||||
QStringList nameFilters;
|
QStringList nameFilters;
|
||||||
|
std::vector<QRegularExpression> nameFiltersRegexps;
|
||||||
|
void rebuildNameFilterRegexps();
|
||||||
#endif
|
#endif
|
||||||
QHash<QString, QString> resolvedSymLinks;
|
QHash<QString, QString> resolvedSymLinks;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user