From 899aef6ab37ce51f05c921631a56d149c6d2cd28 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 8 May 2020 22:18:14 +0200 Subject: 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. Change-Id: Ia3b4d7509bff2375ec0da5c890ecff2e9f7f335c Reviewed-by: Volker Hilsheimer (cherry picked from commit 2ed048fa8dd506a8e57292e49dadd37011354a83) Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index d3a84c9149..6075285c71 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2600,11 +2600,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->alloc)) { // 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; } -- cgit v1.2.3