net: Skip cs_main when a peer has no orphan work #36445

Open
w0xlt wants to merge 1 commit from net/orphan-check-without-cs-main into master AGit
w0xlt commented 2026-10-06 02:18:35 +00:00 (Migrated from github.com)

ProcessMessages takes cs_main for every peer on every handler pass, just to check for orphan transactions to reconsider, though most peers have none. This PR checks for orphan work under m_tx_download_mutex first and skips cs_main when there is none. Peers with orphan work take the same path as before, plus one uncontended lock.

This removes needless cs_main contention with validation, RPC and wallet threads. It's part of a broader effort to reduce cs_main contention in net processing (see #36244 for context), but stands on its own.

ProcessMessages handling a ping master this PR
no contention 0.005 ms 0.005 ms
another thread holds cs_main for 50 ms 50.1 ms 0.02 ms

Release builds, median of 3 runs. SendMessages in the same handler pass still takes cs_main.

Tested by process_messages_without_orphan_work, which fails without this change.

`ProcessMessages` takes `cs_main` for every peer on every handler pass, just to check for orphan transactions to reconsider, though most peers have none. This PR checks for orphan work under `m_tx_download_mutex` first and skips `cs_main` when there is none. Peers with orphan work take the same path as before, plus one uncontended lock. This removes needless `cs_main` contention with validation, RPC and wallet threads. It's part of a broader effort to reduce `cs_main` contention in net processing (see #36244 for context), but stands on its own. | `ProcessMessages` handling a `ping` | master | this PR | |---|---|---| | no contention | 0.005 ms | 0.005 ms | | another thread holds `cs_main` for 50 ms | 50.1 ms | 0.02 ms | Release builds, median of 3 runs. `SendMessages` in the same handler pass still takes `cs_main`. Tested by `process_messages_without_orphan_work`, which fails without this change.
DrahtBot commented 2026-10-06 02:18:43 +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.
A summary of reviews will appear here.

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/36445 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36445.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36445#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. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: cf80493e2367786ce9946c522eb8313a52c90557
Head: 6549bb4bd6ae27c339c7a277bab9d48ed2fb730c

No verified findings are available from this partial review.

Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial_glm review had incomplete evidence.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `cf80493e2367786ce9946c522eb8313a52c90557` Head: `6549bb4bd6ae27c339c7a277bab9d48ed2fb730c` No verified findings are available from this partial review. Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial_glm review had incomplete evidence. [Full review report](<https://review.fish.foo/traces/36445-6549bb4bd6ae27c339c7a277bab9d48ed2fb730c-448-1.html>)
instagibbs commented 2026-10-06 11:33:51 +00:00 (Migrated from github.com)

I think these kind of refactors only make sense if/when https://github.com/bitcoin/bitcoin/pull/36244 is an accepted goal by the project

I have not actually looked, just noting for this PR's sake.

I think these kind of refactors only make sense if/when https://github.com/bitcoin/bitcoin/pull/36244 is an accepted goal by the project I have not actually looked, just noting for this PR's sake.
w0xlt commented 2026-10-06 15:47:17 +00:00 (Migrated from github.com)

This PR stands on its own, though: skipping cs_main when a peer has no orphan work helps on master today, with or without #36244.
#36244 just benefits more from it.

This PR stands on its own, though: skipping `cs_main` when a peer has no orphan work helps on master today, with or without #36244. #36244 just benefits more from it.
w0xlt commented 2026-10-07 05:41:40 +00:00 (Migrated from github.com)

PR description updated for greater clarity.

PR description updated for greater clarity.
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!36445
No description provided.