diff options
Diffstat (limited to 'tests/benchmarks/corelib/tools/qhash')
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/main.h | 61 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/outofline.cpp | 47 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/qhash.pro | 6 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.cpp (renamed from tests/benchmarks/corelib/tools/qhash/main.cpp) | 90 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.h | 48 |
6 files changed, 116 insertions, 149 deletions
diff --git a/tests/benchmarks/corelib/tools/qhash/CMakeLists.txt b/tests/benchmarks/corelib/tools/qhash/CMakeLists.txt index 760b1848d7..9002cc0723 100644 --- a/tests/benchmarks/corelib/tools/qhash/CMakeLists.txt +++ b/tests/benchmarks/corelib/tools/qhash/CMakeLists.txt @@ -1,15 +1,18 @@ -# Generated from qhash.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### -## tst_hash Binary: +## tst_bench_qhash Binary: ##################################################################### -qt_add_benchmark(tst_hash +qt_internal_add_benchmark(tst_bench_qhash SOURCES - main.cpp + tst_bench_qhash.cpp outofline.cpp + NO_PCH_SOURCES + tst_bench_qhash.cpp # undef QT_NO_FOREACH INCLUDE_DIRECTORIES . - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/tools/qhash/main.h b/tests/benchmarks/corelib/tools/qhash/main.h deleted file mode 100644 index eca4b61d58..0000000000 --- a/tests/benchmarks/corelib/tools/qhash/main.h +++ /dev/null @@ -1,61 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtTest module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QString> - -struct Qt4String : QString -{ - Qt4String() {} - Qt4String(const QString &s) : QString(s) {} -}; - -QT_BEGIN_NAMESPACE -uint qHash(const Qt4String &); -QT_END_NAMESPACE - -struct Qt50String : QString -{ - Qt50String() {} - Qt50String(const QString &s) : QString(s) {} -}; - -QT_BEGIN_NAMESPACE -uint qHash(const Qt50String &, uint seed = 0); -QT_END_NAMESPACE - - -struct JavaString : QString -{ - JavaString() {} - JavaString(const QString &s) : QString(s) {} -}; - -QT_BEGIN_NAMESPACE -uint qHash(const JavaString &); -QT_END_NAMESPACE - diff --git a/tests/benchmarks/corelib/tools/qhash/outofline.cpp b/tests/benchmarks/corelib/tools/qhash/outofline.cpp index 4e449e76b4..5b16c36ffb 100644 --- a/tests/benchmarks/corelib/tools/qhash/outofline.cpp +++ b/tests/benchmarks/corelib/tools/qhash/outofline.cpp @@ -1,38 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtTest module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -#include "main.h" +#include "tst_bench_qhash.h" QT_BEGIN_NAMESPACE -uint qHash(const Qt4String &str) +size_t qHash(const Qt4String &str, size_t /* never used */) { - int n = str.length(); + qsizetype n = str.size(); const QChar *p = str.unicode(); uint h = 0; @@ -44,11 +19,11 @@ uint qHash(const Qt4String &str) return h; } -uint qHash(const Qt50String &key, uint seed) +size_t qHash(const Qt50String &key, size_t seed) { const QChar *p = key.unicode(); - int len = key.size(); - uint h = seed; + qsizetype len = key.size(); + size_t h = seed; for (int i = 0; i < len; ++i) h = 31 * h + p[i].unicode(); return h; @@ -65,10 +40,10 @@ uint qHash(const Qt50String &key, uint seed) // Still, we can avoid writing the multiplication as "(h << 5) - h" // -- the compiler will turn it into a shift and an addition anyway // (for instance, gcc 4.4 does that even at -O0). -uint qHash(const JavaString &str) +size_t qHash(const JavaString &str, size_t /* never used */) { - const unsigned short *p = (unsigned short *)str.constData(); - const int len = str.size(); + const auto *p = reinterpret_cast<const char16_t *>(str.constData()); + const qsizetype len = str.size(); uint h = 0; diff --git a/tests/benchmarks/corelib/tools/qhash/qhash.pro b/tests/benchmarks/corelib/tools/qhash/qhash.pro deleted file mode 100644 index f9a873d096..0000000000 --- a/tests/benchmarks/corelib/tools/qhash/qhash.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += benchmark -QT = core testlib - -INCLUDEPATH += . -TARGET = tst_hash -SOURCES += main.cpp outofline.cpp diff --git a/tests/benchmarks/corelib/tools/qhash/main.cpp b/tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.cpp index f9b1624fcc..1a62a48437 100644 --- a/tests/benchmarks/corelib/tools/qhash/main.cpp +++ b/tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.cpp @@ -1,33 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "main.h" +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses + +#include "tst_bench_qhash.h" #include <QFile> #include <QHash> @@ -36,6 +13,8 @@ #include <QUuid> #include <QTest> +static constexpr quint64 RandomSeed32 = 1045982819; +static constexpr quint64 RandomSeed64 = QtPrivate::QHashCombine{}(RandomSeed32, RandomSeed32); class tst_QHash : public QObject { @@ -54,6 +33,8 @@ private slots: void hashing_current_data() { data(); } void hashing_current() { hashing_template<QString>(); } + void hashing_qbytearray_data() { data(); } + void hashing_qbytearray() { hashing_template<QByteArray>(); } void hashing_qt50_data() { data(); } void hashing_qt50() { hashing_template<Qt50String>(); } void hashing_qt4_data() { data(); } @@ -61,15 +42,25 @@ private slots: void hashing_javaString_data() { data(); } void hashing_javaString() { hashing_template<JavaString>(); } + void hashing_nonzero_current_data() { data(); } + void hashing_nonzero_current() { hashing_nonzero_template<QString>(); } + void hashing_nonzero_qbytearray_data() { data(); } + void hashing_nonzero_qbytearray() { hashing_nonzero_template<QByteArray>(); } + void hashing_nonzero_qlatin1string_data() { data(); } + void hashing_nonzero_qlatin1string() { hashing_nonzero_template<OwningLatin1String>(); } + private: void data(); template <typename String> void qhash_template(); - template <typename String> void hashing_template(); + template <typename String, size_t Seed = 0> void hashing_template(); + template <typename String> void hashing_nonzero_template() + { hashing_template<String, size_t(RandomSeed64)>(); } QStringList smallFilePaths; QStringList uuids; QStringList dict; QStringList numbers; + QStringList longstrings; }; ///////////////////// QHash ///////////////////// @@ -78,7 +69,10 @@ private: void tst_QHash::initTestCase() { - // small list of file paths + QHashSeed::setDeterministicGlobalSeed(); + + // small list of strings (that happen to look like file paths produced long + // ago by cd ../.. && find . -print, but that's irrelevant). QFile smallPathsData(QFINDTESTDATA("paths_small_data.txt")); QVERIFY(smallPathsData.open(QIODevice::ReadOnly)); smallFilePaths = QString::fromLatin1(smallPathsData.readAll()).split(QLatin1Char('\n')); @@ -88,15 +82,17 @@ void tst_QHash::initTestCase() // guaranteed to be completely random, generated by http://xkcd.com/221/ QUuid ns = QUuid("{f43d2ef3-2fe9-4563-a6f5-5a0100c2d699}"); uuids.reserve(smallFilePaths.size()); + longstrings.reserve(smallFilePaths.size()); foreach (const QString &path, smallFilePaths) uuids.append(QUuid::createUuidV5(ns, path).toString()); - + for (qsizetype i = 0; i < uuids.size(); ++i) + longstrings.append(uuids.at(i).repeated(8)); // lots of strings with alphabetical characters, vaguely reminiscent of // a dictionary. // - // this programatically generates a series like: + // this programmatically generates a series like: // AAAAAA // AAAAAB // AAAAAC @@ -109,7 +105,7 @@ void tst_QHash::initTestCase() QByteArray id("AAAAAAA"); if (dict.isEmpty()) { - for (int i = id.length() - 1; i > 0;) { + for (int i = id.size() - 1; i > 0;) { dict.append(id); char c = id.at(i); id[i] = ++c; @@ -132,6 +128,7 @@ void tst_QHash::data() QTest::addColumn<QStringList>("items"); QTest::newRow("paths-small") << smallFilePaths; QTest::newRow("uuids-list") << uuids; + QTest::newRow("longstrings-list") << longstrings; QTest::newRow("dictionary") << dict; QTest::newRow("numbers") << numbers; } @@ -152,22 +149,33 @@ template <typename String> void tst_QHash::qhash_template() } } -template <typename String> void tst_QHash::hashing_template() +template <typename String, size_t Seed> void tst_QHash::hashing_template() { // just the hashing function QFETCH(QStringList, items); QList<String> realitems; realitems.reserve(items.size()); - foreach (const QString &s, items) - realitems.append(s); + foreach (const QString &s, items) { + if constexpr (std::is_same_v<QString::value_type, typename String::value_type>) { + realitems.append(s); + } else if constexpr (sizeof(typename String::value_type) == 1) { + realitems.append(String(s.toLatin1())); + } + } QBENCHMARK { - for (int i = 0, n = realitems.size(); i != n; ++i) - (void)qHash(realitems.at(i)); + for (int i = 0, n = realitems.size(); i != n; ++i) { + volatile size_t h = qHash(realitems.at(i), Seed); + (void)h; +#ifdef Q_CC_GNU + // "use" h + asm ("" : "+r" (h)); +#endif + } } } QTEST_MAIN(tst_QHash) -#include "main.moc" +#include "tst_bench_qhash.moc" diff --git a/tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.h b/tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.h new file mode 100644 index 0000000000..501b4a8b7f --- /dev/null +++ b/tests/benchmarks/corelib/tools/qhash/tst_bench_qhash.h @@ -0,0 +1,48 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QHashFunctions> +#include <QString> + +struct OwningLatin1String : QByteArray +{ + OwningLatin1String() = default; + OwningLatin1String(const QByteArray &a) : QByteArray(a) {} + OwningLatin1String(QByteArray &&a) : QByteArray(std::move(a)) {} +}; +QT_BEGIN_NAMESPACE +inline size_t qHash(const OwningLatin1String &s, size_t seed = 0) +{ return qHash(QLatin1StringView(s), seed); } +QT_END_NAMESPACE + +struct Qt4String : QString +{ + Qt4String() {} + Qt4String(const QString &s) : QString(s) {} +}; + +QT_BEGIN_NAMESPACE +size_t qHash(const Qt4String &, size_t = 0); +QT_END_NAMESPACE + +struct Qt50String : QString +{ + Qt50String() {} + Qt50String(const QString &s) : QString(s) {} +}; + +QT_BEGIN_NAMESPACE +size_t qHash(const Qt50String &, size_t seed = 0); +QT_END_NAMESPACE + + +struct JavaString : QString +{ + JavaString() {} + JavaString(const QString &s) : QString(s) {} +}; + +QT_BEGIN_NAMESPACE +size_t qHash(const JavaString &, size_t = 0); +QT_END_NAMESPACE + |