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

Manually merged
musaHaruna merged 1 commit from rpc-unbroadcast-count into master 2026-10-05 10:22:43 +00:00 AGit
musaHaruna commented 2026-09-26 20:31:44 +00:00 (Migrated from github.com)

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.

Follow-up to [#35923](https://github.com/bitcoin/bitcoin/pull/35923/#issuecomment-5799823742). `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](https://github.com/bitcoin/bitcoin/compare/master...musaHaruna:bitcoin:bench-unbroadcast-count) 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.
DrahtBot commented 2026-09-26 20:31:53 +00:00 (Migrated from github.com)

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

External sites

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 <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36349 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36349.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36349#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [maflcko](https://github.com/bitcoin/bitcoin/pull/36349#issuecomment-5978258498), [l0rinc](https://github.com/bitcoin/bitcoin/pull/36349#issuecomment-5979168974), [davidgumberg](https://github.com/bitcoin/bitcoin/pull/36349#issuecomment-5985761235), [sedited](https://github.com/bitcoin/bitcoin/pull/36349#pullrequestreview-5412955599) | 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-->
l0rinc (Migrated from github.com) approved these changes 2026-09-27 22:29:19 +00:00
l0rinc (Migrated from github.com) left a comment

lightly tested ACK fef74dd296

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.

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.
l0rinc (Migrated from github.com) commented 2026-09-27 20:36:29 +00:00

Given that this caused a confusion before:

    /** Returns a copy of the unbroadcast txid set */
Given that this caused a confusion before: ```suggestion /** Returns a copy of the unbroadcast txid set */ ```
l0rinc (Migrated from github.com) commented 2026-09-27 20:25:19 +00:00

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

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

Base: 66776840beb558f7e84451c2c55457f0e06242f0
Head: 052cf121ab0a5373d1753564d90075f1997f4610

No verified findings are available from this partial review.

Review coverage was incomplete. The adversarial_glm review did not complete.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `66776840beb558f7e84451c2c55457f0e06242f0` Head: `052cf121ab0a5373d1753564d90075f1997f4610` No verified findings are available from this partial review. Review coverage was incomplete. The adversarial_glm review did not complete. [Full review report](<https://review.fish.foo/traces/36349-052cf121ab0a5373d1753564d90075f1997f4610-372-2.html>)
davidgumberg commented 2026-10-01 01:45:39 +00:00 (Migrated from github.com)
crACK https://github.com/bitcoin/bitcoin/commit/fef74dd2965b02668f8717c8a8cc4b8b24c0e604
musaHaruna (Migrated from github.com) reviewed 2026-10-04 05:22:55 +00:00
musaHaruna (Migrated from github.com) left a comment

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

Thanks for the review @l0rinc and @davidgumberg. Force pushed [052cf12](https://github.com/bitcoin/bitcoin/pull/36349/changes/052cf121ab0a5373d1753564d90075f1997f4610) and improved docs as suggested by @l0rinc.
musaHaruna (Migrated from github.com) reviewed 2026-10-04 05:23:26 +00:00
musaHaruna (Migrated from github.com) commented 2026-10-04 05:23:26 +00:00

Fixed. Thanks.

Fixed. Thanks.
musaHaruna (Migrated from github.com) reviewed 2026-10-04 05:24:06 +00:00
musaHaruna (Migrated from github.com) commented 2026-10-04 05:24:06 +00:00

Removed the comment.

Removed the comment.
maflcko commented 2026-10-04 08:50:11 +00:00 (Migrated from github.com)

lgtm ACK 052cf121ab

lgtm ACK 052cf121ab0a5373d1753564d90075f1997f4610
l0rinc commented 2026-10-04 10:49:25 +00:00 (Migrated from github.com)
ACK 052cf121ab0a5373d1753564d90075f1997f4610
davidgumberg commented 2026-10-04 23:52:16 +00:00 (Migrated from github.com)
crACK https://github.com/bitcoin/bitcoin/commit/052cf121ab0a5373d1753564d90075f1997f4610
sedited (Migrated from github.com) approved these changes 2026-10-05 10:13:17 +00:00
sedited (Migrated from github.com) left a comment
ACK 052cf121ab0a5373d1753564d90075f1997f4610
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36349
No description provided.