Skip to content

Conversation

@sedited
Copy link
Contributor

@sedited sedited commented Dec 12, 2025

Calculating the rolling bloom filters for the txorphanage takes some CPU time from the scheduler thread. This can be observed for example in this flamegraph, where handling the filter takes about 2.6% of total time (and most of the scheduler thread's time).

During ibd the entries in the tx download bloom filter are just continuously rolled over and aren't consumed, since no mempool entries are created by incoming transactions from peers during ibd. The mempool does accept transactions via RPC, or the wallet at the time, however these don't interact with the orphanage and the txdownloadman, because adding anything to those is guarded by IsInitialBlockDownload() checks as well.

We're usually latching ibd to false a few blocks before catching up to the tip, so this should also not significantly degrade the performance of the filter once fully caught up.

@DrahtBot DrahtBot added the P2P label Dec 12, 2025
@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 12, 2025

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

Code Coverage & Benchmarks

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

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK mzumsande, fjahr

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.

@maflcko
Copy link
Member

maflcko commented Dec 12, 2025

During ibd the entries in the tx download bloom filter are just continuously rolled over, and aren't consumed, since no mempool is maintained at the time.

I think this could be clarified a bit, why this is safe. My understanding is that a mempool does exist and is maintained (albeit it will normally be empty). The mempool will also happily accept transactions via RPC, or the wallet. However, this is fine, because they don't interact with the orphanage and the txdownloadman, because adding anything to those is guarded by IsInitialBlockDownload() checks as well?

Ref:

https://github.com/bitcoin/bitcoin/blob/8da8ff30a57f0fe7f627d121c5e40466f1ecb0b2/src/net_processing.cpp#L4258-L4268

and

https://github.com/bitcoin/bitcoin/blob/8da8ff30a57f0fe7f627d121c5e40466f1ecb0b2/src/net_processing.cpp#L3987-L3990

@sedited
Copy link
Contributor Author

sedited commented Dec 15, 2025

Re #34054 (comment)

I think this could be clarified a bit, why this is safe.

Thanks, that was not precise. Slightly reworded your explanation and added it to the pull request description. I was curious if the ibd check might cause a performance regression, since we're probably triggering fewer de-allocs on the scheduler thread. I tried to get some results in benchcoin. The results were kind of mixed, but there is clearly no more work being done through the txdownloadman during ibd.

@maflcko
Copy link
Member

maflcko commented Dec 16, 2025

fewer de-allocs on the scheduler thread.

I think this patch does not change whether the event is run on the scheduler thread, or not, but rather if the event runs faster in the scheduler thread with the early return. So I think this should not move destructor calls between threads. Or am I missing something?

@sedited
Copy link
Contributor Author

sedited commented Dec 16, 2025

Re #34054 (comment)

but rather if the event runs faster in the scheduler thread with the early return.

Yes, and I was curious if this might change where the final decrement for the block's shared pointer happens.

@maflcko
Copy link
Member

maflcko commented Dec 16, 2025

Oh, I see. I guess if the behavior should be enforced at compile-time, the blocks could be std::moved into the event

@sedited
Copy link
Contributor Author

sedited commented Dec 16, 2025

Copy link
Contributor

@mzumsande mzumsande left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

In addition to RPC/wallet mentioned above, the mempool could also contain transactions from disk - downloaded at a time when we weren't in IBD. This is very common for nodes that aren't online 24/7 , so that they will go back into IBD at each subsequent start. But those shouldn't really be a problem either I think.

Also, this introduces a cs_main lock in the scheduler thread, which could cause some lock contention with msghand - not sure how important this it, but it might go well together with #32885.

}

// The following task can be skipped since we don't maintain a mempool for
// the historical chainstate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment could be adjusted for why we skip it during IBD.

@sedited
Copy link
Contributor Author

sedited commented Dec 24, 2025

Also, this introduces a cs_main lock in the scheduler thread, which could cause some lock contention with msghand - not sure how important this it, but it might go well together with #32885.

Yes, the original PR explicitly worked around this by introducing a variable to track the ibd state: https://github.com/bitcoin/bitcoin/pull/32730/files#diff-b1e19192258d83199d8adaa5ac31f067af98f63554bfdd679bd8e8073815e69dL1911-L1912 . A prior approach of that PR passes the ibd state directly in the BlockConnected callback. I think blocking here might be fine (which is why it opened this), but it's not great either. What do you think of drafting this for now, and we try to make progress with #32885, or do you prefer one of the other approaches?

@fjahr
Copy link
Contributor

fjahr commented Dec 25, 2025

Concept ACK

@mzumsande
Copy link
Contributor

What do you think of drafting this for now, and we try to make progress with #32885, or do you prefer one of the other approaches?

I think I like the approach from #32885 because it addresses the issue at its root (so that many IBD checks would benefit, not just this one). As to whether this should be drafted I'm unsure - it's probably an improvement even with the added locking due to the saved work, but given the mixed results from benchcoin maybe we'd need more testing?

@maflcko
Copy link
Member

maflcko commented Jan 12, 2026

given the mixed results from benchcoin maybe we'd need more testing?

The latest runs shows a speedup in both scenarios, but the earlier run showed a slowdown in mainnet-default-uninstrumented, but the only difference was a rebase, so this seems to indicate that the benchcoin run itself is not stable?

(Commit 8da8ff30a57f0fe7f627d121c5e40466f1ecb0b2 was deleted by GitHub, so I am just going after the comment #34054 (comment))

@l0rinc l0rinc mentioned this pull request Jan 14, 2026
23 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants