Improve performance of QColor::name, now more than 4 times faster

Before: HexRgb: 0.00230 ms per iteration, HexArgb: 0.00290 ms per iteration
After:  HexRgb: 0.00051 ms per iteration, HexArgb: 0.00061 ms per iteration

This showed up as a relevant optimization when profiling KIconLoader
which uses QColor::name() as part of the key -- thanks to Mark Gaiser for
the investigation and first suggestion of a solution. I have also seen
customer code writing a replacement for QColor::name() because it was
too slow to be used as a hash key.

Change-Id: I009ccdd712ea0d869d466e2c9894e0cea58f0e68
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
David Faure 2016-08-04 10:01:21 +02:00
parent eb4bcdd8ce
commit 434c522695
5 changed files with 80 additions and 2 deletions

View File

@ -521,9 +521,10 @@ QString QColor::name(NameFormat format) const
{
switch (format) {
case HexRgb:
return QString::asprintf("#%02x%02x%02x", red(), green(), blue());
return QLatin1Char('#') + QString::number(rgba() | 0x1000000, 16).rightRef(6);
case HexArgb:
return QString::asprintf("#%02x%02x%02x%02x", alpha(), red(), green(), blue());
// it's called rgba() but it does return AARRGGBB
return QLatin1Char('#') + QString::number(rgba() | 0x100000000, 16).rightRef(8);
}
return QString();
}

View File

@ -284,6 +284,8 @@ void tst_QColor::name_data()
QTest::newRow("global color darkMagenta") << QColor(Qt::darkMagenta) << "#800080" << QColor::HexRgb;
QTest::newRow("global color darkYellow") << QColor(Qt::darkYellow) << "#808000" << QColor::HexRgb;
QTest::newRow("transparent red") << QColor(255, 0, 0, 102) << "#66ff0000" << QColor::HexArgb;
QTest::newRow("fully_transparent_green_rgb") << QColor(0, 0, 255, 0) << "#0000ff" << QColor::HexRgb;
QTest::newRow("fully_transparent_green_argb") << QColor(0, 0, 255, 0) << "#000000ff" << QColor::HexArgb;
}
void tst_QColor::name()

View File

@ -1,5 +1,6 @@
TEMPLATE = subdirs
SUBDIRS = \
qcolor \
qpainter \
qregion \
qtransform \

View File

@ -0,0 +1,7 @@
QT += testlib
QT += gui-private
TEMPLATE = app
TARGET = tst_bench_qcolor
SOURCES += tst_qcolor.cpp

View File

@ -0,0 +1,67 @@
/****************************************************************************
**
** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QColor>
class tst_QColor : public QObject
{
Q_OBJECT
private slots:
void nameRgb();
void nameArgb();
};
void tst_QColor::nameRgb()
{
QColor color(128, 255, 10);
QCOMPARE(color.name(), QStringLiteral("#80ff0a"));
QBENCHMARK {
color.name();
}
}
void tst_QColor::nameArgb()
{
QColor color(128, 255, 0, 102);
QCOMPARE(color.name(QColor::HexArgb), QStringLiteral("#6680ff00"));
QBENCHMARK {
color.name(QColor::HexArgb);
}
}
QTEST_MAIN(tst_QColor)
#include "tst_qcolor.moc"