Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize header tree using std::ref for hashes. #713

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions include/bitcoin/node/protocols/protocol_block_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class BCN_API protocol_block_in
void start() NOEXCEPT override;

protected:
/// Squash duplicates and provide constant time retrieval.
using hashmap = std::unordered_set<system::hash_digest>;

struct track
Expand Down
Loading