Add a static QFileInfo::exists(fileName) function

This avoids dynamic construction of the private class. According to
the benchmark we go from 4,550 to 3,900 instruction reads per iteration.
(without change 32629676 the baseline is 5,600)

Change-Id: I5df925e30dbd49bdde87173e481820574ce5abe1
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
hjk 2013-09-13 11:20:55 +02:00 committed by The Qt Project
parent 4c709fb391
commit 63a382a930
5 changed files with 39 additions and 1 deletions

View File

@ -429,7 +429,7 @@ QFile::exists() const
bool bool
QFile::exists(const QString &fileName) QFile::exists(const QString &fileName)
{ {
return QFileInfo(fileName).exists(); return QFileInfo::exists(fileName);
} }
/*! /*!

View File

@ -666,6 +666,28 @@ bool QFileInfo::exists() const
return d->getFileFlags(QAbstractFileEngine::ExistsFlag); return d->getFileFlags(QAbstractFileEngine::ExistsFlag);
} }
/*!
Returns true if the \a file exists; otherwise returns false.
\note If \a file is a symlink that points to a non-existing
file, false is returned.
\note Using this function is faster for than using
\c QFileInfo(file).exists() for file system access.
*/
bool QFileInfo::exists(const QString &file)
{
QFileSystemEntry entry(file);
QFileSystemMetaData data;
QAbstractFileEngine *engine =
QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry, data);
// Expensive fallback to non-QFileSystemEngine implementation
if (engine)
return QFileInfo(file).exists();
QFileSystemEngine::fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute);
return data.exists();
}
/*! /*!
Refreshes the information about the file, i.e. reads in information Refreshes the information about the file, i.e. reads in information
from the file system the next time a cached property is fetched. from the file system the next time a cached property is fetched.

View File

@ -84,6 +84,7 @@ public:
void setFile(const QFile &file); void setFile(const QFile &file);
void setFile(const QDir &dir, const QString &file); void setFile(const QDir &dir, const QString &file);
bool exists() const; bool exists() const;
static bool exists(const QString &file);
void refresh(); void refresh();
QString filePath() const; QString filePath() const;

View File

@ -438,6 +438,7 @@ void tst_QFileInfo::exists()
QFileInfo fi(path); QFileInfo fi(path);
QCOMPARE(fi.exists(), expected); QCOMPARE(fi.exists(), expected);
QCOMPARE(QFileInfo::exists(path), expected);
} }
void tst_QFileInfo::absolutePath_data() void tst_QFileInfo::absolutePath_data()

View File

@ -52,6 +52,8 @@ class qfileinfo : public QObject
{ {
Q_OBJECT Q_OBJECT
private slots: private slots:
void existsTemporary();
void existsStatic();
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
void symLinkTargetPerformanceLNK(); void symLinkTargetPerformanceLNK();
void symLinkTargetPerformanceMounpoint(); void symLinkTargetPerformanceMounpoint();
@ -70,6 +72,18 @@ void qfileinfo::cleanupTestCase()
{ {
} }
void qfileinfo::existsTemporary()
{
QString appPath = QCoreApplication::applicationFilePath();
QBENCHMARK { QFileInfo(appPath).exists(); }
}
void qfileinfo::existsStatic()
{
QString appPath = QCoreApplication::applicationFilePath();
QBENCHMARK { QFileInfo::exists(appPath); }
}
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
void qfileinfo::symLinkTargetPerformanceLNK() void qfileinfo::symLinkTargetPerformanceLNK()
{ {