From 56568fdee66ee30fd6d0afdd2a60c9355aba759c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 24 May 2024 20:04:03 -0300 Subject: [PATCH] QByteArray::setNum: use assign() instead of clear()+append() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit clear() will shed any stored capacity, so that's a waste for the few people who actually use setNum(). The majority use it from number(), in which case the clear() is superfluous anyway. And append() does an extra strlen() check, which we don't need. This considerably reduces the size of both QByteArray::setNum() in a GCC LTO build (though the compiler chose to not inline assign()). Moreover, it now does inline setNum() in the number() functions and in a lot more places (searching for "qulltoa2" in the disassembly finds it in QFileSystemEngine::id, for example). Change-Id: If3345151ddf84c43a4f1fffd17d28f2fc79aa072 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qbytearray.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 09c103a0b94..c140a6aec8b 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -4209,9 +4209,7 @@ QByteArray &QByteArray::setNum(qlonglong n, int base) p = qulltoa2(buff + buffsize, qulonglong(n), base); } - clear(); - append(p, buffsize - (p - buff)); - return *this; + return assign(QByteArrayView{p, buff + buffsize}); } /*! @@ -4226,9 +4224,7 @@ QByteArray &QByteArray::setNum(qulonglong n, int base) char buff[buffsize]; char *p = qulltoa2(buff + buffsize, n, base); - clear(); - append(p, buffsize - (p - buff)); - return *this; + return assign(QByteArrayView{p, buff + buffsize}); } /*!