From 076297eeeee7d83fe65b9eb7f883c7effc5dbf6f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 21 Dec 2022 21:12:58 +0100 Subject: [PATCH] QFuture: port from QSharedPointer to std::shared_ptr Compared to std::shared_ptr, QSharedPointer requires 2x the atomic operations per copy, and this code uses _a lot_ of copies. Port to std::shared_ptr. The uses are all in inline, non-exported code, so there's no BC or SC issue here. Old code will happily continue to use its QSharedPointer-based code, while recompiled code will enjoy the transparent efficiency gain. This also helps prepare QtCore for an eventual QT_NO_SHARED_POINTER (which QtCore will not be able to set on itself, because QPointer is still not ported away from QWeakPointer, but which should affect as few headers as possible). Change-Id: I8159c38d93f3bcfc22a236c8c26541ab5ee4e6d0 Reviewed-by: Sona Kurazyan Reviewed-by: Qt CI Bot (cherry picked from commit 85b15b101c70b962fd0030c26159ef7b1b50a1f4) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/thread/qfuture_impl.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index 6bf3a7406a5..9a60348e3de 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -17,6 +17,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE // @@ -1058,7 +1060,7 @@ struct WhenAnyContext }; template -void addCompletionHandlersImpl(const QSharedPointer &context, +void addCompletionHandlersImpl(const std::shared_ptr &context, const std::tuple &t) { auto future = std::get(t); @@ -1074,7 +1076,7 @@ void addCompletionHandlersImpl(const QSharedPointer &context, } template -void addCompletionHandlers(const QSharedPointer &context, const std::tuple &t) +void addCompletionHandlers(const std::shared_ptr &context, const std::tuple &t) { constexpr qsizetype size = std::tuple_size>::value; addCompletionHandlersImpl(context, t); @@ -1087,7 +1089,7 @@ QFuture whenAllImpl(InputIt first, InputIt last) if (size == 0) return QtFuture::makeReadyFuture(OutputSequence()); - auto context = QSharedPointer>::create(size); + const auto context = std::make_shared>(size); context->futures.resize(size); context->promise.start(); @@ -1106,7 +1108,7 @@ template QFuture whenAllImpl(Futures &&... futures) { constexpr qsizetype size = sizeof...(Futures); - auto context = QSharedPointer>::create(size); + const auto context = std::make_shared>(size); context->futures.resize(size); context->promise.start(); @@ -1128,7 +1130,7 @@ QFuture::type>> whenAnyImpl(I QtFuture::WhenAnyResult { qsizetype(-1), QFuture() }); } - auto context = QSharedPointer>::create(); + const auto context = std::make_shared>(); context->promise.start(); qsizetype idx = 0; @@ -1147,7 +1149,7 @@ QFuture...>> whenAnyImpl(Futures &&... future { using ResultType = std::variant...>; - auto context = QSharedPointer>::create(); + const auto context = std::make_shared>(); context->promise.start(); QtPrivate::addCompletionHandlers(context, std::make_tuple(std::forward(futures)...));