diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2019-03-26 12:15:08 +0000 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2019-03-28 18:07:57 +0000 |
commit | ae6885f6b4d426d17d55b8415074eead04450264 (patch) | |
tree | 743ed101721b12e79b9589e7405e4fc9f245a770 /src/cryptonote_core | |
parent | blockchain: simple cache for the long term block weights (diff) | |
download | monero-ae6885f6b4d426d17d55b8415074eead04450264.tar.xz |
blockchain: incremental long term block weight cache
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 8346d5d00..0813ef698 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1280,6 +1280,8 @@ void Blockchain::get_long_term_block_weights(std::vector<uint64_t>& weights, uin LOG_PRINT_L3("Blockchain::" << __func__); CRITICAL_REGION_LOCAL(m_blockchain_lock); + PERF_TIMER(get_long_term_block_weights); + if (count == 0) return; @@ -1295,10 +1297,30 @@ void Blockchain::get_long_term_block_weights(std::vector<uint64_t>& weights, uin if (cached) { + MTRACE("requesting " << count << " from " << start_height << ", cached"); weights = m_long_term_block_weights_cache; return; } + // in the vast majority of uncached cases, most is still cached, + // as we just move the window one block up: + if (tip_height > 0 && count == m_long_term_block_weights_cache.size() && tip_height < blockchain_height) + { + crypto::hash old_tip_hash = m_db->get_block_hash_from_height(tip_height - 1); + if (old_tip_hash == m_long_term_block_weights_cache_tip_hash) + { + weights = m_long_term_block_weights_cache; + for (size_t i = 1; i < weights.size(); ++i) + weights[i - 1] = weights[i]; + MTRACE("requesting " << count << " from " << start_height << ", incremental"); + weights.back() = m_db->get_block_long_term_weight(tip_height); + m_long_term_block_weights_cache = weights; + m_long_term_block_weights_cache_tip_hash = tip_hash; + return; + } + } + + MTRACE("requesting " << count << " from " << start_height << ", uncached"); weights = m_db->get_long_term_block_weights(start_height, count); m_long_term_block_weights_cache = weights; m_long_term_block_weights_cache_tip_hash = tip_hash; |