CheckBlockIndex()
are consistency checks that are currently enabled by default on regtest.
The function is rather slow, which is annoying if you
- attempt to run it on other networks, especially if not fully synced
- want to generate a long chain on regtest and see block generation slow down because you forgot to disable
-checkblockindex
or don’t know it existed.
One reason why it’s slow is that in order to be able to traverse the block tree depth-first from genesis, it inserts pointers to all block indices into a std::multimap
- for which inserts and lookups become slow once there are hundred thousands of entries.
However, typically the block index is mostly chain-like with just a few forks so a multimap isn’t really needed for the most part. This PR suggests to store the block indices of the chain ending in the best header in a vector instead, and store only the rest of the indices in a multimap. This does not change the actual consistency checks that are being performed for each index, just the way the block index tree is stored and traversed.
This adds a bit of complication to make sure each block is visited (note that there are asserts that check it), making sure that the two containers are traversed correctly, but it speeds up the function considerably:
On master, a single invocation of CheckBlockIndex
takes ~1.4s on mainnet for me (4.9s on testnet which has >2.4 million blocks).
With this branch, the runtime goes down to ~0.27s (0.85s on testnet).This is a speedup by a factor ~5.