mempool: count unbroadcast txids in memory usage #35923

pull l0rinc wants to merge 2 commits into bitcoin:master from l0rinc:l0rinc/mempool-unbroadcast-memory changing 2 files +18 −1
  1. l0rinc commented at 10:33 PM on August 6, 2026: contributor

    Problem: The mempool memory estimate omits the txid set used to retry the initial broadcast of locally submitted transactions, so getmempoolinfo and -maxmempool undercount retained memory.

    Fix: Count this set when calculating mempool memory usage.

  2. test: characterize unbroadcast memory accounting
    Track a mempool transaction before and after adding its txid to the unbroadcast set, which currently retains a node without changing reported memory usage.
    8d5c4a9290
  3. mempool: include unbroadcast txid memory
    Include `m_unbroadcast_txids` in `DynamicMemoryUsage()` to count the set nodes retained for locally submitted transactions awaiting their first successful broadcast, with at most one entry per mempool transaction.
    db21e03c9d
  4. DrahtBot added the label Mempool on Aug 6, 2026
  5. DrahtBot commented at 10:33 PM on August 6, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35923.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  6. DrahtBot closed this on Aug 7, 2026

  7. DrahtBot reopened this on Aug 7, 2026

  8. jeanpablojp commented at 1:07 PM on August 8, 2026: contributor

    ACK db21e03c9d50386072a1123568f917ba65dcf348

    I have tested the code. m_unbroadcast_txids is a std::set<Txid> that memusage.h handles, 80 bytes per txid on my build, and it was not counted elsewhere in DynamicMemoryUsage(). Dropping the term makes the added test fail (760 <= 760), so it covers the change. Built the merge with master (05a7c47): unit suite, mempool_unbroadcast.py and mempool_persist.py pass.

  9. sedited requested review from instagibbs on Sep 20, 2026
  10. sedited requested review from ismaelsadeeq on Sep 20, 2026
  11. bartoli commented at 9:20 PM on September 22, 2026: none

    ACK db21e03c9d50386072a1123568f917ba65dcf348

    Cherry-picked both commits on master. Builds on VS2026. the test seems valid and runs fine. I re-checked CTxMempool members, and now it really seems there are no 'big' members left to account for

  12. musaHaruna commented at 9:06 AM on September 23, 2026: contributor

    Tested ACK db21e03

    Compared getmempoolinfo before and after the change by submitting three transactions to a regtest node, then requesting each individually from a test peer using GETDATA.

    Without the change, usage reported 3320 bytes and remained unchanged as unbroadcastcount decreased. With the change, it initially reported 3560 bytes and decreased by 80 bytes per transaction served, reaching 3320 after all three were served. Transaction count and virtual size stayed unchanged in both runs. These are estimated memory figures on my machine.

    Mempool unit tests and mempool_unbroadcast.py, mempool_persist.py, and mempool_limit.py passed.

    One non-blocking follow-up: MempoolInfoToJSON() uses GetUnbroadcastTxs().size(), which copies the entire set under the mempool lock just to count it. A count accessor could avoid that copy. This predates the PR and can be handled separately. I can put together a small follow-up PR if that sounds useful.

  13. in src/test/mempool_tests.cpp:540 in db21e03c9d
     536 | @@ -537,7 +537,7 @@ BOOST_AUTO_TEST_CASE(MempoolUnbroadcastMemoryUsage)
     537 |  
     538 |      auto usage_before{pool.DynamicMemoryUsage()};
     539 |      pool.AddUnbroadcastTx(txid);
     540 | -    BOOST_CHECK_EQUAL(pool.DynamicMemoryUsage(), usage_before); // TODO: Include the retained set node in reported usage
     541 | +    BOOST_CHECK_GT(pool.DynamicMemoryUsage(), usage_before);
    


    instagibbs commented at 2:31 PM on September 23, 2026:

    nit: bot suggests tight assertion

        BOOST_CHECK_EQUAL(new, before + memusage::MallocUsage(sizeof(memusage::stl_tree_node<Txid>)))
    

    l0rinc commented at 5:32 PM on September 23, 2026:

    This was deliberate to make the characterization test assertions minimal (otherwise the assertion itself would change in the before/after scenario so we wouldn't be testing the same thing), but if reviewers would prefer that, I can extend it.

  14. instagibbs approved
  15. instagibbs commented at 2:33 PM on September 23, 2026: member

    ACK db21e03c9d50386072a1123568f917ba65dcf348

    risk is ~0 without this fix, but this also seems correct

  16. ismaelsadeeq commented at 3:18 PM on September 23, 2026: member

    ACK db21e03c9d50386072a1123568f917ba65dcf348

    Straightforward fix.

  17. l0rinc commented at 5:39 PM on September 23, 2026: contributor

    MempoolInfoToJSON() uses GetUnbroadcastTxs().size(), which copies the entire set under the mempool lock just to count it.

    Hah, good find, seems return-value elision does not eliminate it, please open a new PR for this and see if we have other related similar cases in that area. If you can, a small temporary benchmark could be added as a manual reproducer to prove that the compiler doesn't actually eliminate the copy.

  18. achow101 commented at 11:10 PM on September 23, 2026: member

    ACK db21e03c9d50386072a1123568f917ba65dcf348

  19. achow101 merged this on Sep 23, 2026
  20. achow101 closed this on Sep 23, 2026

  21. l0rinc deleted the branch on Sep 23, 2026

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-10-11 08:51 UTC

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