From 18113e22e92a7b8d759fd0f9c9d696ee7e97a849 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 12 Jul 2021 17:50:04 +0200 Subject: [PATCH] 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 Reviewed-by: Edward Welbourne Reviewed-by: Lars Knoll --- tests/benchmarks/corelib/tools/CMakeLists.txt | 1 + .../tools/qsharedpointer/CMakeLists.txt | 9 ++ .../qsharedpointer/tst_bench_shared_ptr.cpp | 123 ++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt create mode 100644 tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp diff --git a/tests/benchmarks/corelib/tools/CMakeLists.txt b/tests/benchmarks/corelib/tools/CMakeLists.txt index c1b5cad1aa7..805aa922297 100644 --- a/tests/benchmarks/corelib/tools/CMakeLists.txt +++ b/tests/benchmarks/corelib/tools/CMakeLists.txt @@ -8,5 +8,6 @@ add_subdirectory(qlist) add_subdirectory(qmap) add_subdirectory(qrect) add_subdirectory(qringbuffer) +add_subdirectory(qsharedpointer) add_subdirectory(qstack) add_subdirectory(qvector) diff --git a/tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt b/tests/benchmarks/corelib/tools/qsharedpointer/CMakeLists.txt new file mode 100644 index 00000000000..5103fc32293 --- /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 00000000000..4430282403a --- /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 +** 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 +#include +#include +#include + +#include +#include +#include +#include + +#if __has_include() +# include +# include +# 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>(); } + void refAndDeref_null_SSP_int() { refAndDeref>(); } + void refAndDeref_null_BSP_int() { ONLY_IF_BOOST(refAndDeref>()); } + + void refAndDeref_null_QSP_QString() { refAndDeref>(); } + void refAndDeref_null_SSP_QString() { refAndDeref>(); } + void refAndDeref_null_BSP_QString() { ONLY_IF_BOOST(refAndDeref>()); } + + void refAndDeref_nonnull_QSP_int() { refAndDeref(QSharedPointer::create(42)); } + void refAndDeref_nonnull_SSP_int() { refAndDeref(std::make_shared(42)); } + void refAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(refAndDeref(boost::make_shared(42))); } + + void refAndDeref_nonnull_QSP_QString() { refAndDeref(QSharedPointer::create(QStringLiteral("Hello"))); } + void refAndDeref_nonnull_SSP_QString() { refAndDeref(std::make_shared(QStringLiteral("Hello"))); } + void refAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(refAndDeref(boost::make_shared(QStringLiteral("Hello")))); } + +private: + template + void refAndDeref(SP sp = {}) + { + QBENCHMARK { + [[maybe_unused]] auto copy = sp; + } + } + +private Q_SLOTS: + void threadedRefAndDeref_null_QSP_int() { threadedRefAndDeref>(); } + void threadedRefAndDeref_null_SSP_int() { threadedRefAndDeref>(); } + void threadedRefAndDeref_null_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref>()); } + + void threadedRefAndDeref_null_QSP_QString() { threadedRefAndDeref>(); } + void threadedRefAndDeref_null_SSP_QString() { threadedRefAndDeref>(); } + void threadedRefAndDeref_null_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref>()); } + + void threadedRefAndDeref_nonnull_QSP_int() { threadedRefAndDeref(QSharedPointer::create(42)); } + void threadedRefAndDeref_nonnull_SSP_int() { threadedRefAndDeref(std::make_shared(42)); } + void threadedRefAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared(42))); } + + void threadedRefAndDeref_nonnull_QSP_QString() { threadedRefAndDeref(QSharedPointer::create(QStringLiteral("Hello"))); } + void threadedRefAndDeref_nonnull_SSP_QString() { threadedRefAndDeref(std::make_shared(QStringLiteral("Hello"))); } + void threadedRefAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared(QStringLiteral("Hello")))); } + +private: + template + void threadedRefAndDeref(SP sp = {}) + { + std::atomic cancel = false; + std::vector 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"