Skip to content

Commit

Permalink
To prevent high cpu usage update only once in MASTERNODELIST_UPDATE_S…
Browse files Browse the repository at this point in the history
…ECONDS seconds or MASTERNODELIST_FILTER_COOLDOWN_SECONDS seconds after filter was last changed. Also changing date/time format - QDateTime ToString() is way to slow for a list of thousands items, using DateTimeStrFormat instead. UI should work much smoother on mainnet now.
  • Loading branch information
UdjinM6 committed Aug 2, 2016
1 parent d2c6b2a commit 2b1c567
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/qt/masternodelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void MasternodeList::updateMyMasternodeInfo(QString alias, QString addr, QString
QTableWidgetItem *protocolItem = new QTableWidgetItem(QString::number(pmn ? pmn->protocolVersion : -1));
QTableWidgetItem *statusItem = new QTableWidgetItem(QString::fromStdString(pmn ? pmn->Status() : "MISSING"));
QTableWidgetItem *activeSecondsItem = new QTableWidgetItem(QString::fromStdString(DurationToDHMS(pmn ? (pmn->lastPing.sigTime - pmn->sigTime) : 0)));
QTableWidgetItem *lastSeenItem = new QTableWidgetItem(GUIUtil::dateTimeStr(pmn ? pmn->lastPing.sigTime : 0));
QTableWidgetItem *lastSeenItem = new QTableWidgetItem(QString::fromStdString(DateTimeStrFormat("%Y-%m-%d %H:%M", pmn ? pmn->lastPing.sigTime : 0)));
QTableWidgetItem *pubkeyItem = new QTableWidgetItem(QString::fromStdString(pmn ? CBitcoinAddress(pmn->pubkey.GetID()).ToString() : ""));

ui->tableWidgetMyMasternodes->setItem(nodeRow, 0, aliasItem);
Expand Down Expand Up @@ -224,11 +224,19 @@ void MasternodeList::updateMyNodeList(bool reset) {

void MasternodeList::updateNodeList()
{
static int64_t lastListUpdate = 0;
static int64_t nTimeListUpdate = 0;

// update only once in MASTERNODELIST_UPDATE_SECONDS seconds to prevent high cpu usage e.g. on filter change
if(GetTime() - lastListUpdate < MASTERNODELIST_UPDATE_SECONDS) return;
lastListUpdate = GetTime();
// to prevent high cpu usage update only once in MASTERNODELIST_UPDATE_SECONDS seconds
// or MASTERNODELIST_FILTER_COOLDOWN_SECONDS seconds after filter was last changed
int64_t nTimeToWait = fFilterUpdated
? nTimeFilterUpdate - GetTime() + MASTERNODELIST_FILTER_COOLDOWN_SECONDS
: nTimeListUpdate - GetTime() + MASTERNODELIST_UPDATE_SECONDS;

if(fFilterUpdated) ui->countLabel->setText(QString::fromStdString(strprintf("Please wait... %d", nTimeToWait)));
if(nTimeToWait > 0) return;

nTimeListUpdate = GetTime();
fFilterUpdated = false;

TRY_LOCK(cs_masternodes, lockMasternodes);
if(!lockMasternodes)
Expand All @@ -249,7 +257,7 @@ void MasternodeList::updateNodeList()
QTableWidgetItem *protocolItem = new QTableWidgetItem(QString::number(mn.protocolVersion));
QTableWidgetItem *statusItem = new QTableWidgetItem(QString::fromStdString(mn.Status()));
QTableWidgetItem *activeSecondsItem = new QTableWidgetItem(QString::fromStdString(DurationToDHMS(mn.lastPing.sigTime - mn.sigTime)));
QTableWidgetItem *lastSeenItem = new QTableWidgetItem(GUIUtil::dateTimeStr(mn.lastPing.sigTime));
QTableWidgetItem *lastSeenItem = new QTableWidgetItem(QString::fromStdString(DateTimeStrFormat("%Y-%m-%d %H:%M", mn.lastPing.sigTime)));
QTableWidgetItem *pubkeyItem = new QTableWidgetItem(QString::fromStdString(CBitcoinAddress(mn.pubkey.GetID()).ToString()));

if (strCurrentFilter != "")
Expand Down Expand Up @@ -279,7 +287,9 @@ void MasternodeList::updateNodeList()

void MasternodeList::on_filterLineEdit_textChanged(const QString &filterString) {
strCurrentFilter = filterString;
ui->countLabel->setText("Please wait...");
nTimeFilterUpdate = GetTime();
fFilterUpdated = true;
ui->countLabel->setText(QString::fromStdString(strprintf("Please wait... %d", MASTERNODELIST_FILTER_COOLDOWN_SECONDS)));
}

void MasternodeList::on_startButton_clicked()
Expand Down
7 changes: 5 additions & 2 deletions src/qt/masternodelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <QTimer>
#include <QWidget>

#define MASTERNODELIST_UPDATE_SECONDS 5
#define MY_MASTERNODELIST_UPDATE_SECONDS 60
#define MY_MASTERNODELIST_UPDATE_SECONDS 60
#define MASTERNODELIST_UPDATE_SECONDS 15
#define MASTERNODELIST_FILTER_COOLDOWN_SECONDS 3

namespace Ui {
class MasternodeList;
Expand Down Expand Up @@ -40,6 +41,8 @@ class MasternodeList : public QWidget

private:
QMenu *contextMenu;
int64_t nTimeFilterUpdate;
bool fFilterUpdated;

public Q_SLOTS:
void updateMyMasternodeInfo(QString alias, QString addr, QString privkey, QString txHash, QString txIndex, CMasternode *pmn);
Expand Down

0 comments on commit 2b1c567

Please sign in to comment.