From e6cba05b6623d96278ac042b50eaba1c0cd77ddb Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Tue, 27 Oct 2020 10:27:26 +0100 Subject: [PATCH] QLocale: improve speed of 'applyIntegerFormatting()' This patch reduced the runtime of "QString::number(12345678)" from 119ns to 84ns in one measurement. It removes one copy of a QString into a local lambda function, it adds a std::move on return, removing another QString instantiation and it removes the usage of StringBuilder on return, which created another QString. Task-number: QTBUG-87330 Change-Id: Ia37e314353c354ae04402cd482d0f7aeabbfc0cb Reviewed-by: Andrei Golubev Reviewed-by: Edward Welbourne --- src/corelib/text/qlocale.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 9e5368bd86..55de69998b 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -3578,7 +3578,7 @@ QString QLocaleData::applyIntegerFormatting(QString &&numStr, bool negative, int const auto digitWidth = zero.size(); const auto digitCount = numStr.length() / digitWidth; - const auto basePrefix = [numStr, zero, base, flags] () -> QStringView { + const auto basePrefix = [&] () -> QStringView { if (flags & ShowBase) { const bool upper = flags & UppercaseBase; if (base == 16) @@ -3589,9 +3589,9 @@ QString QLocaleData::applyIntegerFormatting(QString &&numStr, bool negative, int return zero; } return {}; - }; + }(); - const QString prefix = signPrefix(negative, flags) + basePrefix(); + const QString prefix = signPrefix(negative, flags) + basePrefix; // Count how much of width we've used up. Each digit counts as one int usedWidth = digitCount + prefix.size(); @@ -3625,7 +3625,10 @@ QString QLocaleData::applyIntegerFormatting(QString &&numStr, bool negative, int numStr.prepend(zero); } - return prefix + (flags & CapitalEorX ? std::move(numStr).toUpper() : numStr); + QString result(flags & CapitalEorX ? std::move(numStr).toUpper() : std::move(numStr)); + if (prefix.size()) + result.prepend(prefix); + return result; } /*