Skip to content

Commit 412445a

Browse files
laanwjknst
authored andcommitted
Merge bitcoin-core/gui#313: qt: Optimize string concatenation by default
a02c970 qt, refactor: Revert explicit including QStringBuilder (Hennadii Stepanov) 3fd3a0f qt, build: Optimize string concatenation (Hennadii Stepanov) Pull request description: From [Qt docs](https://doc.qt.io/qt-5/qstring.html#more-efficient-string-construction): > ... multiple uses of the \[`QString`\] '+' operator usually means multiple memory allocations. When concatenating n substrings, where n > 2, there can be as many as n - 1 calls to the memory allocator. With this PR > ... the '+' will automatically be performed as the `QStringBuilder` '%' everywhere. The change in the `src/Makefile.qt.include` file does not justify submitting this PR into the main repo, IMHO. ACKs for top commit: laanwj: Code review ACK a02c970 Talkless: utACK a02c970, built successfully on Debian Sid with Qt 5.15.2, but did not check if any displayed strings are "wrong" after refactoring. jarolrod: ACK a02c970 Tree-SHA512: cbb476ee96f27c3bd6e125efab74d8bf24bbdb4c30576b3feea45e203405f3bf5b497dd7d3e11361fc825fcbf4b893b152921a9efdeaf73b42d1865d85f0ae84
1 parent 89a7c60 commit 412445a

File tree

10 files changed

+29
-18
lines changed

10 files changed

+29
-18
lines changed

src/Makefile.qt.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ RES_ANIMATION = $(wildcard $(srcdir)/qt/res/animation/spinner-*.png)
364364

365365
BITCOIN_RC = qt/res/dash-qt-res.rc
366366

367-
BITCOIN_QT_INCLUDES = -DQT_NO_KEYWORDS
367+
BITCOIN_QT_INCLUDES = -DQT_NO_KEYWORDS -DQT_USE_QSTRINGBUILDER
368368

369369
qt_libbitcoinqt_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
370370
$(QT_INCLUDES) $(QT_DBUS_INCLUDES) $(QR_CFLAGS)

src/qt/bitcoin.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include <QMessageBox>
5555
#include <QProcess>
5656
#include <QSettings>
57-
#include <QStringBuilder>
5857
#include <QThread>
5958
#include <QTimer>
6059
#include <QTranslator>
@@ -492,8 +491,8 @@ void BitcoinApplication::handleRunawayException(const QString &message)
492491
{
493492
QMessageBox::critical(
494493
nullptr, tr("Runaway exception"),
495-
tr("A fatal error occurred. %1 can no longer continue safely and will quit.").arg(PACKAGE_NAME) %
496-
QLatin1String("<br><br>") % GUIUtil::MakeHtmlLink(message, PACKAGE_BUGREPORT));
494+
tr("A fatal error occurred. %1 can no longer continue safely and will quit.").arg(PACKAGE_NAME) +
495+
QLatin1String("<br><br>") + GUIUtil::MakeHtmlLink(message, PACKAGE_BUGREPORT));
497496
::exit(EXIT_FAILURE);
498497
}
499498

@@ -503,8 +502,8 @@ void BitcoinApplication::handleNonFatalException(const QString& message)
503502
QMessageBox::warning(
504503
nullptr, tr("Internal error"),
505504
tr("An internal error occurred. %1 will attempt to continue safely. This is "
506-
"an unexpected bug which can be reported as described below.").arg(PACKAGE_NAME) %
507-
QLatin1String("<br><br>") % GUIUtil::MakeHtmlLink(message, PACKAGE_BUGREPORT));
505+
"an unexpected bug which can be reported as described below.").arg(PACKAGE_NAME) +
506+
QLatin1String("<br><br>") + GUIUtil::MakeHtmlLink(message, PACKAGE_BUGREPORT));
508507
}
509508

510509
WId BitcoinApplication::getMainWinId() const

src/qt/guiutil.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#include <QShortcut>
7070
#include <QSize>
7171
#include <QString>
72-
#include <QStringBuilder>
7372
#include <QTextDocument> // for Qt::mightBeRichText
7473
#include <QThread>
7574
#include <QTimer>
@@ -1931,7 +1930,7 @@ QString MakeHtmlLink(const QString& source, const QString& link)
19311930
{
19321931
return QString(source).replace(
19331932
link,
1934-
QLatin1String("<a href=\"") % link % QLatin1String("\">") % link % QLatin1String("</a>"));
1933+
QLatin1String("<a href=\"") + link + QLatin1String("\">") + link + QLatin1String("</a>"));
19351934
}
19361935

19371936
void PrintSlotException(

src/qt/optionsmodel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include <QDebug>
29+
#include <QLatin1Char>
2930
#include <QSettings>
3031
#include <QStringList>
3132

@@ -381,7 +382,7 @@ static ProxySetting GetProxySetting(QSettings &settings, const QString &name)
381382

382383
static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port)
383384
{
384-
settings.setValue(name, ip_port.ip + ":" + ip_port.port);
385+
settings.setValue(name, QString{ip_port.ip + QLatin1Char(':') + ip_port.port});
385386
}
386387

387388
static const QString GetDefaultProxyAddress()

src/qt/overviewpage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ void OverviewPage::updateCoinJoinProgress()
469469

470470
ui->coinJoinProgress->setValue(progress);
471471

