From 2ed048fa8dd506a8e57292e49dadd37011354a83 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 8 May 2020 22:18:14 +0200 Subject: [PATCH] QString: optimize insert() of a substring The old code malloc()ed a buffer to hold a copy of the data if a substring of *this was to be inserted. Instead, use a QVarLengthArray. Pick-to: 5.15 Change-Id: Ia3b4d7509bff2375ec0da5c890ecff2e9f7f335c Reviewed-by: Volker Hilsheimer --- src/corelib/text/qstring.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index aa3f49a619..f07f25b8dd 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2578,11 +2578,8 @@ QString& QString::insert(int i, const QChar *unicode, int size) const std::less less; if (!less(s, d.data()) && less(s, d.data() + d.size)) { // Part of me - take a copy - ushort *tmp = static_cast(::malloc(size * sizeof(QChar))); - Q_CHECK_PTR(tmp); - memcpy(tmp, s, size * sizeof(QChar)); - insert(i, reinterpret_cast(tmp), size); - ::free(tmp); + const QVarLengthArray copy(s, s + size); + insert(i, reinterpret_cast(copy.data()), size); return *this; }