From 81a0753299fb8cc3e6b4b5e41a0292024ccea9e5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2024 07:23:38 +0200 Subject: [PATCH] QDir: replace a QScopedArrayPointer with a QVLA QDirSortItem contains three implicitly-shared members (two QStrings and one QFileInfo), so the mere default-initialization of the QDirSortItem[n] array with which we initialize the QScopedArrayPointer will take quite some time. Using QVarLengthArray, OTOH, delays construction until we have the data to initialize the sort items, saving n default constructor and n move assignment operator calls, plus the memory allocation for the case where we have less than 65 elements to sort. This code precedes the start of the public history, but the emplacement won't work before 905bc6293354a0d3ee832b6dd3f632a647f809f3 introduced the QDirSortItem ctor, so not picking back further than that. Pick-to: 6.7 Change-Id: I262217eabaded2a6aef08b87fc6369812bc8958a Reviewed-by: Thiago Macieira --- src/corelib/io/qdir.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 9291201d881..05947f33804 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -313,9 +313,10 @@ inline void QDirPrivate::sortFileList(QDir::SortFlags sort, const QFileInfoList names->append(fi.fileName()); } } else { - QScopedArrayPointer si(new QDirSortItem[n]); + QVarLengthArray si; + si.reserve(n); for (qsizetype i = 0; i < n; ++i) - si[i] = QDirSortItem{l.at(i), sort}; + si.emplace_back(l.at(i), sort); #ifndef QT_BOOTSTRAPPED if (sort.testAnyFlag(QDir::LocaleAware)) {