summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Buhr <andreas.buhr@qt.io>2020-10-26 19:33:47 +0100
committerAndreas Buhr <andreas.buhr@qt.io>2020-10-31 13:38:45 +0100
commit0d3fb3784b5de548e132f0c8e522a82adc1b9fd8 (patch)
treed9cfb5facd48a1661249562aee15989aea58634b
parent81957e753ea0f875556490857fbff50688db42f1 (diff)
Performance improvement for integer->QString conversionHEADdev
The compiler can generate better code when the base is known in integer to string conversion. This patch creates separate branches for known bases and leaves generic code as a fallback. Saved about 12ns per conversion of 12345678 in one measurement. Task-number: QTBUG-87330 Change-Id: I44c9bb467cf211f7e617ed55104476062296bba6 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r--src/corelib/text/qlocale_tools.cpp47
1 files changed, 43 insertions, 4 deletions
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp
index 80c9665c92..4421d40340 100644
--- a/src/corelib/text/qlocale_tools.cpp
+++ b/src/corelib/text/qlocale_tools.cpp
@@ -468,11 +468,50 @@ QString qulltoa(qulonglong number, int base, const QStringView zero)
char16_t buff[maxlen];
char16_t *const end = buff + maxlen, *p = end;
+ // Performance-optimized code. Compiler can generate faster code when base is known.
if (base != 10 || zero == u"0") {
- while (number != 0) {
- int c = number % base;
- *--p = c < 10 ? '0' + c : c - 10 + 'a';
- number /= base;
+ switch (base) {
+#ifndef __OPTIMIZE_SIZE__
+ case 10:
+ while (number != 0) {
+ const int c = number % 10;
+ const qulonglong temp = number / 10;
+ *--p = '0' + c;
+ number = temp;
+ }
+ break;
+ case 2:
+ while (number != 0) {
+ const int c = number % 2;
+ const qulonglong temp = number / 2;
+ *--p = '0' + c;
+ number = temp;
+ }
+ break;
+ case 8:
+ while (number != 0) {
+ const int c = number % 8;
+ const qulonglong temp = number / 8;
+ *--p = '0' + c;
+ number = temp;
+ }
+ break;
+ case 16:
+ while (number != 0) {
+ const int c = number % 16;
+ const qulonglong temp = number / 16;
+ *--p = c < 10 ? '0' + c : c - 10 + 'a';
+ number = temp;
+ }
+ break;
+#endif
+ default:
+ while (number != 0) {
+ const int c = number % base;
+ const qulonglong temp = number / base;
+ *--p = c < 10 ? '0' + c : c - 10 + 'a';
+ number = temp;
+ }
}
} else if (zero.size() && !zero.at(0).isSurrogate()) {
const char16_t zeroUcs2 = zero.at(0).unicode();