From fd31177c45fa47bab867503d8b1b1505ee81654e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 12 May 2022 07:30:23 +0200 Subject: [PATCH] QObject: Turn flaggedSignatures into a thread_local static ... out of QThreadData. No-one except two functions in qobject.cpp uses the object, and its creation is constinit, so there's no advantage to expose it to the world as a QThreadData member. Remove it from QThreadData, move the class' definition to the unnamed namespace in qobject.cpp, ensure constinit'ability by letting the language zero out the members (as opposed to an STL algorithm call), declare it constinit thread_local static, and adapt the two users (basically, removing the retrieval of QThreadData::current()). Almost no effect on Clang, but saves ~400 bytes on optimized GCC 11.2 Linux AMD64 C++20 builds. Change-Id: I22432d4ec5eb4ab59920656409b21768983fb4db Reviewed-by: Thiago Macieira --- src/corelib/kernel/qobject.cpp | 26 ++++++++++++++++++++++---- src/corelib/thread/qthread_p.h | 21 --------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index ee168dfc79..c72b8a761c 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2602,11 +2602,29 @@ void QObject::deleteLater() Signals and slots *****************************************************************************/ +namespace { +// This class provides (per-thread) storage for qFlagLocation() +class FlaggedDebugSignatures +{ + static constexpr uint Count = 2; + + uint idx = 0; + const char *locations[Count] = {}; + +public: + void store(const char* method) + { locations[idx++ % Count] = method; } + + bool contains(const char *method) const + { return std::find(locations, locations + Count, method) != locations + Count; } +}; + +Q_THREAD_LOCAL_CONSTINIT static thread_local FlaggedDebugSignatures flaggedSignatures = {}; +} // unnamed namespace + const char *qFlagLocation(const char *method) { - QThreadData *currentThreadData = QThreadData::current(false); - if (currentThreadData != nullptr) - currentThreadData->flaggedSignatures.store(method); + flaggedSignatures.store(method); return method; } @@ -2618,7 +2636,7 @@ static int extract_code(const char *member) static const char *extract_location(const char *member) { - if (QThreadData::current()->flaggedSignatures.contains(member)) { + if (flaggedSignatures.contains(member)) { // signature includes location information after the first null-terminator const char *location = member + qstrlen(member) + 1; if (*location != '\0') diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index 134c1577b2..6fdf785633 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -261,26 +261,6 @@ public: return canWait; } - // This class provides per-thread (by way of being a QThreadData - // member) storage for qFlagLocation() - class FlaggedDebugSignatures - { - static const uint Count = 2; - - uint idx; - const char *locations[Count]; - - public: - FlaggedDebugSignatures() : idx(0) - { std::fill_n(locations, Count, static_cast(nullptr)); } - - void store(const char* method) - { locations[idx++ % Count] = method; } - - bool contains(const char *method) const - { return std::find(locations, locations + Count, method) != locations + Count; } - }; - private: QAtomicInt _ref; @@ -294,7 +274,6 @@ public: QAtomicPointer threadId; QAtomicPointer eventDispatcher; QList tls; - FlaggedDebugSignatures flaggedSignatures; bool quitNow; bool canWait;