Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible places where comparison-specific test macros should replace generic comparisons:
2026-12-03 10:53:12 |
6b53396 to
5786017
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
5786017 to
b96e669
Compare
b96e669 to
084b356
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
37cd620 to
c95469a
Compare
419fbc6 to
8d5b787
Compare
8d5b787 to
3d28add
Compare
jeanpablojp
left a comment
There was a problem hiding this comment.
Concept ACK
The speedup holds here too. PruneBlockFiles takes 0.65s on my i9 laptop for a first prune, and 0.09s, one pass over the index, when the entries are already pruned.
nit: the commit message still says out-of-range file numbers are rejected with vector::at(). Maybe the message could name the explicit range check instead.
7ae31a6 to
38d6447
Compare
l0rinc
left a comment
There was a problem hiding this comment.
Concept ACK
I also had this in my backlog, solved it slightly differently, I did a quick pass over the diff.
I’d split the batching from the change to which entries get marked dirty so each is easier to review. Ideally commits are telling a story, leading the reviewers through the changes. Besides splitting the commits, also check if you can simplify the added tests. As usual, please make sure you understand every line you're submitting so that reviewers not the first humans that will try to understand it fully. Human review is the bottleneck so please make sure it's easy to review.
The description is also quite verbose, a few details to fix in the description, quick AI summary of the suggestions:
The dirty-write savings apply when file 0 is selected. After file 0 has been pruned, both selectors skip its zero-sized record, so batching explains the savings after downtime. The mixed valid and out-of-range case calls
PruneBlockFilesdirectly. The manual test covers file selection throughFlushStateToDisk. The single index pass is the main improvement, and the bitmap size depends on the highest file number even when only a few files are selected.
38d6447 to
0ef8cc3
Compare
|
Thanks for the review! I've added all proposed patches, split the PR into 3 commits and condensed the PR description.
I am aware of this, I'm not submitting PRs lightly. I do understand the code changes, but I'm not familiar in the codebase. This was not a random AI find, I was thoroughly confused by the long pause at startup caused by this bug, and wanted to prevent other people from running into it as well. I will take your remarks into account for the future. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
l0rinc
left a comment
There was a problem hiding this comment.
The split makes the batching and file-zero changes a bit easier to follow, but the implementation is still very verbose, please simplify it more - left some hints.
0ef8cc3 to
56427c7
Compare
…uning Apply BlockManager arguments via ApplyArgsManOptions in ChainTestingSetup so unit tests can run in prune mode. Add blockmanager_find_files_to_prune_manual to cover manual pruning through FlushStateToDisk, closing file 2 and placing the prune cutoff at file 1's last block so both sides of the height cutoff are tested.
When starting the node with pruning enabled (e.g. -prune=550) on a node with full chain history or pruning many files at once, FindFilesToPrune and FindFilesToPruneManual previously called PruneOneBlockFile once per file. Because PruneOneBlockFile iterated over the entire m_block_index for each file, this was an O(M * N) operation (M = files to prune, N = total blocks). When pruning a full chain (~5,770 files and ~972k blocks), this triggered ~5.6 billion iterations and froze the node for ~10 minutes on startup while holding cs_main. Replace PruneOneBlockFile with PruneBlockFiles(const std::set<int>&), which collects all candidate files and prunes them in a single pass over m_block_index using a compact bitmap (std::vector<bool> sized to MaxBlockfileNum() + 1). Validate the sorted set's extremes against [0, MaxBlockfileNum()] up front and throw std::out_of_range so out-of-range file numbers fail before any block index entry is modified. Add unit test blockmanager_prune_unlinked_siblings covering batch pruning with a gap, m_blocks_unlinked sibling retention, and out-of-range rejection, and update existing caller tests in blockmanager_tests and rescan_tests.
CBlockIndex::nFile is only meaningful when BLOCK_HAVE_DATA or BLOCK_HAVE_UNDO is set. Header-only and already-pruned entries also have nFile == 0, so pruning file 0 previously matched them and marked them dirty in m_dirty_blockindex. Skip entries without BLOCK_HAVE_MASK in PruneBlockFiles to avoid unnecessary dirty writes when file 0 is pruned and to avoid bitmap lookups on already-pruned entries. Extend blockmanager_prune_unlinked_siblings to verify that pruning file 0 dirties the genesis entry while leaving the header-only parent and already-pruned sibling clean, using a private test-access peer to inspect m_dirty_blockindex.
56427c7 to
e7346a9
Compare
In a profiled mainnet IBD with -prune=2048, 7.4% of the validation thread's on-CPU samples between BRR and v19 (47-55% in windows with a prune event) were in PruneOneBlockFile under FindFilesToPrune: every pruned file walked all ~2.6M block index entries to find the few with that nFile, a 4.2-4.4 s stall under cs_main each time a 128 MiB block file became prunable. BlockManager now keeps the index entries that have block or undo data grouped by file number. The map is built by one walk on the first PruneOneBlockFile call, so nodes that never prune pay nothing, and ReceivedBlockTransactions adds each stored block to it afterwards. Pruning a file takes its list out of the map and visits only those entries, clearing their data flags, positions and m_blocks_unlinked links and marking them dirty as before. Entries without data, which all have nFile 0, are no longer marked dirty when file 0 is pruned. Upstream bitcoin#36621 (open) batches all files of one prune into a single walk, which does not help IBD where each prune removes one file.

When enabling pruning on a full chain (e.g.$M$ files across $N$ blocks took $O(M \times N)$ work (~5.6B iterations for ~5,770 files and ~972k blocks, stalling startup while holding
-prune=550) or pruning a large backlog after downtime,FindFilesToPruneandFindFilesToPruneManualpreviously calledPruneOneBlockFileonce per file. BecausePruneOneBlockFilescanned all ofm_block_indexon every call, pruningcs_main).This PR splits the changes into three commits:
BlockManageroptions inChainTestingSetupand addblockmanager_find_files_to_prune_manual, covering file selection throughFlushStateToDiskon both sides of the prune height cutoff.PruneOneBlockFilewithPruneBlockFiles(const std::set<int>&)to prune all selected files in a single pass overm_block_indexusing astd::vector<bool>bitmap (sized toMaxBlockfileNum() + 1, independent of how many files are selected). Out-of-range file numbers are rejected up front before any index entry is modified. Addsblockmanager_prune_unlinked_siblings, and updates existing caller tests.CBlockIndexentries withoutBLOCK_HAVE_DATA | BLOCK_HAVE_UNDOinPruneBlockFiles. Because header-only and already-pruned entries also havenFile == 0, this avoids dirtying them when file 0 is selected (after file 0 is pruned, both selectors skip its zero-sized record, so batching is what accounts for the speedup on later backlogs). Addsblockmanager_prune_skips_dataless_entries.Benchmarked with ~972k blocks and 5,770 files to prune: