getblock
is slow on verbosity, it’s happened because LOCK(cs_main)
applied for whole function including JSON generation
https://github.com/bitcoin/bitcoin/blob/ef70f9b52b851c7997a9f1a0834714e3eebc1fd8/src/rpc/blockchain.cpp#L810
I added few lines which print spent time:
0 LOCK(cs_main);
1 auto t1 = std::chrono::high_resolution_clock::now();
2...
3 auto t2 = std::chrono::high_resolution_clock::now();
4 auto ret = blockToJSON(block, pblockindex, verbosity >= 2);
5 auto t3 = std::chrono::high_resolution_clock::now();
6 std::cout << "blockToJSON " << pblockindex->nHeight << ": " << std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() << " " << std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count() << std::endl;
7 return ret;
and numbers looks like:
0blockToJSON 570019: 14727 201870
1blockToJSON 570022: 16049 225224
2blockToJSON 570021: 33776 217398
3blockToJSON 570020: 21754 191669
4blockToJSON 570023: 30949 236941
5blockToJSON 570025: 23346 223673
6blockToJSON 570024: 18732 203002
7blockToJSON 570026: 17467 216213
8blockToJSON 570027: 16658 217395
9blockToJSON 570028: 19015 219183
i.e. everything before JSON generation takes 15-30ms, while JSON takes minimum 190ms and lock cs_main
for all this time. As result it’s possible extract only ~5 blocks per second.
Looking on the code I think it’s possible copy data from CBlockIndex
to extra structure, so in this case cs_main
can be unlocked right after GetBlockChecked
.
https://github.com/bitcoin/bitcoin/blob/ef70f9b52b851c7997a9f1a0834714e3eebc1fd8/src/rpc/blockchain.cpp#L828
As result getblock
in verbosity mode should be much faster (in case if -rpcthreads
will be adjusted too).
Does this make sense?