From 9579be5c7da32479d6cd69b416908726402a3761 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Thu, 12 May 2022 22:18:08 +0200 Subject: [PATCH] Optimized SideChain::miner_count() --- src/side_chain.cpp | 18 +++++++++++------- src/side_chain.h | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 3cfbf53..4879aa4 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -61,6 +61,7 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name) : m_pool(pool) , m_networkType(type) , m_chainTip{ nullptr } + , m_seenWalletsLastPruneTime(0) , m_poolName(pool_name ? pool_name : "default") , m_targetBlockTime(10) , m_minDifficulty(MIN_DIFFICULTY, 0) @@ -846,14 +847,17 @@ uint64_t SideChain::miner_count() MutexLock lock(m_seenWalletsLock); - // Delete wallets that weren't seen for more than 72 hours and return how many remain - for (auto it = m_seenWallets.begin(); it != m_seenWallets.end();) { - if (it->second + 72 * 60 * 60 <= cur_time) { - it = m_seenWallets.erase(it); - } - else { - ++it; + // Every 5 minutes, delete wallets that weren't seen for more than 72 hours + if (m_seenWalletsLastPruneTime + 5 * 60 <= cur_time) { + for (auto it = m_seenWallets.begin(); it != m_seenWallets.end();) { + if (it->second + 72 * 60 * 60 < cur_time) { + it = m_seenWallets.erase(it); + } + else { + ++it; + } } + m_seenWalletsLastPruneTime = cur_time; } return m_seenWallets.size(); diff --git a/src/side_chain.h b/src/side_chain.h index c4a17c2..8907ff1 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -105,6 +105,7 @@ private: uv_mutex_t m_seenWalletsLock; unordered_map m_seenWallets; + uint64_t m_seenWalletsLastPruneTime; uv_mutex_t m_seenBlocksLock; unordered_set m_seenBlocks;