Skip to content

Commit 80e1c55

Browse files
committed
block_connected: don't serialize block hash twice
In the validation:block_connected tracepoint, we call block->GetHash(), which ends up calling CBlockHeader::GetHash(), executing around 8000 serialization instructions. We don't need to do this extra work, because block->GetHash() is already called further up in the function. Let's save that value as a local variable and re-use it in our tracepoint so there is no unnecessary tracepoint overhead. Signed-off-by: William Casarin <[email protected]>
1 parent d05be15 commit 80e1c55

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/validation.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,10 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
18661866
{
18671867
AssertLockHeld(cs_main);
18681868
assert(pindex);
1869-
assert(*pindex->phashBlock == block.GetHash());
1869+
1870+
uint256 block_hash{block.GetHash()};
1871+
assert(*pindex->phashBlock == block_hash);
1872+
18701873
int64_t nTimeStart = GetTimeMicros();
18711874

18721875
// Check it again in case a previous version let a bad block in
@@ -1900,7 +1903,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19001903

19011904
// Special case for the genesis block, skipping connection of its transactions
19021905
// (its coinbase is unspendable)
1903-
if (block.GetHash() == m_params.GetConsensus().hashGenesisBlock) {
1906+
if (block_hash == m_params.GetConsensus().hashGenesisBlock) {
19041907
if (!fJustCheck)
19051908
view.SetBestBlock(pindex->GetBlockHash());
19061909
return true;
@@ -2158,7 +2161,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21582161
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
21592162

21602163
TRACE6(validation, block_connected,
2161-
block.GetHash().data(),
2164+
block_hash.data(),
21622165
pindex->nHeight,
21632166
block.vtx.size(),
21642167
nInputs,

0 commit comments

Comments
 (0)