From a85b4d79db5c11f5a309fbbc52e80a81096c0f01 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 11 Feb 2017 09:37:32 +0100 Subject: [PATCH] QThreadPool: replace a QSet with a QList QThreadPool maintains three containers of QThreadPoolThread*: - allThreads, a QSet - waitingThreads, a QQueue - expiredThreads, also a QQueue None of the operations on allThreads make use of QSets fast lookup. The only functions called on it are isEmpty(), count(), insert(), and swap(). Since therefore QSet adds nothing but overhead, causes indeterminism (e.g. when deleting threads in Private::reset()) and code bloat, use the same container for allThreads that underlies QQueue: QList. Port insert() to append(). Add an assert to verify that we're not running into an ABA problem here (but this should never fire, since we're never deleting threads except in Private::reset(), where we do remove them from allThreads), just in case. Saves ~0.5KiB in text size on optimized Linux AMD64 GCC 7.0 builds. Change-Id: I53a4d5ef2c204420f7c8852f1e72ab3d6ea43d08 Reviewed-by: Thiago Macieira --- src/corelib/thread/qthreadpool.cpp | 5 +++-- src/corelib/thread/qthreadpool_p.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 7ce757064f7..2ecf8f729a1 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -245,7 +245,8 @@ void QThreadPoolPrivate::startThread(QRunnable *runnable) { QScopedPointer thread(new QThreadPoolThread(this)); thread->setObjectName(QLatin1String("Thread (pooled)")); - allThreads.insert(thread.data()); + Q_ASSERT(!allThreads.contains(thread.data())); // if this assert hits, we have an ABA problem (deleted threads don't get removed here) + allThreads.append(thread.data()); ++activeThreads; if (runnable->autoDelete()) @@ -265,7 +266,7 @@ void QThreadPoolPrivate::reset() while (!allThreads.empty()) { // move the contents of the set out so that we can iterate without the lock - QSet allThreadsCopy; + QList allThreadsCopy; allThreadsCopy.swap(allThreads); locker.unlock(); diff --git a/src/corelib/thread/qthreadpool_p.h b/src/corelib/thread/qthreadpool_p.h index 5694bc8b109..ea8127efef0 100644 --- a/src/corelib/thread/qthreadpool_p.h +++ b/src/corelib/thread/qthreadpool_p.h @@ -86,7 +86,7 @@ public: void stealAndRunRunnable(QRunnable *runnable); mutable QMutex mutex; - QSet allThreads; + QList allThreads; QQueue waitingThreads; QQueue expiredThreads; QVector > queue;