From a17b650347a7aaafe75cd16bbd6029eac3260d77 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 25 Oct 2010 10:13:52 +0200 Subject: QDataStream: speedup steaming of QString by avoiding an unesessary copy. The operator<< now does the same way as the operator>> Reviewed-by: Shane Kearns --- src/corelib/tools/qstring.cpp | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 30ef9de..2737574 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -7460,31 +7460,17 @@ QDataStream &operator<<(QDataStream &out, const QString &str) out << str.toLatin1(); } else { if (!str.isNull() || out.version() < 3) { - int byteOrder = out.byteOrder(); - const QChar* ub = str.unicode(); - static const uint auto_size = 1024; - char t[auto_size]; - char *b; - if (str.length()*sizeof(QChar) > auto_size) { - b = new char[str.length()*sizeof(QChar)]; + if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { + out.writeBytes(reinterpret_cast(str.unicode()), sizeof(QChar) * str.length()); } else { - b = t; - } - int l = str.length(); - char *c=b; - while (l--) { - if (byteOrder == QDataStream::BigEndian) { - *c++ = (char)ub->row(); - *c++ = (char)ub->cell(); - } else { - *c++ = (char)ub->cell(); - *c++ = (char)ub->row(); + QVarLengthArray buffer(str.length()); + const ushort *data = reinterpret_cast(str.constData()); + for (int i = 0; i < str.length(); i++) { + buffer[i] = qbswap(*data); + ++data; } - ub++; + out.writeBytes(reinterpret_cast(buffer.data()), sizeof(ushort) * buffer.size()); } - out.writeBytes(b, sizeof(QChar)*str.length()); - if (str.length()*sizeof(QChar) > auto_size) - delete [] b; } else { // write null marker out << (quint32)0xffffffff; -- cgit v0.12