mempool: get the mempool order without sorting #36977

pull arejula27 wants to merge 3 commits into bitcoin:master from arejula27:txgraph-main-order changing 6 files +119 −6
  1. arejula27 commented at 12:10 PM on November 25, 2026: contributor

    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.

  2. DrahtBot added the label Mempool on Nov 25, 2026
  3. DrahtBot commented at 12:10 PM on November 25, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. bench: add RpcMempoolTxids
    Benchmark getrawmempool without verbose on a mempool made of chains of
    25 transactions. This covers the cost of ordering the entries, which
    RpcMempool does not show.
    98b1610173
  5. txgraph: add GetMainOrder
    Return all transactions in the main graph in the order of
    CompareMainOrder by walking the chunk index, without comparing
    transactions. The unit test and the fuzz test check that it matches the
    order of a BlockBuilder that skips nothing, and the fuzz test also
    checks the order implied by CompareMainOrder.
    7a3420d4ef
  6. txmempool: get main order from TxGraph::GetMainOrder
    GetSortedScoreWithTopology() sorted all entries with
    TxGraph::CompareMainOrder, which does pairwise comparisons across
    clusters. Ask the TxGraph for the main order instead, which it already
    has in its chunk index. check() keeps the comparison sort as a
    reference and asserts that both orders agree.
    a007033a9f
  7. arejula27 force-pushed on Nov 25, 2026
  8. arejula27 commented at 12:20 PM on November 25, 2026: contributor

    Some context on why I looked at this: a user who moved from Knots to Core reported that Electrum took about one minute to connect to his electrs during the tx spike around block 976230 (50k to 70k small txs in the mempool), and about 2 seconds increasig the tx minimum fee (so less tx on mempool). electrs calls getrawmempool in a loop, in the same thread that serves clients, and on Core each of those calls sorts the whole mempool while holding cs (76 of 96 ms with 60k txs on x86).

Contributors
Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 02:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me