From 8892e3d0fc8258975f27aa90bef741fb4254ce7e Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 10 Oct 2013 20:02:15 -0700 Subject: [PATCH] Improve the Latin1 conversion in QString a little First, use Qt::Uninitialized, since we're about to overwrite the memory anyway with the new Latin 1 string. Second, move the actual body of the conversion to a static void function, which seems to improve code generation a little and, of course, paves the way for the in-place conversion. Change-Id: Iaed99ba1e52facad676510aa98443223e188d70a Reviewed-by: Konstantin Ritt Reviewed-by: Olivier Goffart --- src/corelib/tools/qstring.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index a94b18b5d3c..1b07e31c89f 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -3946,13 +3946,9 @@ static inline __m128i mergeQuestionMarks(__m128i chunk) } #endif -static QByteArray toLatin1_helper(const QChar *data, int length) +static void toLatin1_helper(uchar *dst, const ushort *src, int length) { - QByteArray ba; if (length) { - ba.resize(length); - const ushort *src = reinterpret_cast(data); - uchar *dst = (uchar*) ba.data(); #if defined(__SSE2__) if (length >= 16) { const int chunkCount = length >> 4; // divided by 16 @@ -4003,7 +3999,6 @@ static QByteArray toLatin1_helper(const QChar *data, int length) ++src; } } - return ba; } QByteArray QString::toLatin1_helper(const QString &string) @@ -4016,7 +4011,13 @@ QByteArray QString::toLatin1_helper(const QString &string) QByteArray QString::toLatin1_helper(const QChar *data, int length) { - return QT_PREPEND_NAMESPACE(toLatin1_helper)(data, length); + QByteArray ba(length, Qt::Uninitialized); + + // since we own the only copy, we're going to const_cast the constData; + // that avoids an unnecessary call to detach() and expansion code that will never get used + QT_PREPEND_NAMESPACE(toLatin1_helper)(reinterpret_cast(const_cast(ba.constData())), + reinterpret_cast(data), length); + return ba; } /*!