Rebased and reopened #28945
Original description:
TL;DR: this change improves sync time on my PC by ~6%.
While syncing when the CCoinsViewCache gets full it is flushed to disk and then memory is freed. Depending on available cache size, this happens several times. This PR removes the unnecessary reallocations for temporary CCoinsViewCache and reserves the cacheCoins’s bucket array to its previous size.
I benchmarked the change on an AMD Ryzen 9 7950X with this command:
0hyperfine \ 1--show-output --parameter-list commit b5a271334ca81a6adcb1c608d85c83621a9eae47,ceeb71816512642e92a110b2cf5d2549d090fe78 \ 2--setup 'git checkout {commit} && make -j$(nproc) src/bitcoind' \ 3--prepare 'sync; sudo /sbin/sysctl vm.drop_caches=3' \ 4-M 3 './src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000'
Where b5a2713 is mergebase, and ceeb718 this PR.
Which runs
./src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000
3 times and compares both commits, giving this result:0Benchmark 1: ./src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (commit = b5a271334ca81a6adcb1c608d85c83621a9eae47) 1 Time (mean ± σ): 2815.648 s ± 70.720 s [User: 2639.959 s, System: 640.051 s] 2 Range (min … max): 2736.616 s … 2872.964 s 3 runs 3 4Benchmark 2: ./src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (commit = ceeb71816512642e92a110b2cf5d2549d090fe78) 5 Time (mean ± σ): 2661.520 s ± 23.205 s [User: 2473.010 s, System: 624.040 s] 6 Range (min … max): 2635.816 s … 2680.926 s 3 runs 7 8Summary 9 ./src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (commit = ceeb71816512642e92a110b2cf5d2549d090fe78) ran 10 1.06 ± 0.03 times faster than ./src/bitcoind -dbcache=500 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (commit = b5a271334ca81a6adcb1c608d85c83621a9eae47)