Skip to content

Commit 3672099

Browse files
committed
Merge bitcoin/bitcoin#20827: During IBD, prune as much as possible until we get close to where we will eventually keep blocks
d298ff8 During IBD, prune as much as possible until we get close to where we will eventually keep blocks (Luke Dashjr) Pull request description: This should reduce pruning flushes even more, speeding up IBD with pruning on systems that have a sufficient dbcache. Assumes 1 MB per block between tip and best header chain. Simply adds this to the buffer pruning is trying to leave available, which results in pruning almost everything up until we get close to where we need to be keeping blocks. ACKs for top commit: andrewtoth: ACK d298ff8 fjahr: utACK d298ff8 achow101: ACK d298ff8 Tree-SHA512: 2a482376bfb177e2ba7c2f0bb0b58b02efdb38b34755a18d1fc3e869df5959c85b6f1009e1386fa8b89c4f90d520383e36bd3e21dec221042315134efb1a455b
2 parents ac923e7 + d298ff8 commit 3672099

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/node/blockstorage.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ void BlockManager::FindFilesToPrune(
307307
// Distribute our -prune budget over all chainstates.
308308
const auto target = std::max(
309309
MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget() / chainman.GetAll().size());
310+
const uint64_t target_sync_height = chainman.m_best_header->nHeight;
310311

311312
if (chain.m_chain.Height() < 0 || target == 0) {
312313
return;
@@ -329,10 +330,13 @@ void BlockManager::FindFilesToPrune(
329330
// On a prune event, the chainstate DB is flushed.
330331
// To avoid excessive prune events negating the benefit of high dbcache
331332
// values, we should not prune too rapidly.
332-
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
333-
if (chainman.IsInitialBlockDownload()) {
334-
// Since this is only relevant during IBD, we use a fixed 10%
335-
nBuffer += target / 10;
333+
// So when pruning in IBD, increase the buffer to avoid a re-prune too soon.
334+
const auto chain_tip_height = chain.m_chain.Height();
335+
if (chainman.IsInitialBlockDownload() && target_sync_height > (uint64_t)chain_tip_height) {
336+
// Since this is only relevant during IBD, we assume blocks are at least 1 MB on average
337+
static constexpr uint64_t average_block_size = 1000000; /* 1 MB */
338+
const uint64_t remaining_blocks = target_sync_height - chain_tip_height;
339+
nBuffer += average_block_size * remaining_blocks;
336340
}
337341

338342
for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {

0 commit comments

Comments
 (0)