rpc: avoid copying unbroadcast transaction IDs just to count them #36349

pull musaHaruna wants to merge 1 commits into bitcoin:master from musaHaruna:rpc-unbroadcast-count changing 2 files +8 −2
  1. musaHaruna commented at 8:31 PM on September 26, 2026: contributor

    Follow-up to #35923.

    getmempoolinfo currently calls GetUnbroadcastTxs().size() to report unbroadcastcount. The getter returns the set by value, so every call copies its entries under the mempool lock and immediately discards the copy after reading its size.

    Fix: Add GetUnbroadcastTxCount() and use it in the RPC to read the size under the same lock without allocating a temporary set. This is a separate accessor because broadcast retry and persistence still need the transaction IDs returned by the existing GetUnbroadcastTxs().

    I also searched for similar cases nearby: the ancestor/descendant count helpers and the cluster-count path build vectors just to read their sizes, while the mining RPC uses GetDust(...).empty() to check whether any dust outputs exist. These could potentially use count-only or boolean helpers, but would require separate changes to the graph and policy code, so I left them out of this PR.

    Added a temporary bench_bitcoin benchmark on my branch, comparing both getters.

    Results below are medians from five runs on an Apple M5, using Apple clang 17/libc++ with -O2:

    Unbroadcast IDs GetUnbroadcastTxs().size() (ns/query) GetUnbroadcastTxCount()(ns/query)
    0 25.22 22.60
    1 58.99 22.52
    10 420.82 22.79
    100 4,788.22 22.45
    1,000 57,904.17 22.60
    10,000 908,399.04 22.62

    The copy cost grew with the set size, while the count accessor stayed approximately constant.

  2. DrahtBot added the label RPC/REST/ZMQ on Sep 26, 2026
  3. DrahtBot commented at 8:31 PM on September 26, 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.

    Type Reviewers
    ACK maflcko, l0rinc, davidgumberg, sedited

    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.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in src/txmempool.h:586 in fef74dd296
     582 | @@ -583,6 +583,13 @@ class CTxMemPool
     583 |          return m_unbroadcast_txids;
     584 |      }
     585 |  
     586 | +    /** Returns the number of transactions in the unbroadcast set */
    


    l0rinc commented at 8:25 PM on September 27, 2026:

    The comment just rewords the method name, I don't think it's useful


    musaHaruna commented at 5:24 AM on October 4, 2026:

    Removed the comment.

  5. in src/txmempool.h:579 in fef74dd296


    l0rinc commented at 8:36 PM on September 27, 2026:

    Given that this caused a confusion before:

        /** Returns a copy of the unbroadcast txid set */
    

    musaHaruna commented at 5:23 AM on October 4, 2026:

    Fixed. Thanks.

  6. l0rinc approved
  7. l0rinc commented at 10:29 PM on September 27, 2026: contributor

    lightly tested ACK fef74dd2965b02668f8717c8a8cc4b8b24c0e604

    This avoids copying the unbroadcast set on every getmempoolinfo call. The other two callers use the txids after releasing the mempool lock, so avoiding their copies is less straightforward.

  8. rpc: avoid copying unbroadcast txids to count them 052cf121ab
  9. musaHaruna force-pushed on Oct 4, 2026
  10. musaHaruna commented at 5:22 AM on October 4, 2026: contributor

    Thanks for the review @l0rinc and @davidgumberg. Force pushed 052cf12 and improved docs as suggested by @l0rinc.

  11. maflcko commented at 8:50 AM on October 4, 2026: member

    lgtm ACK 052cf121ab0a5373d1753564d90075f1997f4610

  12. DrahtBot requested review from l0rinc on Oct 4, 2026
  13. DrahtBot requested review from davidgumberg on Oct 4, 2026
  14. l0rinc commented at 10:49 AM on October 4, 2026: contributor

    ACK 052cf121ab0a5373d1753564d90075f1997f4610

  15. sedited approved
  16. sedited commented at 10:13 AM on October 5, 2026: contributor

    ACK 052cf121ab0a5373d1753564d90075f1997f4610

  17. sedited merged this on Oct 5, 2026
  18. sedited closed this on Oct 5, 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-30 09:40 UTC