Skip to content

Commit 922c49a

Browse files
committed
Merge bitcoin/bitcoin#23819: ConnectBlock: don't serialize block hash twice
eb8b22d block_connected: re-use previous GetTimeMicros (William Casarin) 80e1c55 block_connected: don't serialize block hash twice (William Casarin) Pull request description: 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. Shave off an extra 100 or so instructions from the validation:block_connected tracepoint by reusing a nearby GetTimeMicros(). This brings the tracepoint down to 54 instructions. Still high, but much better than the previous ~154 and 8000 instructions which it was originally. Signed-off-by: William Casarin <jb55@jb55.com> ACKs for top commit: 0xB10C: ACK eb8b22d laanwj: Code review ACK eb8b22d theStack: re-ACK eb8b22d Tree-SHA512: 92ae585e487554e0f73042a8abaa239f630502c1d198e010bd7c1de252d882bccb627bbf0e4faec09c1253e782b145bcf153f9fee78cdb8456188044a96f8267
2 parents df08250 + eb8b22d commit 922c49a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/validation.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,10 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19231923
{
19241924
AssertLockHeld(cs_main);
19251925
assert(pindex);
1926-
assert(*pindex->phashBlock == block.GetHash());
1926+
1927+
uint256 block_hash{block.GetHash()};
1928+
assert(*pindex->phashBlock == block_hash);
1929+
19271930
int64_t nTimeStart = GetTimeMicros();
19281931

19291932
// Check it again in case a previous version let a bad block in
@@ -1957,7 +1960,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19571960

19581961
// Special case for the genesis block, skipping connection of its transactions
19591962
// (its coinbase is unspendable)
1960-
if (block.GetHash() == m_params.GetConsensus().hashGenesisBlock) {
1963+
if (block_hash == m_params.GetConsensus().hashGenesisBlock) {
19611964
if (!fJustCheck)
19621965
view.SetBestBlock(pindex->GetBlockHash());
19631966
return true;
@@ -2214,12 +2217,12 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
22142217
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
22152218

22162219
TRACE6(validation, block_connected,
2217-
block.GetHash().data(),
2220+
block_hash.data(),
22182221
pindex->nHeight,
22192222
block.vtx.size(),
22202223
nInputs,
22212224
nSigOpsCost,
2222-
GetTimeMicros() - nTimeStart // in microseconds (µs)
2225+
nTime5 - nTimeStart // in microseconds (µs)
22232226
);
22242227

22252228
return true;

0 commit comments

Comments
 (0)