From 03356fd17a5611911e620dbb65f8bb578fdffafb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 30 Nov 2015 12:31:11 +0100 Subject: [PATCH] QMessageBox: optimize textToCopy string construction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Keep 'separator' a QLatin1String. - saves at least two memory allocations - necessitates carrying the \n previously prepended to it around explicitly 2. Start adding to 'textToCopy' with op+= - saves one allocation, costs one -> ±0 - preallocates more capacity than if we started with assignment 3. Collapse three unconditional op+= into one - more efficient usage of QStringBuilder 4. Don't collect button texts in a separate variable, but append to 'textToCopy' directly. - saves at least one memory allocation, probably more since the growth increments of 'textToCopy' should be larger (due to more content) than those of a new variable. Also replace index-based iteration over the buttons with C++11 range-for over a const QList. Avoids the detach that happened previously, due to use of op[] instead of at(), but frankly, I was just too lazy to separate this change. Change-Id: I27a46a6a163c16d773124f140e085325b17ce5d1 Reviewed-by: Friedemann Kleint --- src/widgets/dialogs/qmessagebox.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index 4ad235051c..76a9de0f1c 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1478,24 +1478,21 @@ void QMessageBox::keyPressEvent(QKeyEvent *e) #if defined(Q_OS_WIN) if (e == QKeySequence::Copy) { - QString separator = QString::fromLatin1("---------------------------\n"); - QString textToCopy = separator; - separator.prepend(QLatin1Char('\n')); - textToCopy += windowTitle() + separator; // title - textToCopy += d->label->text() + separator; // text + const QLatin1String separator("---------------------------\n"); + QString textToCopy; + textToCopy += separator + windowTitle() + QLatin1Char('\n') + separator // title + + d->label->text() + QLatin1Char('\n') + separator; // text if (d->informativeLabel) - textToCopy += d->informativeLabel->text() + separator; + textToCopy += d->informativeLabel->text() + QLatin1Char('\n') + separator; - QString buttonTexts; - QList buttons = d->buttonBox->buttons(); - for (int i = 0; i < buttons.count(); i++) { - buttonTexts += buttons[i]->text() + QLatin1String(" "); - } - textToCopy += buttonTexts + separator; + const QList buttons = d->buttonBox->buttons(); + for (const auto *button : buttons) + textToCopy += button->text() + QLatin1String(" "); + textToCopy += QLatin1Char('\n') + separator; #ifndef QT_NO_TEXTEDIT if (d->detailsText) - textToCopy += d->detailsText->text() + separator; + textToCopy += d->detailsText->text() + QLatin1Char('\n') + separator; #endif QApplication::clipboard()->setText(textToCopy); return;