Repository navigation
Remove chain traversal on sync stats info - #631
randomlogin wants to merge 1 commit into
Conversation
Previously to show the number of synced filters and their headers we traversed the canonical chain. It was done on each new batch which made the sync process very slow. We change the way stats are calculated by introducing counters to the `BlockTree` which is handled during sync.
|
#632 just added a cursor to the filter request state. Perhaps we can make use of that here |
I don't really see a way we can use cursor here, this PR addresses only headers sync. The only filter-related change is in Am I missing something? Or you meant cursor usage in further optimizations? |
|
Would you also be able to benchmark #642? |
Yes, I'll post the results. |
|
Thanks for investigating and finding this problem. For now I think I am going to go with #642 because I have been meaning to rework/remove the |
Indistinguishable by performance from this implementation. Mainnet with a dedicated peer:
|
|
Closing this since 0.8.0 addressed the logs |
I was trying to sync kyoto on mutiny net and realized that it is suspiciously slow, even just the header chain.
Currently on each batch of headers we have the following:
Both calls
total_filter_headers_synced()andtotal_filters_synced()iterate through the whole chain from the current tip up to genesis (with debug it does it twice).Here are the benchmarks:
Headers sync with the current code (I didn't have patience to wait sync of all 3.5mln headers):
Headers sync with `send_chain_update` being a no-op:
Of course on mainnet syncing from the taproot checkpoint it is not that drastic, but is still an unneeded overhead.
(Measurements are done on mutinynet because I have a local synced bitcoin core on mutiny net, as benchmarks on mainnet showed dependence on particular choice of peers we connect to.)
The whole sync stats message is a valuable info, so removing it completely to me seems a bad idea.
Instead we could:
total_filters_syncedwithtotal_filter_headers_synced, as they are only called together insend_chain_update. However it doesn't solve the quadratic sync time.The idea is that for every filter (or filter header) received we check whether the block is in the current canonical chain, in that case increment the counter. On a reorg we explicitly count delta of blocks with filters and change the counter accordingly. This would require us to have a
BTreeMaplookup per filter, but that is okay.This PR adds two counter fields to
BlockTree, they are incremented on a receival of cfilter/cfheader; changedaccordingly during reorgs.
Benchmarks with the proposed change
As a side-improvement we also no longer traverse the canonical chain on
chain.filters_synced()andchain.filter_header_synced()now also become constant-time and they have multiple call locations.I am happy to remove debug assertions of discrepancy check (I run tests with them and they succeeded). I'll also run
kyoto on mainnet for some time in the next days to check if there will be discrepancies in stats count.
Of course there is a lot of space for other optimizations, like reconsider
next_cf_header_message(they also perform a full traversal) andnext_filter_messagealso, we could usevecforcanonical_hashesand we do hash already hashed data, which is redundant.