QDir::entryList: don't fill a QFileInfo cache if we just want names

This improves the performance of tst_QDir_10000::iDontWantAnyStat
(QDir::entryList Unsorted)
from 9.7ms to 7.2ms, i.e. the same as iDontWantAnyStatIterator
(QDirIterator).

Change-Id: I3faf8af1a55575df97912b1ce720492c8fd903c9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2023-02-02 20:11:37 +01:00
parent eae031d5c3
commit 94057bd800
2 changed files with 20 additions and 8 deletions

View File

@ -100,7 +100,7 @@ QDirPrivate::QDirPrivate(const QDirPrivate &copy)
dirEntry(copy.dirEntry)
{
QMutexLocker locker(&copy.fileCache.mutex);
fileCache.fileListsInitialized = copy.fileCache.fileListsInitialized;
fileCache.fileListsInitialized = copy.fileCache.fileListsInitialized.load();
fileCache.files = copy.fileCache.files;
fileCache.fileInfos = copy.fileCache.fileInfos;
fileCache.absoluteDirEntry = copy.fileCache.absoluteDirEntry;
@ -1378,17 +1378,29 @@ QStringList QDir::entryList(const QStringList &nameFilters, Filters filters,
if (sort == NoSort)
sort = d->sort;
const bool needsSorting = (sort & QDir::SortByMask) != QDir::Unsorted;
if (filters == d->filters && sort == d->sort && nameFilters == d->nameFilters) {
d->initFileLists(*this);
return d->fileCache.files;
// Don't fill a QFileInfo cache if we just need names
if (needsSorting || d->fileCache.fileListsInitialized) {
d->initFileLists(*this);
return d->fileCache.files;
}
}
QFileInfoList l;
QDirIterator it(d->dirEntry.filePath(), nameFilters, filters);
while (it.hasNext())
l.append(it.nextFileInfo());
QStringList ret;
d->sortFileList(sort, l, &ret, nullptr);
if (needsSorting) {
QFileInfoList l;
while (it.hasNext())
l.append(it.nextFileInfo());
d->sortFileList(sort, l, &ret, nullptr);
} else {
while (it.hasNext()) {
it.next();
ret.append(it.fileName());
}
}
return ret;
}

View File

@ -71,7 +71,7 @@ public:
QMutex mutex;
QStringList files;
QFileInfoList fileInfos;
bool fileListsInitialized = false;
std::atomic<bool> fileListsInitialized = false;
QFileSystemEntry absoluteDirEntry;
QFileSystemMetaData metaData;
};