diff options
author | Andreas Buhr <andreas.buhr@qt.io> | 2020-10-27 10:27:26 +0100 |
---|---|---|
committer | Andreas Buhr <andreas.buhr@qt.io> | 2020-11-08 15:53:36 +0100 |
commit | e6cba05b6623d96278ac042b50eaba1c0cd77ddb (patch) | |
tree | d5cceba0a839c4001b5a286e1be12a1d5d67d835 | |
parent | 4c5c4c6d1ce0caa2a1fc12a1a239aceca8ed28e8 (diff) |
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 <andrei.golubev@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r-- | src/corelib/text/qlocale.cpp | 11 |
1 files 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; } /* |