From 5fcee880975c0ee9e373b097209882fbd30add0b Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Jan 2014 18:01:11 -0800 Subject: [PATCH] Abuse const_cast in QStringBuilder to get a little more performance Without this const_cast, the call to QString::data() or QByteArray::data() will cause a call to detach(), which is totally unnecessary since we've just allocated data a couple of lines before. Since we know we're the owner of the only reference, we can skip the detach attempt. Change-Id: If40f66100f85cc9b405025f21c562828ead23475 Reviewed-by: hjk Reviewed-by: Olivier Goffart --- src/corelib/tools/qstringbuilder.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index e09b02b5a9..489357f5fd 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -112,7 +112,9 @@ private: const uint len = QConcatenable< QStringBuilder >::size(*this); T s(len, Qt::Uninitialized); - typename T::iterator d = s.data(); + // we abuse const_cast / constData here because we know we've just + // allocated the data and we're the only reference count + typename T::iterator d = const_cast(s.constData()); typename T::const_iterator const start = d; QConcatenable< QStringBuilder >::appendTo(*this, d);