472-
QString strToolPip = ("<b>" + tr("Overall progress") + ": %1%</b><br/>" +
472+
QString strToolPip = QString("<b>" + tr("Overall progress") + ": %1%</b><br/>" +
473473
tr("Denominated") + ": %2%<br/>" +
474474
tr("Partially mixed") + ": %3%<br/>" +
475475
tr("Mixed") + ": %4%<br/>" +

src/qt/peertablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
115115
return (qint64)rec->nodeStats.nodeid;
116116
case Address:
117117
// prepend to peer address down-arrow symbol for inbound connection and up-arrow for outbound connection
118-
return QString(rec->nodeStats.fInbound ? "" : "") + QString::fromStdString(rec->nodeStats.m_addr_name);
118+
return QString::fromStdString((rec->nodeStats.fInbound ? "" : "") + rec->nodeStats.m_addr_name);
119119
case ConnectionType:
120120
return GUIUtil::ConnectionTypeToQString(rec->nodeStats.m_conn_type, /* prepend_direction */ false);
121121
case Network:

src/qt/recentrequeststablemodel.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include <utility>
1919

20+
#include <QLatin1Char>
21+
#include <QLatin1String>
22+
2023
RecentRequestsTableModel::RecentRequestsTableModel(WalletModel *parent) :
2124
QAbstractTableModel(parent), walletModel(parent)
2225
{
@@ -126,7 +129,11 @@ void RecentRequestsTableModel::updateAmountColumnTitle()
126129
/** Gets title for amount column including current display unit if optionsModel reference available. */
127130
QString RecentRequestsTableModel::getAmountTitle()
128131
{
129-
return (this->walletModel->getOptionsModel() != nullptr) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
132+
if (!walletModel->getOptionsModel()) return {};
133+
return tr("Requested") +
134+
QLatin1String(" (") +
135+
BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) +
136+
QLatin1Char(')');
130137
}
131138

132139
QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void TestGUI(interfaces::Node& node)
201201

202202
QCOMPARE(uri.count("amount=0.00000001"), 2);
203203
QCOMPARE(receiveRequestDialog->QObject::findChild<QLabel*>("amount_tag")->text(), QString("Amount:"));
204-
QCOMPARE(receiveRequestDialog->QObject::findChild<QLabel*>("amount_content")->text(), QString("0.00000001 ") + BitcoinUnits::name(unit));
204+
QCOMPARE(receiveRequestDialog->QObject::findChild<QLabel*>("amount_content")->text(), QString::fromStdString("0.00000001 ") + BitcoinUnits::name(unit));
205205

206206
QCOMPARE(uri.count("label=TEST_LABEL_1"), 2);
207207
QCOMPARE(receiveRequestDialog->QObject::findChild<QLabel*>("label_tag")->text(), QString("Label:"));

src/qt/transactiondesc.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <stdint.h>
2727
#include <string>
2828

29+
#include <QLatin1String>
30+
2931
QString TransactionDesc::FormatTxStatus(const interfaces::WalletTx& wtx, const interfaces::WalletTxStatus& status, bool inMempool, int numBlocks)
3032
{
3133
if (!status.is_final)
@@ -44,19 +46,20 @@ QString TransactionDesc::FormatTxStatus(const interfaces::WalletTx& wtx, const i
4446
bool fChainLocked = status.is_chainlocked;
4547

4648
if (nDepth == 0) {
47-
strTxStatus = tr("0/unconfirmed, %1").arg((inMempool ? tr("in memory pool") : tr("not in memory pool"))) + (status.is_abandoned ? ", "+tr("abandoned") : "");
49+
const QString abandoned{status.is_abandoned ? QLatin1String(", ") + tr("abandoned") : QString()};
50+
strTxStatus = tr("0/unconfirmed, %1").arg((inMempool ? tr("in memory pool") : tr("not in memory pool"))) + abandoned;
4851
} else if (!fChainLocked && nDepth < 6) {
4952
strTxStatus = tr("%1/unconfirmed").arg(nDepth);
5053
} else {
5154
strTxStatus = tr("%1 confirmations").arg(nDepth);
5255
if (fChainLocked) {
53-
strTxStatus += ", " + tr("locked via ChainLocks");
56+
strTxStatus += QLatin1String(", ") + tr("locked via ChainLocks");
5457
return strTxStatus;
5558
}
5659
}
5760

5861
if (status.is_islocked) {
59-
strTxStatus += ", " + tr("verified via InstantSend");
62+
strTxStatus += QLatin1String(", ") + tr("verified via InstantSend");
6063
}
6164

6265
return strTxStatus;

src/qt/transactiontablemodel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <QDateTime>
2424
#include <QDebug>
2525
#include <QIcon>
26+
#include <QLatin1Char>
27+
#include <QLatin1String>
2628
#include <QList>
2729
#include <QMessageBox>
2830

@@ -466,9 +468,9 @@ QVariant TransactionTableModel::txAddressDecoration(const TransactionRecord *wtx
466468
QString TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx, bool tooltip) const
467469
{
468470
QString watchAddress;
469-
if (tooltip) {
471+
if (tooltip && wtx->involvesWatchAddress) {
470472
// Mark transactions involving watch-only addresses by adding " (watch-only)"
471-
watchAddress = wtx->involvesWatchAddress ? QString(" (") + tr("watch-only") + QString(")") : "";
473+
watchAddress = QLatin1String(" (") + tr("watch-only") + QLatin1Char(')');
472474
}
473475

474476
switch(wtx->type)

0 commit comments

Comments
 (0)