Skip to content

mempool: Avoid needless vtx iteration during IBD - #32827

Merged
glozow merged 2 commits into
bitcoin:masterfrom
l0rinc:l0rinc/empty-mempool-IBD
Jul 21, 2025
Merged

mempool: Avoid needless vtx iteration during IBD#32827
glozow merged 2 commits into
bitcoin:masterfrom
l0rinc:l0rinc/empty-mempool-IBD

Conversation

@l0rinc

@l0rinc l0rinc commented Jun 29, 2025

Copy link
Copy Markdown
Contributor

During Initial Block Download, the mempool is usually empty, but CTxMemPool::removeForBlock is still called for every connected block where we:

  • iterate over every transaction in the block even though none will be found in the empty mapTx, always leaving txs_removed_for_block empty...
  • which is pre-allocated regardless with 40 bytes * vtx.size(), even though it will always remain empty.

Similarly to #32730 (comment), this change introduces a minor performance & memory optimization by only executing the loop if any of the affected mempool maps have any contents. The second commit is cherry-picked from there since it's related to this change as well.

@DrahtBot

DrahtBot commented Jun 29, 2025

Copy link
Copy Markdown
Contributor

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/32827.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK optout21, ismaelsadeeq, glozow
Stale ACK luke-jr, maflcko, mzumsande

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28676 ([WIP] Cluster mempool implementation by sdaftuar)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@l0rinc
l0rinc marked this pull request as draft June 29, 2025 18:24

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"known to be empty" / "always empty" is too strong:
It's quite common for nodes that are not online 24/7 to be in IBD (because they have to catch up a few weeks/months) but have a non-empty mempool. This doesn't affect the approach, just the PR / commit message description.

@l0rinc

l0rinc commented Jun 30, 2025

Copy link
Copy Markdown
Contributor Author

Thanks, updated - though I don't think that's "Initial", just regular Block Download.

Comment thread src/txmempool.cpp
@maflcko

maflcko commented Jun 30, 2025

Copy link
Copy Markdown
Member

Similarly to #32730 (comment),

Could make sense to include that change here as well?

@l0rinc
l0rinc force-pushed the l0rinc/empty-mempool-IBD branch 3 times, most recently from 5b94b56 to 028f7e5 Compare June 30, 2025 17:47
@l0rinc l0rinc changed the title mempool: Avoid needless vtx iteration in removeForBlock during IBD mempool: Avoid needless vtx iteration during IBD Jun 30, 2025
@l0rinc
l0rinc force-pushed the l0rinc/empty-mempool-IBD branch from 028f7e5 to 54f9cb8 Compare June 30, 2025 21:07
@l0rinc
l0rinc marked this pull request as ready for review July 1, 2025 14:22
@maflcko

maflcko commented Jul 7, 2025

Copy link
Copy Markdown
Member

Just to clarify this is just a refactor/cleanup and doesn't affect end-to-end IBD performance in a measurable way?

@l0rinc

l0rinc commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

That's my expectation, yes. Would you like me to measure it to sure?

Edit: measured a full reindex, the effect is <1%, as expected

@luke-jr

luke-jr commented Jul 8, 2025

Copy link
Copy Markdown
Member

crACK 54f9cb85c4be2c30be0eb89f29b76a4cbf6f1c50

@maflcko

maflcko commented Jul 9, 2025

Copy link
Copy Markdown
Member

lgtm ACK 54f9cb85c4be2c30be0eb89f29b76a4cbf6f1c50 🌌

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: lgtm ACK 54f9cb85c4be2c30be0eb89f29b76a4cbf6f1c50 🌌
Wlk4PL+rrh0WOZRZ2h/TJZ1bw7tQq2rihW0c4tE3QXFO7MY7GBGSVVYcDT6sjjyMwDFbnFqFOFNUkrQTmSOICg==

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review ACK 54f9cb85c4be2c30be0eb89f29b76a4cbf6f1c50

I think it makes sense not to do these iterations over block txns if these can never result in any action - even if it doesn't increase performance measurably.

Comment thread src/txmempool.cpp Outdated

@ismaelsadeeq ismaelsadeeq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept ACK. edit I think the comment about updating the fee logic should be removed, as it can easily become stale.

Comment thread src/txmempool.cpp Outdated
Comment thread src/txmempool.cpp Outdated

@ismaelsadeeq ismaelsadeeq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code review ACK 54f9cb85c4be2c30be0eb89f29b76a4cbf6f1c50

l0rinc and others added 2 commits July 18, 2025 16:50
During Initial Block Download, the mempool is usually empty, but `CTxMemPool::removeForBlock` is still called for every connected block where we:
* iterate over every transaction in the block even though none will be found in the empty `mapTx`, always leaving `txs_removed_for_block` empty...
* which is pre-allocated regardless with 40 bytes * vtx.size(), even though it will always remain empty.

This change introduces a minor performance optimization by only executing the loop if any of the core mempool maps have any contents.

The call to `MempoolTransactionsRemovedForBlock` and the updates to the rolling fee logic remain unchanged.

The `removeForBlock` was also updated stylistically to match the surrounding methods and a clarification was added to clarify that it affects fee estimation as well.
@l0rinc

l0rinc commented Jul 18, 2025

Copy link
Copy Markdown
Contributor Author

Had to rebase after 067365d#diff-e6100361fa0e9e25478f808ca084e5f681d4dddbbee7b3bea0f9d5bcd29db3aaR39-R563, src/txorphanage.cpp was moved to src/node/txorphanage.cpp, the change is exactly the same otherwise, would appreciate re-reviews.

Comment thread src/txmempool.cpp
@optout21

Copy link
Copy Markdown
Contributor

ACK 249889b

@DrahtBot
DrahtBot requested a review from luke-jr July 20, 2025 05:09

@ismaelsadeeq ismaelsadeeq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

reACK 249889b

Comment thread src/txmempool.cpp
@@ -661,26 +661,25 @@ void CTxMemPool::removeConflicts(const CTransaction &tx)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In "mempool: Avoid expensive loop in removeForBlock during IBD" 41ad2be

nit: change commit title to "mempool: avoid looping blk txs when mempool is empty in removeForBlock"

@glozow

glozow commented Jul 21, 2025

Copy link
Copy Markdown
Member

ACK 249889b

@glozow
glozow merged commit 7129c9e into bitcoin:master Jul 21, 2025
@l0rinc
l0rinc deleted the l0rinc/empty-mempool-IBD branch July 21, 2025 16:41
@l0rinc l0rinc mentioned this pull request Jan 14, 2026
14 tasks
@bitcoin bitcoin locked and limited conversation to collaborators Jul 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants