TLDR:
- Adds a
TRACE_RAII
macro to easily trace runtime of a code block. - Switch to
CBufferedFile
inBlockManager::ReadBlockFromDisk
is slightly faster: - 9% faster unserialization => 1.2% faster
-reindex-chainstate
While profiling -reindex-changestate
I saw lots of fread()
calls in in BlockManager::ReadBlockFromDisk
. This replaces the use of CAutoFile
with CBufferedFile
with a small buffer, leading to much fewer calls to
fread()
, which gives a little speedup.
I measured runtime of the synchronization with the TRACE_RAII
macro. I ran this command which took about 30 minutes on my PC, with and without CBufferedFile:
0sync && sudo /sbin/sysctl vm.drop_caches=3 && ~/git/github.com/martinus/bitcoin/src/bitcoind -dbcache=20000 -reindex-chainstate -printtoconsole=0 -stopatheight=500000
The measured time spent in unserializing blocks are:
- 308.227s
CAutoFile
- 277.827s
CBufferedFile
It is a bit hard to measure the total effect on -reindex-chainstate
due to random fluctuations in the benchmark. For somewhat reliable results I’ve run the benchmark 10 times, using hyperfine:
0hyperfine \ --parameter-list commit 0f3a6a74a2889817df0f52e19c37de19c664daac,ce26cb6025c47bb4b3e51a579689635da7ca1f6b \
1--setup 'git checkout {commit} && make -j$(nproc)' \
2--prepare 'sync; sudo /sbin/sysctl vm.drop_caches=3' \
3'./bitcoind -dbcache=20000 -reindex-chainstate -printtoconsole=0 -stopatheight=500000'
The results are:
0Benchmark 1: ./bitcoind -dbcache=20000 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (CAutoFile)
1 Time (mean ± σ): 1847.622 s ± 13.448 s [User: 1702.768 s, System: 54.059 s]
2 Range (min … max): 1835.319 s … 1877.065 s 10 runs
3
4Benchmark 2: ./bitcoind -dbcache=20000 -reindex-chainstate -printtoconsole=0 -stopatheight=500000 (CBufferedFile)
5 Time (mean ± σ): 1824.660 s ± 14.394 s [User: 1679.417 s, System: 53.965 s]
6 Range (min … max): 1798.764 s … 1848.940 s 10 runs
So the 9% improvement in unserializing seems to translate to roughly 1.2% total runtime improvement in -reindex-chainstate
on my machine.