@@ -36,6 +36,8 @@ ClientModel::ClientModel(OptionsModel *_optionsModel, QObject *parent) :
36
36
banTableModel(0 ),
37
37
pollTimer(0 )
38
38
{
39
+ cachedBestHeaderHeight = -1 ;
40
+ cachedBestHeaderTime = -1 ;
39
41
peerTableModel = new PeerTableModel (this );
40
42
banTableModel = new BanTableModel (this );
41
43
pollTimer = new QTimer (this );
@@ -72,20 +74,28 @@ int ClientModel::getNumBlocks() const
72
74
return chainActive.Height ();
73
75
}
74
76
75
- int ClientModel::getHeaderTipHeight () const
77
+ int ClientModel::getHeaderTipHeight ()
76
78
{
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
+ }
86
+ }
87
+ return cachedBestHeaderHeight;
81
88
}
82
89
83
- int64_t ClientModel::getHeaderTipTime () const
90
+ int64_t ClientModel::getHeaderTipTime ()
84
91
{
85
- LOCK (cs_main);
86
- if (!pindexBestHeader)
87
- return 0 ;
88
- return pindexBestHeader->GetBlockTime ();
92
+ if (cachedBestHeaderTime == -1 ) {
93
+ LOCK (cs_main);
94
+ if (pindexBestHeader) {
95
+ cachedBestHeaderTime = pindexBestHeader->GetBlockTime ();
96
+ }
97
+ }
98
+ return cachedBestHeaderTime;
89
99
}
90
100
91
101
quint64 ClientModel::getTotalBytesRecv () const
@@ -283,6 +293,11 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CB
283
293
284
294
int64_t & nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
285
295
296
+ if (fHeader ) {
297
+ // cache best headers time and height to reduce future cs_main locks
298
+ clientmodel->cachedBestHeaderHeight = pIndex->nHeight ;
299
+ clientmodel->cachedBestHeaderTime = pIndex->GetBlockTime ();
300
+ }
286
301
// if we are in-sync, update the UI regardless of last update time
287
302
if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
288
303
// pass a async signal to the UI thread
0 commit comments