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).

Pick-to: 6.5
Change-Id: I8159c38d93f3bcfc22a236c8c26541ab5ee4e6d0
Reviewed-by: Sona Kurazyan <kurazyan.sona@gmail.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-12-21 21:12:58 +01:00
parent 5dc0f52e70
commit 85b15b101c

View File

@ -17,6 +17,8 @@
#include <QtCore/qpointer.h>
#include <QtCore/qpromise.h>
#include <memory>
QT_BEGIN_NAMESPACE
//
@ -1058,7 +1060,7 @@ struct WhenAnyContext
};
template<qsizetype Index, typename ContextType, typename... Ts>
void addCompletionHandlersImpl(const QSharedPointer<ContextType> &context,
void addCompletionHandlersImpl(const std::shared_ptr<ContextType> &context,
const std::tuple<Ts...> &t)
{
auto future = std::get<Index>(t);
@ -1074,7 +1076,7 @@ void addCompletionHandlersImpl(const QSharedPointer<ContextType> &context,
}
template<typename ContextType, typename... Ts>
void addCompletionHandlers(const QSharedPointer<ContextType> &context, const std::tuple<Ts...> &t)
void addCompletionHandlers(const std::shared_ptr<ContextType> &context, const std::tuple<Ts...> &t)
{
constexpr qsizetype size = std::tuple_size<std::tuple<Ts...>>::value;
addCompletionHandlersImpl<size - 1, ContextType, Ts...>(context, t);
@ -1087,7 +1089,7 @@ QFuture<OutputSequence> whenAllImpl(InputIt first, InputIt last)
if (size == 0)
return QtFuture::makeReadyFuture(OutputSequence());
auto context = QSharedPointer<QtPrivate::WhenAllContext<OutputSequence>>::create(size);
const auto context = std::make_shared<QtPrivate::WhenAllContext<OutputSequence>>(size);
context->futures.resize(size);
context->promise.start();
@ -1106,7 +1108,7 @@ template<typename OutputSequence, typename... Futures>
QFuture<OutputSequence> whenAllImpl(Futures &&... futures)
{
constexpr qsizetype size = sizeof...(Futures);
auto context = QSharedPointer<QtPrivate::WhenAllContext<OutputSequence>>::create(size);
const auto context = std::make_shared<QtPrivate::WhenAllContext<OutputSequence>>(size);
context->futures.resize(size);
context->promise.start();
@ -1128,7 +1130,7 @@ QFuture<QtFuture::WhenAnyResult<typename Future<ValueType>::type>> whenAnyImpl(I
QtFuture::WhenAnyResult { qsizetype(-1), QFuture<PackagedType>() });
}
auto context = QSharedPointer<QtPrivate::WhenAnyContext<ResultType>>::create();
const auto context = std::make_shared<QtPrivate::WhenAnyContext<ResultType>>();
context->promise.start();
qsizetype idx = 0;
@ -1147,7 +1149,7 @@ QFuture<std::variant<std::decay_t<Futures>...>> whenAnyImpl(Futures &&... future
{
using ResultType = std::variant<std::decay_t<Futures>...>;
auto context = QSharedPointer<QtPrivate::WhenAnyContext<ResultType>>::create();
const auto context = std::make_shared<QtPrivate::WhenAnyContext<ResultType>>();
context->promise.start();
QtPrivate::addCompletionHandlers(context, std::make_tuple(std::forward<Futures>(futures)...));