From d01bd60d43a649008e31e12a2ec42a975e3896b1 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 18 Jan 2022 15:45:56 +0100 Subject: [PATCH] Optimize ContinuationWrapper used for support of move-only continuations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After QFuture continuations became non-copyable (see earlier commits), we have to always use ContinuationWrapper to save the continuations inside std::function, since it requires the callable to be copyable. Optimize the wrapper, by storing the callable directly (instead of using a ref-counted QSharedPointer) and introducing a fake copy-constructor that makes sure that it's never called. Change-Id: I0ed5f90ad62ede3b5c6d6e56ef58eb6377122920 Reviewed-by: Edward Welbourne Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit b292928e35b9864c2c3a023f79b892f40b08cd86) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/thread/qfuture_impl.h | 14 +++++++++++--- src/corelib/thread/qfutureinterface.cpp | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index 69f57652ea0..b09d84a6d39 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -538,11 +538,19 @@ bool Continuation::execute() template struct ContinuationWrapper { - ContinuationWrapper(Function &&f) : function(QSharedPointer::create(std::move(f))) { } - void operator()(const QFutureInterfaceBase &parentData) { (*function)(parentData); } + ContinuationWrapper(Function &&f) : function(std::move(f)) { } + ContinuationWrapper(const ContinuationWrapper &other) + : function(std::move(const_cast(other).function)) + { + Q_ASSERT_X(false, "QFuture", "Continuation shouldn't be copied"); + } + ContinuationWrapper(ContinuationWrapper &&other) = default; + ContinuationWrapper &operator=(ContinuationWrapper &&) = default; + + void operator()(const QFutureInterfaceBase &parentData) { function(parentData); } private: - QSharedPointer function; + Function function; }; template diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp index dfc905e8283..cad6111ab94 100644 --- a/src/corelib/thread/qfutureinterface.cpp +++ b/src/corelib/thread/qfutureinterface.cpp @@ -837,7 +837,7 @@ void QFutureInterfaceBasePrivate::setState(QFutureInterfaceBase::State newState) void QFutureInterfaceBase::setContinuation(std::function func) { - setContinuation(func, nullptr); + setContinuation(std::move(func), nullptr); } void QFutureInterfaceBase::setContinuation(std::function func,