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 <a.samirh78@gmail.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 47375a213f0751b5649ee7c4ae47fae020f9c77f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-08-08 10:02:46 +02:00 committed by Qt Cherry-pick Bot
parent 74f33348f2
commit 7cd5f6952e

View File

@ -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
}
}
}
}