diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-08-08 10:02:46 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-08-14 23:11:54 +0300 |
commit | 47375a213f0751b5649ee7c4ae47fae020f9c77f (patch) | |
tree | 6d83d5db68e32d66e7186888187322b24676a40b /tests/benchmarks/corelib/mimetypes | |
parent | 40afcb9d01713c02737f6f83a59437fc8320ef0b (diff) |
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).
Pick-to: 6.6 6.5
Task-number: QTBUG-115839
Change-Id: I844ae38104bb2980ea194b85f9017a3e95791ea2
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/benchmarks/corelib/mimetypes')
-rw-r--r-- | tests/benchmarks/corelib/mimetypes/qmimedatabase/tst_bench_qmimedatabase.cpp | 29 |
1 files 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 bd30c5b812..3f6a5cc969 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 + } } } } |