diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2021-07-12 17:50:04 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2021-07-27 14:58:41 +0200 |
commit | 18113e22e92a7b8d759fd0f9c9d696ee7e97a849 (patch) | |
tree | e2bf55e84d61e4fb8a7aee4d84913e3431894679 /tests/benchmarks/corelib/tools/qsharedpointer | |
parent | df62fdb51c2cd8746c0fe38b5af4b575cbc2bf00 (diff) |
Add a benchmark for QSharedPointer
Initial results with GCC:
Even though GCC's assembly looks rather bad because of the implicit
fall-back from atomics to non-atomics for single-threaded
applications, libstdc++'s shared_ptr still performs twice as fast as
QSharedPointer, proving that the branch predictor eats libstdc++'s
is-multi-threaded-check for breakfast and the two atomic operations of
a QSharedPointer copy vs. one for std::shared_ptr dominate the
run-time.
********* Start testing of tst_QSharedPointer *********
Config: Using QtTest library 6.2.0, Qt 6.2.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 11.1.1 20210712), ubuntu 20.04
PASS : tst_QSharedPointer::initTestCase()
PASS : tst_QSharedPointer::refAndDeref_null_QSP_int()
RESULT : tst_QSharedPointer::refAndDeref_null_QSP_int():
0.0000024 msecs per iteration (total: 81, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_null_SSP_int()
RESULT : tst_QSharedPointer::refAndDeref_null_SSP_int():
0.0000024 msecs per iteration (total: 81, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_null_BSP_int()
RESULT : tst_QSharedPointer::refAndDeref_null_BSP_int():
0.0000025 msecs per iteration (total: 87, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_null_QSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_null_QSP_QString():
0.0000025 msecs per iteration (total: 86, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_null_SSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_null_SSP_QString():
0.0000023 msecs per iteration (total: 80, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_null_BSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_null_BSP_QString():
0.0000026 msecs per iteration (total: 88, iterations: 33554432)
PASS : tst_QSharedPointer::refAndDeref_nonnull_QSP_int()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_QSP_int():
0.000019 msecs per iteration (total: 83, iterations: 4194304)
PASS : tst_QSharedPointer::refAndDeref_nonnull_SSP_int()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_SSP_int():
0.000010 msecs per iteration (total: 90, iterations: 8388608)
PASS : tst_QSharedPointer::refAndDeref_nonnull_BSP_int()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_BSP_int():
0.0000094 msecs per iteration (total: 79, iterations: 8388608)
PASS : tst_QSharedPointer::refAndDeref_nonnull_QSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_QSP_QString():
0.000017 msecs per iteration (total: 75, iterations: 4194304)
PASS : tst_QSharedPointer::refAndDeref_nonnull_SSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_SSP_QString():
0.000010 msecs per iteration (total: 90, iterations: 8388608)
PASS : tst_QSharedPointer::refAndDeref_nonnull_BSP_QString()
RESULT : tst_QSharedPointer::refAndDeref_nonnull_BSP_QString():
0.0000091 msecs per iteration (total: 77, iterations: 8388608)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_QSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_QSP_int():
0.000016 msecs per iteration (total: 68, iterations: 4194304)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_SSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_SSP_int():
0.000025 msecs per iteration (total: 53, iterations: 2097152)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_BSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_BSP_int():
0.000027 msecs per iteration (total: 58, iterations: 2097152)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_QSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_QSP_QString():
0.000016 msecs per iteration (total: 71, iterations: 4194304)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_SSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_SSP_QString():
0.000027 msecs per iteration (total: 58, iterations: 2097152)
PASS : tst_QSharedPointer::threadedRefAndDeref_null_BSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_null_BSP_QString():
0.000017 msecs per iteration (total: 73, iterations: 4194304)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_QSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_QSP_int():
0.00073 msecs per iteration (total: 96, iterations: 131072)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_SSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_SSP_int():
0.000408 msecs per iteration (total: 107, iterations: 262144)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_BSP_int()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_BSP_int():
0.00033 msecs per iteration (total: 89, iterations: 262144)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_QSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_QSP_QString():
0.000877 msecs per iteration (total: 115, iterations: 131072)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_SSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_SSP_QString():
0.00033 msecs per iteration (total: 89, iterations: 262144)
PASS : tst_QSharedPointer::threadedRefAndDeref_nonnull_BSP_QString()
RESULT : tst_QSharedPointer::threadedRefAndDeref_nonnull_BSP_QString():
0.000385 msecs per iteration (total: 101, iterations: 262144)
PASS : tst_QSharedPointer::cleanupTestCase()
Totals: 26 passed, 0 failed, 0 skipped, 0 blacklisted, 7995ms
********* Finished testing of tst_QSharedPointer *********
Change-Id: I0bed70142ffdbde6898ec0e27cb470b50fc0e97d
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/benchmarks/corelib/tools/qsharedpointer')
-rw-r--r-- | tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp | 123 |
2 files changed, 132 insertions, 0 deletions
diff --git a/tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt b/tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt new file mode 100644 index 0000000000..5103fc3229 --- /dev/null +++ b/tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt @@ -0,0 +1,9 @@ +qt_internal_add_benchmark(tst_bench_shared_ptr + SOURCES + tst_bench_shared_ptr.cpp + INCLUDE_DIRECTORIES + . + PUBLIC_LIBRARIES + Qt::Core + Qt::Test +) diff --git a/tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp b/tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp new file mode 100644 index 0000000000..4430282403 --- /dev/null +++ b/tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> +** 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 <QObject> +#include <QScopeGuard> +#include <QSharedPointer> +#include <QTest> + +#include <atomic> +#include <memory> +#include <thread> +#include <vector> + +#if __has_include(<boost/shared_ptr.hpp>) +# include <boost/shared_ptr.hpp> +# include <boost/make_shared.hpp> +# define ONLY_IF_BOOST(x) x +#else +# define ONLY_IF_BOOST(x) QSKIP("This benchmark requires Boost.SharedPtr.") +#endif + +class tst_QSharedPointer : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void refAndDeref_null_QSP_int() { refAndDeref<QSharedPointer<int>>(); } + void refAndDeref_null_SSP_int() { refAndDeref<std::shared_ptr<int>>(); } + void refAndDeref_null_BSP_int() { ONLY_IF_BOOST(refAndDeref<boost::shared_ptr<int>>()); } + + void refAndDeref_null_QSP_QString() { refAndDeref<QSharedPointer<QString>>(); } + void refAndDeref_null_SSP_QString() { refAndDeref<std::shared_ptr<QString>>(); } + void refAndDeref_null_BSP_QString() { ONLY_IF_BOOST(refAndDeref<boost::shared_ptr<QString>>()); } + + void refAndDeref_nonnull_QSP_int() { refAndDeref(QSharedPointer<int>::create(42)); } + void refAndDeref_nonnull_SSP_int() { refAndDeref(std::make_shared<int>(42)); } + void refAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(refAndDeref(boost::make_shared<int>(42))); } + + void refAndDeref_nonnull_QSP_QString() { refAndDeref(QSharedPointer<QString>::create(QStringLiteral("Hello"))); } + void refAndDeref_nonnull_SSP_QString() { refAndDeref(std::make_shared<QString>(QStringLiteral("Hello"))); } + void refAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(refAndDeref(boost::make_shared<QString>(QStringLiteral("Hello")))); } + +private: + template <typename SP> + void refAndDeref(SP sp = {}) + { + QBENCHMARK { + [[maybe_unused]] auto copy = sp; + } + } + +private Q_SLOTS: + void threadedRefAndDeref_null_QSP_int() { threadedRefAndDeref<QSharedPointer<int>>(); } + void threadedRefAndDeref_null_SSP_int() { threadedRefAndDeref<std::shared_ptr<int>>(); } + void threadedRefAndDeref_null_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref<boost::shared_ptr<int>>()); } + + void threadedRefAndDeref_null_QSP_QString() { threadedRefAndDeref<QSharedPointer<QString>>(); } + void threadedRefAndDeref_null_SSP_QString() { threadedRefAndDeref<std::shared_ptr<QString>>(); } + void threadedRefAndDeref_null_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref<boost::shared_ptr<QString>>()); } + + void threadedRefAndDeref_nonnull_QSP_int() { threadedRefAndDeref(QSharedPointer<int>::create(42)); } + void threadedRefAndDeref_nonnull_SSP_int() { threadedRefAndDeref(std::make_shared<int>(42)); } + void threadedRefAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared<int>(42))); } + + void threadedRefAndDeref_nonnull_QSP_QString() { threadedRefAndDeref(QSharedPointer<QString>::create(QStringLiteral("Hello"))); } + void threadedRefAndDeref_nonnull_SSP_QString() { threadedRefAndDeref(std::make_shared<QString>(QStringLiteral("Hello"))); } + void threadedRefAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared<QString>(QStringLiteral("Hello")))); } + +private: + template <typename SP> + void threadedRefAndDeref(SP sp = {}) + { + std::atomic<bool> cancel = false; + std::vector<std::thread> threads; + const auto numCores = std::max(2U, std::thread::hardware_concurrency()); + for (uint i = 0; i < numCores - 1; ++i) { + threads.emplace_back([sp, &cancel] { + while (!cancel.load(std::memory_order_relaxed)) { + for (int i = 0; i < 100; ++i) + [[maybe_unused]] auto copy = sp; + } + }); + } + const auto join = qScopeGuard([&] { + cancel.store(true, std::memory_order_relaxed); + for (auto &t : threads) + t.join(); + }); + + QBENCHMARK { + [[maybe_unused]] auto copy = sp; + } + } +}; + +QTEST_MAIN(tst_QSharedPointer) + +#include "tst_bench_shared_ptr.moc" |