From 7cd5f6952e1e89705a73a20d0ee9f5c7385b5052 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 8 Aug 2023 10:02:46 +0200 Subject: [PATCH] QMimeDatabase benchmark: port away from Q_FOREACH The original code duplicated contained elements of a QStringList by repeated appending it to itself, preventing the container from being marked const. Instead, keep a list of unique mime-types, and iterate over the list eight times. As a drive-by, port from QList to a C array ("never use a dynamically-sized container for statically-sized data"), use u""_s UDLs (since we're touching almost all lines of the function, anyway, also in the unrelated mimeTypeForName() call). This allows porting the Q_FOREACH loop (which anyway cannot deal with C arrays) to a ranged for one (which can). Task-number: QTBUG-115839 Change-Id: I844ae38104bb2980ea194b85f9017a3e95791ea2 Reviewed-by: Ahmad Samir Reviewed-by: Fabian Kosmale (cherry picked from commit 47375a213f0751b5649ee7c4ae47fae020f9c77f) Reviewed-by: Qt Cherry-pick Bot --- .../qmimedatabase/tst_bench_qmimedatabase.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/benchmarks/corelib/mimetypes/qmimedatabase/tst_bench_qmimedatabase.cpp b/tests/benchmarks/corelib/mimetypes/qmimedatabase/tst_bench_qmimedatabase.cpp index bd30c5b8124..3f6a5cc9693 100644 --- a/tests/benchmarks/corelib/mimetypes/qmimedatabase/tst_bench_qmimedatabase.cpp +++ b/tests/benchmarks/corelib/mimetypes/qmimedatabase/tst_bench_qmimedatabase.cpp @@ -53,22 +53,27 @@ void tst_QMimeDatabase::inheritsPerformance() { // Check performance of inherits(). // This benchmark (which started in 2009 in kmimetypetest.cpp) uses 40 mimetypes. - QStringList mimeTypes; - mimeTypes << QLatin1String("image/jpeg") << QLatin1String("image/png") << QLatin1String("image/tiff") << QLatin1String("text/plain") << QLatin1String("text/html"); - mimeTypes += mimeTypes; - mimeTypes += mimeTypes; - mimeTypes += mimeTypes; - QCOMPARE(mimeTypes.size(), 40); + // (eight groups of five unique ones) + const QString uniqueMimeTypes[] = { + u"image/jpeg"_s, + u"image/png"_s, + u"image/tiff"_s, + u"text/plain"_s, + u"text/html"_s, + }; + constexpr size_t NumOuterLoops = 40 / std::size(uniqueMimeTypes); QMimeDatabase db; - QMimeType mime = db.mimeTypeForName(QString::fromLatin1("text/x-chdr")); + const QMimeType mime = db.mimeTypeForName(u"text/x-chdr"_s); QVERIFY(mime.isValid()); QString match; QBENCHMARK { - for (const QString &mt : std::as_const(mimeTypes)) { - if (mime.inherits(mt)) { - match = mt; - // of course there would normally be a "break" here, but we're testing worse-case - // performance here + for (size_t i = 0; i < NumOuterLoops; ++i) { + for (const QString &mt : uniqueMimeTypes) { + if (mime.inherits(mt)) { + match = mt; + // of course there would normally be a "break" here, but + // we're testing worse-case performance here + } } } }