From 236d4637b2258def1e678e515b42632943eb888d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 Sep 2019 19:30:08 +0200 Subject: Optimize QEventDispatcherWinRT::runOnMainThread() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - use std::make_shared instead of QSharedPointer - two memory allocations saved - co-locate semaphore and HRESULT object in a single State object - one more memory allocation saved - pass the shared_ptr by value into the runnable - two more memory allocations saved Not only is the new code much faster, it's also much more readable. Also use QSemaphoreReleaser, just in case the delegate should throw. Change-Id: Ib99b9da86984d440d10b72e3071aa88099e24a1f Reviewed-by: MÃ¥rten Nordheim --- src/corelib/kernel/qeventdispatcher_winrt.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qeventdispatcher_winrt.cpp b/src/corelib/kernel/qeventdispatcher_winrt.cpp index 600c6c38fd..f7a1f969a8 100644 --- a/src/corelib/kernel/qeventdispatcher_winrt.cpp +++ b/src/corelib/kernel/qeventdispatcher_winrt.cpp @@ -44,12 +44,13 @@ #include #include #include -#include #include #include #include #include +#include + #include #include #include @@ -300,19 +301,19 @@ HRESULT QEventDispatcherWinRT::runOnMainThread(const std::function &d if (QThread::currentThread() == QCoreApplication::instance()->thread()) return delegate(); - auto semaphore = QSharedPointer(new QSemaphore); - auto ptrSemaphore = new QSharedPointer(semaphore); - auto result = QSharedPointer(new HRESULT); - auto ptrResult = new QSharedPointer(result); + struct State { + QSemaphore semaphore; + HRESULT result; + }; + + const auto state = std::make_shared(); - QMetaObject::invokeMethod(QCoreApplication::instance(), [delegate, ptrSemaphore, ptrResult]() { - **ptrResult = delegate(); - delete ptrResult; - (*ptrSemaphore)->release(); - delete ptrSemaphore; + QMetaObject::invokeMethod(QCoreApplication::instance(), [delegate, state]() { + const QSemaphoreReleaser releaser{state->semaphore}; + state->result = delegate(); }, nullptr); - return semaphore->tryAcquire(1, timeout) ? *result : E_FAIL; + return state->semaphore.tryAcquire(1, timeout) ? state->result : E_FAIL; } bool QEventDispatcherWinRT::processEvents(QEventLoop::ProcessEventsFlags flags) -- cgit v1.2.3