CTxMemPool::GetSortedScoreWithTopology() gets the global mempool order with a std::sort that uses TxGraph::CompareMainOrder as comparator. This is N log N comparisons, and it runs with cs held. It is used by getrawmempool, the P2P mempool message, mempool.dat dumping and check().
TxGraph already keeps all main chunks in an index ordered by ChunkOrder (m_main_chunkindex), which is the same order as CompareMainOrder. This PR adds TxGraph::GetMainOrder(), which walks that index and expands each chunk in linearization order. The result is all transactions in CompareMainOrder order, in O(N) and with no comparator calls. GetSortedScoreWithTopology() now uses it. The old sort is kept in check() as an assert that both orders are equal.
The order is the same, so there is no change in RPC, P2P or mempool.dat output.
I ran some preliminar experiments and got:
Ryzen 5 3600, x86:
| before | after | |
|---|---|---|
RpcMempoolTxids (10k txs) |
5.10 ms | 1.07 ms |
getrawmempool false handler, 60k txs |
99 ms | 24 ms |
Most of the remaining time in RpcMempoolTxids is building the JSON array, not the order. getrawmempool true does not change much because entryToJSON dominates. The time with cs held goes down in the same way, which matters for indexers that call getrawmempool every few seconds.