Use a std::vector instead of a QVector in QMimeDatabasePrivate

Using std::vector saves a bit c++ code when using range-for loop over
the vector.

Besides, no shared copies created anymore which is a bit faster.

Change-Id: I564306ed9ac907e9f32f59b33ed15c027a59b4eb
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Ivan Komissarov 2018-09-25 21:49:05 +03:00
parent 0e1866017f
commit 09e926cc27
3 changed files with 29 additions and 26 deletions

View File

@ -109,8 +109,8 @@ void QMimeDatabasePrivate::loadProviders()
mimeDirs.prepend(QLatin1String(":/qt-project.org/qmime"));
//qDebug() << "mime dirs:" << mimeDirs;
QVector<QMimeProviderBase *> currentProviders = m_providers;
m_providers.clear();
Providers currentProviders;
std::swap(m_providers, currentProviders);
m_providers.reserve(mimeDirs.size());
for (const QString &mimeDir : qAsConst(mimeDirs)) {
const QString cacheFile = mimeDir + QStringLiteral("/mime.cache");
@ -133,7 +133,7 @@ void QMimeDatabasePrivate::loadProviders()
provider = new QMimeXMLProvider(this, mimeDir);
//qDebug() << "Created XML provider for" << mimeDir;
}
m_providers.append(provider);
m_providers.push_back(provider);
} else {
QMimeProviderBase *provider = *it;
currentProviders.erase(it);
@ -143,18 +143,18 @@ void QMimeDatabasePrivate::loadProviders()
provider = new QMimeXMLProvider(this, mimeDir);
//qDebug() << "Created XML provider to replace binary provider for" << mimeDir;
}
m_providers.append(provider);
m_providers.push_back(provider);
}
}
qDeleteAll(currentProviders);
}
QVector<QMimeProviderBase *> QMimeDatabasePrivate::providers()
const QMimeDatabasePrivate::Providers &QMimeDatabasePrivate::providers()
{
#ifndef Q_OS_WASM // stub implementation always returns true
Q_ASSERT(!mutex.tryLock()); // caller should have locked mutex
#endif
if (m_providers.isEmpty()) {
if (m_providers.empty()) {
loadProviders();
m_lastCheck.start();
} else {
@ -166,8 +166,7 @@ QVector<QMimeProviderBase *> QMimeDatabasePrivate::providers()
QString QMimeDatabasePrivate::resolveAlias(const QString &nameOrAlias)
{
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders) {
for (const auto &provider : providers()) {
const QString ret = provider->resolveAlias(nameOrAlias);
if (!ret.isEmpty())
return ret;
@ -181,9 +180,8 @@ QString QMimeDatabasePrivate::resolveAlias(const QString &nameOrAlias)
*/
QMimeType QMimeDatabasePrivate::mimeTypeForName(const QString &nameOrAlias)
{
const auto allProviders = providers();
const QString mimeName = resolveAlias(nameOrAlias);
for (QMimeProviderBase *provider : allProviders) {
for (const auto &provider : providers()) {
const QMimeType mime = provider->mimeTypeForName(mimeName);
if (mime.isValid())
return mime;
@ -207,8 +205,7 @@ QMimeGlobMatchResult QMimeDatabasePrivate::findByFileName(const QString &fileNam
{
QMimeGlobMatchResult result;
// TODO this parses in the order (local, global). Check that it handles "NOGLOBS" correctly.
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders)
for (const auto &provider : providers())
provider->addFileNameMatches(fileName, result);
return result;
}
@ -229,8 +226,7 @@ void QMimeDatabasePrivate::loadGenericIcon(QMimeTypePrivate &mimePrivate)
QMutexLocker locker(&mutex);
if (mimePrivate.fromCache) {
mimePrivate.genericIconName.clear();
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders) {
for (const auto &provider : providers()) {
provider->loadGenericIcon(mimePrivate);
if (!mimePrivate.genericIconName.isEmpty())
break;
@ -243,8 +239,7 @@ void QMimeDatabasePrivate::loadIcon(QMimeTypePrivate &mimePrivate)
QMutexLocker locker(&mutex);
if (mimePrivate.fromCache) {
mimePrivate.iconName.clear();
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders) {
for (const auto &provider : providers()) {
provider->loadIcon(mimePrivate);
if (!mimePrivate.iconName.isEmpty())
break;
@ -278,8 +273,7 @@ QStringList QMimeDatabasePrivate::parents(const QString &mimeName)
{
Q_ASSERT(!mutex.tryLock());
QStringList result;
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders)
for (const auto &provider : providers())
provider->addParents(mimeName, result);
if (result.isEmpty()) {
const QString parent = fallbackParent(mimeName);
@ -293,8 +287,7 @@ QStringList QMimeDatabasePrivate::listAliases(const QString &mimeName)
{
QMutexLocker locker(&mutex);
QStringList result;
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders)
for (const auto &provider : providers())
provider->addAliases(mimeName, result);
return result;
}
@ -333,8 +326,7 @@ QMimeType QMimeDatabasePrivate::findByData(const QByteArray &data, int *accuracy
*accuracyPtr = 0;
QMimeType candidate;
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders)
for (const auto &provider : providers())
provider->findByMagic(data, accuracyPtr, candidate);
if (candidate.isValid())
@ -416,8 +408,7 @@ QMimeType QMimeDatabasePrivate::mimeTypeForFileNameAndData(const QString &fileNa
QList<QMimeType> QMimeDatabasePrivate::allMimeTypes()
{
QList<QMimeType> result;
const auto allProviders = providers();
for (QMimeProviderBase *provider : allProviders)
for (const auto &provider : providers())
provider->addAllMimeTypes(result);
return result;
}

View File

@ -102,11 +102,12 @@ public:
bool mimeInherits(const QString &mime, const QString &parent);
private:
QVector<QMimeProviderBase *> providers();
using Providers = std::vector<QMimeProviderBase *>;
const Providers &providers();
bool shouldCheck();
void loadProviders();
mutable QVector<QMimeProviderBase *> m_providers;
mutable Providers m_providers;
QElapsedTimer m_lastCheck;
public:

View File

@ -35,6 +35,7 @@ class tst_QMimeDatabase: public QObject
private slots:
void inheritsPerformance();
void benchMimeTypeForName();
};
void tst_QMimeDatabase::inheritsPerformance()
@ -70,5 +71,15 @@ void tst_QMimeDatabase::inheritsPerformance()
// parsing XML, and then keeps being around 4.5 MB for all the in-memory hashes.
}
void tst_QMimeDatabase::benchMimeTypeForName()
{
QMimeDatabase db;
QBENCHMARK {
const auto s = db.mimeTypeForName(QStringLiteral("text/plain"));
QVERIFY(s.isValid());
}
}
QTEST_MAIN(tst_QMimeDatabase)
#include "main.moc"