Skip to content

Commit

Permalink
Optimize header tree using std::ref for hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jan 13, 2025
1 parent 9864f6e commit e31d99b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_organize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class chaser_organize
typename Block::cptr block;
chain_state::ptr state;
};
using block_tree = std::unordered_map<system::hash_digest, block_state>;
using block_tree = std::unordered_map<system::hash_cref, block_state>;
using header_links = std_vector<database::header_link>;

/// Protected constructor for abstract base.
Expand Down
13 changes: 7 additions & 6 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit e31d99b

Please sign in to comment.