Use QChar::fromUcs4(i) rather than QChar(i) on out-of-range i
Follow-up to commit 915be6606e
, catching
some benchmarks that took for granted they can assign an arbitrary int
to QChar. Since 6.0 this has triggered an assertion.
Given the choice between limiting the range (from 100000 to 0x10000)
and actually handling the out-of-range values as UCS-4 data, the
latter seemed like a more interesting test.
At the same time, take the construction of the strings out of the
loop, as that's not a QMap performance matter, it's a QString one.
Pick-to: 6.1 6.2
Task-number: QTBUG-91713
Change-Id: Id6abab08b5c879f0f764350f66d6aa1dd9f1620a
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
parent
9159ce0373
commit
b5950f6aff
@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Copyright (C) 2021 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
@ -61,8 +61,27 @@ private slots:
|
||||
void insertion_string_int2_hint();
|
||||
|
||||
void insertMap();
|
||||
|
||||
private:
|
||||
QStringList helloEachWorld(int count);
|
||||
};
|
||||
|
||||
QStringList tst_QMap::helloEachWorld(int count)
|
||||
{
|
||||
QStringList result;
|
||||
result.reserve(count);
|
||||
result << QStringLiteral("Hello World"); // at index 0, not used
|
||||
|
||||
char16_t name[] = u"Hello World";
|
||||
QStringView str(name);
|
||||
for (int i = 1; i < count; ++i) {
|
||||
auto p = name + 6; // In the gap between words.
|
||||
for (const auto ch : QChar::fromUcs4(i))
|
||||
p++[0] = ch;
|
||||
result << str.toString();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void tst_QMap::insertion_int_int()
|
||||
{
|
||||
@ -124,16 +143,13 @@ void tst_QMap::insertion_int_string()
|
||||
void tst_QMap::insertion_string_int()
|
||||
{
|
||||
QMap<QString, int> map;
|
||||
QString str("Hello World");
|
||||
const QStringList names = helloEachWorld(100000);
|
||||
QBENCHMARK {
|
||||
for (int i = 1; i < 100000; ++i) {
|
||||
str[0] = QChar(i);
|
||||
map.insert(str, i);
|
||||
}
|
||||
for (int i = 1; i < 100000; ++i)
|
||||
map.insert(names.at(i), i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void tst_QMap::lookup_int_int()
|
||||
{
|
||||
QMap<int, int> map;
|
||||
@ -163,18 +179,14 @@ void tst_QMap::lookup_int_string()
|
||||
void tst_QMap::lookup_string_int()
|
||||
{
|
||||
QMap<QString, int> map;
|
||||
QString str("Hello World");
|
||||
for (int i = 1; i < 100000; ++i) {
|
||||
str[0] = QChar(i);
|
||||
map.insert(str, i);
|
||||
}
|
||||
const QStringList names = helloEachWorld(100000);
|
||||
for (int i = 1; i < 100000; ++i)
|
||||
map.insert(names.at(i), i);
|
||||
|
||||
int sum = 0;
|
||||
QBENCHMARK {
|
||||
for (int i = 1; i < 100000; ++i) {
|
||||
str[0] = QChar(i);
|
||||
sum += map.value(str);
|
||||
}
|
||||
for (int i = 1; i < 100000; ++i)
|
||||
sum += map.value(names.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user