1 suggestion
Optional improvements
[suggestion] · REST JSON headers add a cs_main acquisition per header
Opt-in REST JSON headers requests can now acquire cs_main up to 2001 times instead of once; sampling nTx in the existing critical section avoids those additional acquisitions, though the latency impact is unmeasured.
Location: src/rpc/blockchain.cpp:215 (head).
Details and suggested fix
rest_headers collects up to 2000 active-chain block indices under one LOCK(cs_main), releases it, and then invokes blockheaderToJSON for every JSON header. The new read therefore adds a possible wait on cs_main for each header. Verbose getblockheader similarly acquires the mutex for lookup and again here. Each added critical section only copies a scalar; REST defaults to count=5, and binary/hex REST output and non-verbose getblockheader skip this path.
Keep the synchronization required by ReceivedBlockTransactions, but capture (pindex, nTx) pairs in the existing locked REST loop and pass the sampled count into blockheaderToJSON. In getblockheader, capture the count alongside the lookup. For blockToJSON, supply block.vtx.size() from the already deserialized block. This keeps header serialization outside the mutex without introducing a separate acquisition for each count.