Skip to content

Commit f5440a3

Browse files
authored
Merge pull request #713 from evoskuil/master
Optimize header tree using std::ref for hashes.
2 parents 562d467 + e31d99b commit f5440a3

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

include/bitcoin/node/chasers/chaser_organize.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class chaser_organize
5353
typename Block::cptr block;
5454
chain_state::ptr state;
5555
};
56-
using block_tree = std::unordered_map<system::hash_digest, block_state>;
56+
using block_tree = std::unordered_map<system::hash_cref, block_state>;
5757
using header_links = std_vector<database::header_link>;
5858

5959
/// Protected constructor for abstract base.

include/bitcoin/node/impl/chasers/chaser_organize.ipp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void CLASS::do_organize(typename Block::cptr block,
139139
return;
140140
}
141141

142-
const auto it = tree_.find(hash);
142+
const auto it = tree_.find(system::hash_cref(hash));
143143
if (it != tree_.end())
144144
{
145145
handler(error_duplicate(), it->second.state->height());
@@ -448,7 +448,8 @@ TEMPLATE
448448
void CLASS::cache(const typename Block::cptr& block,
449449
const chain_state::ptr& state) NOEXCEPT
450450
{
451-
tree_.insert({ block->hash(), { block, state } });
451+
tree_.emplace(system::hash_cref(block->get_hash()),
452+
block_state{ block, state });
452453
}
453454

454455
TEMPLATE
@@ -463,7 +464,7 @@ CLASS::chain_state::ptr CLASS::get_chain_state(
463464
return state_;
464465

465466
// Previous block may be cached because it is not yet strong.
466-
const auto it = tree_.find(previous_hash);
467+
const auto it = tree_.find(system::hash_cref(previous_hash));
467468
if (it != tree_.end())
468469
return it->second.state;
469470

@@ -484,8 +485,8 @@ bool CLASS::get_branch_work(uint256_t& work, size_t& branch_point,
484485
work = header.proof();
485486

486487
// Sum all branch work from tree.
487-
for (auto it = tree_.find(*previous); it != tree_.end();
488-
it = tree_.find(*previous))
488+
for (auto it = tree_.find(system::hash_cref(*previous)); it != tree_.end();
489+
it = tree_.find(system::hash_cref(*previous)))
489490
{
490491
const auto& next = get_header(*it->second.block);
491492
previous = &next.previous_block_hash();
@@ -561,7 +562,7 @@ code CLASS::push_block(const Block& block,
561562
TEMPLATE
562563
code CLASS::push_block(const system::hash_digest& key) NOEXCEPT
563564
{
564-
const auto handle = tree_.extract(key);
565+
const auto handle = tree_.extract(system::hash_cref(key));
565566
if (!handle)
566567
return error::organize15;
567568

include/bitcoin/node/protocols/protocol_block_in.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class BCN_API protocol_block_in
5151
void start() NOEXCEPT override;
5252

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

5657
struct track

0 commit comments

Comments
 (0)