node: batch block file pruning in a single pass over block index #36621

pull mutatrum wants to merge 1 commits into bitcoin:master from mutatrum:pr-batch-pruning changing 5 files +238 −14
  1. mutatrum commented at 11:39 AM on October 23, 2026: contributor

    When starting a 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 running on a Pi 5 for ~10 minutes on startup while holding cs_main.

    This PR replaces PruneOneBlockFile with PruneBlockFiles(const std::set<int>&), which collects all candidate files and prunes them in a single O(M + N) pass over m_block_index using a compact dynamic bitfield (std::vector<bool>). Also skips entries without block or undo data to avoid unneeded checks and prevent file 0 pruning from dirtying header-only or already-pruned index entries. The new signature is analogous to UnlinkPrunedFiles({file_number});.

    Benchmarked with ~972k blocks and 5,770 files to prune:

    • x86_64 (i7-8665U @ 1.90 GHz): 75.1s -> 14.7ms (~5,100x speedup)
    • ARM64 (Raspberry Pi 5): 606.0s -> 26.3ms (>3,500x speedup)

    Both timings were taken with a datadir whose block index and block file info records came from a full chain, with the blk*.dat and rev*.dat files backfilled as 0-byte files. Pruning selection reads only the index and the file info, so empty files reproduce the startup cost without a full chain, and UnlinkPrunedFiles deletes them cheaply.

    A case where this matters in practice is a pruned node restarting after a long outage. It downloads headers for the whole gap first, leaving many header-only entries behind, then has to prune a large backlog in a few flushes. The data-less skip avoids re-dirtying all of those entries, and the single pass bounds the work by the index size instead of files times index size.

    Add unit test blockmanager_prune_block_files_batch to verify batch pruning, empty set no-ops, clean dirty-set behavior, and cleared block and undo metadata. Add a test for the FindFilesToPruneManual caller path, including a mixed valid and out-of-range file set. Update caller tests in blockmanager_tests and rescan_tests.

    Addition, I ran the test on my laptop as well, going from full node to prune=5000 took 8:11:

    2026-11-11T16:50:09Z init message: Pruning blockstore…
    2026-11-11T16:58:20Z block tree size = 975494
    
  2. DrahtBot commented at 11:40 AM on October 23, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK jeanpablojp

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36858 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte)
    • #36856 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte)
    • #36854 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte)
    • #36739 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager by pzafonte)
    • #36738 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36738.svg"></sub> (refactor: move block writes and pruning into BlockManager by pzafonte)
    • #36598 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc)
    • #36149 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36149.svg"></sub> (validation: Leave some pruned blocks in m_blocks_unlinked, fix another nSequenceId issue by mzumsande)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. mutatrum force-pushed on Oct 23, 2026
  4. DrahtBot added the label CI failed on Oct 23, 2026
  5. DrahtBot commented at 12:41 PM on October 23, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task macOS-cross to x86_64: https://github.com/bitcoin/bitcoin/actions/runs/39947671739/job/120394208316</sub> <sub>LLM reason (✨ experimental): CI failed compiling blockmanager_tests.cpp: Clang’s thread-safety checks report accesses to nFile and nStatus without holding cs_main, and -Werror treats them as errors.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  6. mutatrum force-pushed on Oct 23, 2026
  7. DrahtBot removed the label CI failed on Oct 23, 2026
  8. mutatrum force-pushed on Nov 2, 2026
  9. DrahtBot added the label CI failed on Nov 3, 2026
  10. DrahtBot commented at 12:21 AM on November 3, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task ASan + LSan + UBSan + integer: https://github.com/bitcoin/bitcoin/actions/runs/41544539018/job/125616778379</sub> <sub>LLM reason (✨ experimental): CI failed because blockmanager_tests triggered UBSan on an implicit signed-to-unsigned conversion in BlockManager::PruneBlockFiles (blockstorage.cpp:280).</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  11. mutatrum force-pushed on Nov 3, 2026
  12. mutatrum force-pushed on Nov 3, 2026
  13. mutatrum force-pushed on Nov 3, 2026
  14. DrahtBot removed the label CI failed on Nov 3, 2026
  15. mutatrum force-pushed on Nov 3, 2026
  16. mutatrum force-pushed on Nov 3, 2026
  17. DrahtBot added the label CI failed on Nov 3, 2026
  18. mutatrum force-pushed on Nov 3, 2026
  19. DrahtBot removed the label CI failed on Nov 3, 2026
  20. jeanpablojp commented at 11:09 PM on November 20, 2026: contributor

    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.

  21. node: batch block file pruning in a single pass over block index
    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 O(M + N)
    pass over m_block_index using a compact dynamic bitfield
    (std::vector<bool>). Also skip entries without block or undo data to
    avoid unneeded checks and prevent file 0 pruning from dirtying
    header-only or already-pruned index entries.
    
    Benchmarked with ~972k blocks and 5,770 files to prune:
    - x86_64 (i7-8665U @ 1.90 GHz): 75.1s -> 14.7ms (~5,100x speedup)
    - ARM64 (Raspberry Pi 5): 606.0s -> 26.3ms (>3,500x speedup)
    
    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_block_files_batch to verify batch
    pruning, empty set no-ops, clean dirty-set behavior, and cleared block
    and undo metadata. Add a test covering the FindFilesToPruneManual
    caller path end to end. Update caller tests in blockmanager_tests and
    rescan_tests.
    
    Apply BlockManager arguments in the test setup so unit tests can run in
    prune mode.
    38d6447063
  22. mutatrum force-pushed on Nov 25, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-30 04:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me