From 853f7b661f1222c8d2c5f2ad65d9dd61b6afbbbd Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 18 Jul 2024 21:35:54 +0200 Subject: [PATCH] QDebug: make toString() SCARY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract the non-template-dependent code into an out-of-line function and pass the actual streaming operation as a callback. Saves 4% (174811→167936) in executable size for tst_qdebug and 2.7% (93639→91122) for tst_tostring on Linux GCC 9 AMD64 release builds. Change-Id: If232e5b26c66981ffcb614f1bdb7007c71e879bf Reviewed-by: Thiago Macieira (cherry picked from commit 5cdd1f594d26e1d4f84b00741be1ab7231458512) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qdebug.cpp | 11 +++++++++++ src/corelib/io/qdebug.h | 14 ++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 70832c3dbcb..600b8b13b80 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -1002,6 +1002,17 @@ QDebug &QDebug::resetFormat() \include qdebug-toString.qdocinc */ +/*! \internal */ +QString QDebug::toStringImpl(StreamTypeErased s, const void *obj) +{ + QString result; + { + QDebug d(&result); + s(d.nospace(), obj); + } + return result; +} + /*! \fn template QDebug operator<<(QDebug debug, const QList &list) \relates QDebug diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 35ce50a0f15..e554e92c60f 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -223,13 +223,19 @@ public: QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); } #endif // QT_SUPPORTS_INT128 +private: + template + static void streamTypeErased(QDebug &d, const void *obj) + { + d << *static_cast(obj); + } + using StreamTypeErased = void(*)(QDebug&, const void*); + QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj); +public: template static QString toString(const T &object) { - QString buffer; - QDebug stream(&buffer); - stream.nospace() << object; - return buffer; + return toStringImpl(&streamTypeErased, &object); } };