Context
The new accounting options introduced in #32582 can be quite heavy, and not needed when debug logging is disabled.
Problem
PartiallyDownloadedBlock::FillBlock() and PeerManagerImpl::SendBlockTransactions() accumulate transaction sizes for debug logging by calling GetTotalSize() in loops, which invokes expensive GetSerializeSize() serializations.
Fixes
Guard the size calculations with LogAcceptCategory() checks so serialization only occurs when compact block debug logging is enabled.
Also modernized the surrounding code a bit since the change is quite trivial.
Reproducer
You can test the change by starting your up-to-date bitcoind node with -debug=cmpctblock and observing the compact block lines such as:
[cmpctblock] Successfully reconstructed block 00000000000000000000b432f545625df1698f0c6e8ee9e2a04058f65baab111 with 1 txn prefilled, 1891 txn from mempool (incl at least 0 from extra pool) and 1767 txn (935772 bytes) requested
And test the negative by adding throw ""; instead of the GetTotalSize call and starting bitcoind without the -debug=cmpctblock argument to prove that it’s not called.
 0diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp
 1index 5f0217c2ba..a8a61ecf73 100644
 2--- a/src/blockencodings.cpp
 3+++ b/src/blockencodings.cpp
 4@@ -186,6 +186,7 @@ bool PartiallyDownloadedBlock::IsTxAvailable(size_t index) const
 5 
 6 ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool segwit_active)
 7 {
 8+    LogInfo("PartiallyDownloadedBlock::FillBlock called");
 9     if (header.IsNull()) return READ_STATUS_INVALID;
10 
11     uint256 hash = header.GetHash();
12@@ -201,7 +202,7 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
13             }
14             block.vtx[i] = vtx_missing[tx_missing_offset++];
15             if (LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug)) {
16-                tx_missing_size += block.vtx[i]->GetTotalSize();
17+                throw "";
18             }
19         } else {
20             block.vtx[i] = std::move(txn_available[i]);
21diff --git a/src/net_processing.cpp b/src/net_processing.cpp
22index 26a5aacaee..d879c557f1 100644
23--- a/src/net_processing.cpp
24+++ b/src/net_processing.cpp
25@@ -2470,6 +2470,7 @@ uint32_t PeerManagerImpl::GetFetchFlags(const Peer& peer) const
26 
27 void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req)
28 {
29+    LogInfo("PeerManagerImpl::SendBlockTransactions called");
30     BlockTransactions resp(req);
31     [[maybe_unused]] uint32_t tx_requested_size{0};
32     for (size_t i = 0; i < req.indexes.size(); i++) {
33@@ -2479,7 +2480,7 @@ void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlo
34         }
35         resp.txn[i] = block.vtx[req.indexes[i]];
36         if (LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug)) {
37-            tx_requested_size += resp.txn[i]->GetTotalSize();
38+            throw "";
39         }
40     }