Browse Source

Reduce cs_main locks during modal overlay by adding an atomic cache

0.15
Jonas Schnelli 7 years ago
parent
commit
7148f5e7d7
No known key found for this signature in database
GPG Key ID: 1EB776BB03C7922D
  1. 35
      src/qt/clientmodel.cpp
  2. 8
      src/qt/clientmodel.h

35
src/qt/clientmodel.cpp

@ -36,6 +36,8 @@ ClientModel::ClientModel(OptionsModel *_optionsModel, QObject *parent) :
banTableModel(0), banTableModel(0),
pollTimer(0) pollTimer(0)
{ {
cachedBestHeaderHeight = -1;
cachedBestHeaderTime = -1;
peerTableModel = new PeerTableModel(this); peerTableModel = new PeerTableModel(this);
banTableModel = new BanTableModel(this); banTableModel = new BanTableModel(this);
pollTimer = new QTimer(this); pollTimer = new QTimer(this);
@ -72,20 +74,28 @@ int ClientModel::getNumBlocks() const
return chainActive.Height(); return chainActive.Height();
} }
int ClientModel::getHeaderTipHeight() const int ClientModel::getHeaderTipHeight()
{ {
LOCK(cs_main); if (cachedBestHeaderHeight == -1) {
if (!pindexBestHeader) // make sure we initially populate the cache via a cs_main lock
return 0; // otherwise we need to wait for a tip update
return pindexBestHeader->nHeight; LOCK(cs_main);
if (pindexBestHeader) {
cachedBestHeaderHeight = pindexBestHeader->nHeight;
}
}
return cachedBestHeaderHeight;
} }
int64_t ClientModel::getHeaderTipTime() const int64_t ClientModel::getHeaderTipTime()
{ {
LOCK(cs_main); if (cachedBestHeaderTime == -1) {
if (!pindexBestHeader) LOCK(cs_main);
return 0; if (pindexBestHeader) {
return pindexBestHeader->GetBlockTime(); cachedBestHeaderTime = pindexBestHeader->GetBlockTime();
}
}
return cachedBestHeaderTime;
} }
quint64 ClientModel::getTotalBytesRecv() const quint64 ClientModel::getTotalBytesRecv() const
@ -283,6 +293,11 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CB
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification; int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
if (fHeader) {
// cache best headers time and height to reduce future cs_main locks
clientmodel->cachedBestHeaderHeight = pIndex->nHeight;
clientmodel->cachedBestHeaderTime = pIndex->GetBlockTime();
}
// if we are in-sync, update the UI regardless of last update time // if we are in-sync, update the UI regardless of last update time
if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) { if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
//pass a async signal to the UI thread //pass a async signal to the UI thread

8
src/qt/clientmodel.h

@ -51,8 +51,8 @@ public:
//! Return number of connections, default is in- and outbound (total) //! Return number of connections, default is in- and outbound (total)
int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const; int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
int getNumBlocks() const; int getNumBlocks() const;
int getHeaderTipHeight() const; int getHeaderTipHeight();
int64_t getHeaderTipTime() const; int64_t getHeaderTipTime();
//! Return number of transactions in the mempool //! Return number of transactions in the mempool
long getMempoolSize() const; long getMempoolSize() const;
//! Return the dynamic memory usage of the mempool //! Return the dynamic memory usage of the mempool
@ -81,6 +81,10 @@ public:
QString formatClientStartupTime() const; QString formatClientStartupTime() const;
QString dataDir() const; QString dataDir() const;
// caches for the best header
std::atomic<int> cachedBestHeaderHeight;
std::atomic<int64_t> cachedBestHeaderTime;
private: private:
OptionsModel *optionsModel; OptionsModel *optionsModel;
PeerTableModel *peerTableModel; PeerTableModel *peerTableModel;

Loading…
Cancel
Save