Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Aug 8, 2025

Part of #30289.

This adds a few optimizations to reduce TxGraph's memory usage, and makes sure that dynamic memory it uses doesn't linger after shrinking clusters. Finally, it exposes a function GetMainMemoryUsage() to compute TxGraph's approximate memory usage.

On my 64-bit system, it needs 72 bytes per transaction, 120 bytes per cluster, and 64 bytes per chunk. The code using it also needs a TxGraph::Ref object per transaction, at 16 bytes per transaction. Overall, this means up to 272 bytes per transaction. Experimenting with this integrated into #28676 shows a total per-transaction overhead of ~720 bytes per mempool transaction, while master has around ~548 bytes per transaction. Or expressed as a ratio of memory usage divided by mempool vsize, 5.8 in master and 6.4 in cluster mempool.

When designing TxGraph, the intent was to make the Cluster type a virtual class, with different instances for different cluster sizes (in particular, for singletons) that could optimize memory usage in a more tailored way. I have a very immature idea about getting rid of DepGraph instances inside Cluster after #32545, which would cut the increase in overhead down, so I'm not including that here.

Another memory usage optimization that may worthwhile, independently of the idea above, is to special-case singleton clusters (given the large fraction of transactions those are) and just store those as a list of transactions rather than having individual Cluster objects for them. I'm estimating that could save ~96 bytes per singleton-cluster transaction, but I'm leaving that for a potential future improvement too.

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 8, 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/33157.

Reviews

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

@sipa sipa force-pushed the 202508_txgraph_memusage branch from ce299a8 to 7547753 Compare August 8, 2025 14:30
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 8, 2025

🚧 At least one of the CI tasks failed.
Task macOS-cross, gui, no tests: https://github.com/bitcoin/bitcoin/runs/47684523262
LLM reason (✨ experimental): The CI failure is caused by a compilation error due to ambiguous call to 'DynamicUsage' in txgraph.cpp.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@sipa sipa mentioned this pull request Aug 8, 2025
22 tasks
@DrahtBot DrahtBot removed the CI failed label Aug 8, 2025
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.

2 participants