From aa485aee2f9f1dad4072a1ed8a93c97438f9ac7e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 15 Oct 2015 03:05:26 +0200 Subject: QTextStream: optimize putString() Instead of filling a QString with the padding characters, use a QVarLengthArray. Do this only in the code path where it's actually needed, and mark that code path as unlikely. Change-Id: I11e04ccc4a07e16e430f2ea6dbb2f0f736908f5b Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- src/corelib/io/qtextstream.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/corelib/io/qtextstream.cpp') diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index ccf832e2e8..5d82453270 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -222,6 +222,8 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384; #include "qbuffer.h" #include "qfile.h" #include "qnumeric.h" +#include "qvarlengtharray.h" + #ifndef Q_OS_WINCE #include #endif @@ -896,13 +898,15 @@ inline void QTextStreamPrivate::putChar(QChar ch) */ void QTextStreamPrivate::putString(const QChar *data, int len, bool number) { - QString pad; - int padLeft = 0, padRight = 0; - - // handle padding int padSize = params.fieldWidth - len; - if (padSize > 0) { - pad = QString(padSize, params.padChar); + if (Q_UNLIKELY(padSize > 0)) { + // handle padding + static const int PreallocatedPadding = 80; // typical line length + QVarLengthArray pad(padSize); + std::fill_n(pad.begin(), padSize, params.padChar); + + int padLeft = 0, padRight = 0; + switch (params.fieldAlignment) { case QTextStream::AlignLeft: padRight = padSize; @@ -925,11 +929,12 @@ void QTextStreamPrivate::putString(const QChar *data, int len, bool number) padRight = padSize - padSize/2; break; } + write(pad.constData(), padLeft); + write(data, len); + write(pad.constData(), padRight); + } else { + write(data, len); } - - write(pad.constData(), padLeft); - write(data, len); - write(pad.constData(), padRight); } /*! -- cgit v1.2.3