From e31d99b10e3e68de4e3fa993ada388e035d823af Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 13 Jan 2025 15:36:11 -0500 Subject: [PATCH] Optimize header tree using std::ref for hashes. --- include/bitcoin/node/chasers/chaser_organize.hpp | 2 +- .../bitcoin/node/impl/chasers/chaser_organize.ipp | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/bitcoin/node/chasers/chaser_organize.hpp b/include/bitcoin/node/chasers/chaser_organize.hpp index 36f86493..35ff8b84 100644 --- a/include/bitcoin/node/chasers/chaser_organize.hpp +++ b/include/bitcoin/node/chasers/chaser_organize.hpp @@ -53,7 +53,7 @@ class chaser_organize typename Block::cptr block; chain_state::ptr state; }; - using block_tree = std::unordered_map; + using block_tree = std::unordered_map; using header_links = std_vector; /// Protected constructor for abstract base. diff --git a/include/bitcoin/node/impl/chasers/chaser_organize.ipp b/include/bitcoin/node/impl/chasers/chaser_organize.ipp index 1d1b9f42..6486b7bf 100644 --- a/include/bitcoin/node/impl/chasers/chaser_organize.ipp +++ b/include/bitcoin/node/impl/chasers/chaser_organize.ipp @@ -139,7 +139,7 @@ void CLASS::do_organize(typename Block::cptr block, return; } - const auto it = tree_.find(hash); + const auto it = tree_.find(system::hash_cref(hash)); if (it != tree_.end()) { handler(error_duplicate(), it->second.state->height()); @@ -448,7 +448,8 @@ TEMPLATE void CLASS::cache(const typename Block::cptr& block, const chain_state::ptr& state) NOEXCEPT { - tree_.insert({ block->hash(), { block, state } }); + tree_.emplace(system::hash_cref(block->get_hash()), + block_state{ block, state }); } TEMPLATE @@ -463,7 +464,7 @@ CLASS::chain_state::ptr CLASS::get_chain_state( return state_; // Previous block may be cached because it is not yet strong. - const auto it = tree_.find(previous_hash); + const auto it = tree_.find(system::hash_cref(previous_hash)); if (it != tree_.end()) return it->second.state; @@ -484,8 +485,8 @@ bool CLASS::get_branch_work(uint256_t& work, size_t& branch_point, work = header.proof(); // Sum all branch work from tree. - for (auto it = tree_.find(*previous); it != tree_.end(); - it = tree_.find(*previous)) + for (auto it = tree_.find(system::hash_cref(*previous)); it != tree_.end(); + it = tree_.find(system::hash_cref(*previous))) { const auto& next = get_header(*it->second.block); previous = &next.previous_block_hash(); @@ -561,7 +562,7 @@ code CLASS::push_block(const Block& block, TEMPLATE code CLASS::push_block(const system::hash_digest& key) NOEXCEPT { - const auto handle = tree_.extract(key); + const auto handle = tree_.extract(system::hash_cref(key)); if (!handle) return error::organize15;