Skip to content

Commit 987a6c0

Browse files
committed
Merge #10231: [Qt] Reduce a significant cs_main lock freeze
4082fb0 Add missing <atomic> header in clientmodel.h (Jonas Schnelli) 928d4a9 Set both time/height header caches at the same time (Jonas Schnelli) 610a917 Declare headers height/time cache mutable, re-set the methods const (Jonas Schnelli) cf92bce Update the remaining blocks left in modaloverlay at init. (Jonas Schnelli) 7148f5e Reduce cs_main locks during modal overlay by adding an atomic cache (Jonas Schnelli) Tree-SHA512: a92ca22f90b8b2a5e8eb94fdce531ef44542e21a8dbbb0693f7723d7018592cb68de687a2a0aac91d31cbf019793f8e922550656d2b130ed3d854d60630341db
2 parents a987def + 4082fb0 commit 987a6c0

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
478478
connect(_clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
479479
connect(_clientModel, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool)));
480480

481+
modalOverlay->setKnownBestHeight(_clientModel->getHeaderTipHeight(), QDateTime::fromTime_t(_clientModel->getHeaderTipTime()));
481482
setNumBlocks(_clientModel->getNumBlocks(), _clientModel->getLastBlockDate(), _clientModel->getVerificationProgress(NULL), false);
482483
connect(_clientModel, SIGNAL(numBlocksChanged(int,QDateTime,double,bool)), this, SLOT(setNumBlocks(int,QDateTime,double,bool)));
483484

@@ -505,8 +506,6 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
505506
// initialize the disable state of the tray icon with the current value in the model.
506507
setTrayIconVisible(optionsModel->getHideTrayIcon());
507508
}
508-
509-
modalOverlay->setKnownBestHeight(clientModel->getHeaderTipHeight(), QDateTime::fromTime_t(clientModel->getHeaderTipTime()));
510509
} else {
511510
// Disable possibility to show main window via action
512511
toggleHideAction->setEnabled(false);

src/qt/clientmodel.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ ClientModel::ClientModel(OptionsModel *_optionsModel, QObject *parent) :
3636
banTableModel(0),
3737
pollTimer(0)
3838
{
39+
cachedBestHeaderHeight = -1;
40+
cachedBestHeaderTime = -1;
3941
peerTableModel = new PeerTableModel(this);
4042
banTableModel = new BanTableModel(this);
4143
pollTimer = new QTimer(this);
@@ -74,18 +76,28 @@ int ClientModel::getNumBlocks() const
7476

7577
int ClientModel::getHeaderTipHeight() const
7678
{
77-
LOCK(cs_main);
78-
if (!pindexBestHeader)
79-
return 0;
80-
return pindexBestHeader->nHeight;
79+
if (cachedBestHeaderHeight == -1) {
80+
// make sure we initially populate the cache via a cs_main lock
81+
// otherwise we need to wait for a tip update
82+
LOCK(cs_main);
83+
if (pindexBestHeader) {
84+
cachedBestHeaderHeight = pindexBestHeader->nHeight;
85+
cachedBestHeaderTime = pindexBestHeader->GetBlockTime();
86+
}
87+
}
88+
return cachedBestHeaderHeight;
8189
}
8290

8391
int64_t ClientModel::getHeaderTipTime() const
8492
{
85-
LOCK(cs_main);
86-
if (!pindexBestHeader)
87-
return 0;
88-
return pindexBestHeader->GetBlockTime();
93+
if (cachedBestHeaderTime == -1) {
94+
LOCK(cs_main);
95+
if (pindexBestHeader) {
96+
cachedBestHeaderHeight = pindexBestHeader->nHeight;
97+
cachedBestHeaderTime = pindexBestHeader->GetBlockTime();
98+
}
99+
}
100+
return cachedBestHeaderTime;
89101
}
90102

91103
quint64 ClientModel::getTotalBytesRecv() const
@@ -283,6 +295,11 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CB
283295

284296
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
285297

298+
if (fHeader) {
299+
// cache best headers time and height to reduce future cs_main locks
300+
clientmodel->cachedBestHeaderHeight = pIndex->nHeight;
301+
clientmodel->cachedBestHeaderTime = pIndex->GetBlockTime();
302+
}
286303
// if we are in-sync, update the UI regardless of last update time
287304
if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
288305
//pass a async signal to the UI thread

src/qt/clientmodel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <QObject>
99
#include <QDateTime>
1010

11+
#include <atomic>
12+
1113
class AddressTableModel;
1214
class BanTableModel;
1315
class OptionsModel;
@@ -81,6 +83,10 @@ class ClientModel : public QObject
8183
QString formatClientStartupTime() const;
8284
QString dataDir() const;
8385

86+
// caches for the best header
87+
mutable std::atomic<int> cachedBestHeaderHeight;
88+
mutable std::atomic<int64_t> cachedBestHeaderTime;
89+
8490
private:
8591
OptionsModel *optionsModel;
8692
PeerTableModel *peerTableModel;

0 commit comments

Comments
 (0)