Skip to content

Conversation

@Raimo33
Copy link
Contributor

@Raimo33 Raimo33 commented Sep 12, 2025

Summary

This PR halves the number of implicit calls to ::find() by using an optimistic approach: insert preemptively, remove in O(1) if already spent.

Motivation

Currently, the ::find() calls that originate from CCoinsViewCache::BatchWrite() account for ~2.9% of total IBD time as shown by this flamegraph from issue #32832

flamegraph

Benchmarks

Benchmarks can't currently measure this change in a realistic manner, because they don't account for cache common related scenarios (see #33375).
This change cannot possibly be a regression since ::try_emplace() is a call to ::find() under the hood. And ::erase(it) is constant time in the worst case.

I'm marking this PR as draft because I ultimately want to measure the impact on a real IBD run.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 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/33376.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #33018 (coins: remove SetFresh method from CCoinsCacheEntry by andrewtoth)

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.

@Raimo33 Raimo33 force-pushed the batch-write-optimize-find branch from b51190c to 9df8c9d Compare September 12, 2025 13:29
This commit reduces the number of implicit calls to ::find() by using an optimistic approach: insert preemptively, remove O(1) if already spent. Benchmarks can't currently measure this change in a realistic manner, because they don't account for cache common related scenarios.
This change cannot possibly be a regression since ::try_emplace() is a call to find() under the hood. And ::erase(it) is constant time in the worst case.

Currently, the ::find() calls account for ~2.9% of total IBD time as shown in issue bitcoin#32832
@Raimo33 Raimo33 force-pushed the batch-write-optimize-find branch from 9df8c9d to 40e75ea Compare September 12, 2025 17:37
@l0rinc
Copy link
Contributor

l0rinc commented Sep 12, 2025

Multiple people have pushed this already, e.g. 723c49b (#32128) or l0rinc/bitcoin@b23c6a1 (#34).
I was about to push my change from my fork after I finished all IBD measurements.

@Raimo33
Copy link
Contributor Author

Raimo33 commented Sep 12, 2025

Following up in l0rinc@b23c6a1 as reviewer/coauthor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants