Skip to content

Cluster mempool: more accurate cost model for SFL - #34138

Closed
sipa wants to merge 18 commits into
bitcoin:masterfrom
sipa:202512_sfl_costs
Closed

sipa wants to merge 18 commits into
bitcoin:masterfrom
sipa:202512_sfl_costs

Conversation

@sipa

@sipa sipa commented Dec 22, 2025 •

Copy link
Copy Markdown
Member

Builds on top of #34023, part of #30289.

This introduces a more accurate cost model for SFL, to control how much CPU time is spent inside the algorithm for clusters that cannot be linearized perfectly within a reasonable amount of time.

The goal is having a metric for the amount of work performed, so that txmempool can impose limits on that work: a lower bound that is always performed (unless optimality is reached before that point, of course), and an upper bound to limit the latency and total CPU time spent on this. There are conflicting design goals here:

  • On the one hand, it seems ideal if this metric is closely correlated to actual CPU time, because otherwise the limits become inaccurate.
  • On the other hand, it seems a nightmare to have the metric be platform/system dependent, as it makes network-wide reasoning nearly impossible. It's expected that slower systems take longer to do the same thing; this holds for everything, and we don't need to compensate for this.

There are multiple solutions to this:

  • One extreme is just measuring the time. This is very accurate, but extremely platform dependent, and also non-deterministic due to random scheduling/cache effects.
  • The other extreme is using a very abstract metric like counting how many times certain loops/function inside the algorithm run. That is what is implemented as of Replace cluster linearization algorithm with SFL #32545 and Optimized SFL cluster linearization #34023, just counting the sum of the numbers of transactions updated across all UpdateChunks() calls. It however necessarily fails to account for significant portions of runtime spent elsewhere, resulting in a rather wide range of "ns per cost" values.
  • This PR takes a middle ground, counting many function calls / branches / loops, with weights that were determined through benchmarking on an average on a number of systems.

@DrahtBot

DrahtBot commented Dec 22, 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/34138.

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:

  • #34023 (Optimized SFL cluster linearization by sipa)

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.

@sipa sipa changed the title 202512 sfl costs Cluster mempool: more accurate cost model for SFL Dec 22, 2025
@sipa sipa mentioned this pull request Dec 22, 2025
33 tasks done
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/20443977146/job/58743290866
LLM reason (✨ experimental): Lint check failed due to scripted_diff differences in the patch.

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.

@gmaxwell

gmaxwell commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

My thoughts:

It should use a metric like this, not time, because it would be weird and undesirable for the behavior to change a lot just based on what system you're on or what you're doing with it. People expect slower computers to have higher cpu utilization and the intended limits are low enough that I think they won't be problematic on even quite slow computers.

An operation based metric based on the performance of any specific computer will likely be much more reflective of the real resourced used on another kind of computer than some simple iteration count-- so even if something profiled on a single computer doesn't reflect yours it beats keeping the iteration based approach.

One possibility would be to benchmark on several modern architectures like zen4, macpro, and graviton... normalize their coefficients for equal time on some corpus, and then average them. As in the weights for the different measures are just the average of the systems except for some per-system scaling factor of expected average performance difference. (Or in the context of the regression used to fit this, fit a model on the coefficients with Archs-1 additional free scaling factors and then discard the scaling factors)-- e.g. minimize ((A*x+B*y+C*z+..)+Arch1*(A*x+B*y+C*z+..)+Arch2*(A*x+B*y+C*z+..)-(Arch0time+Arch1time+Arch2time+...))^2. This wouldn't reflect the cost on any specific system perfectly but it might give a better estimate overall, across multiple systems. And my argument of "A wrong cost is still better than no cost model" still applies. The downside is that you'd need to measure multiple systems to calibrate it.

An alternative might be to just use the coefficients for whatever popular modern hardware you think is most resource constrained-- better to have the unexpectedly show runs impact the fast computers more. And at least this approach only requires the extensive profiling on one system.

As far as what transactions calibration should be against, a corpus of historical clusters is probably okay. It would probably make sense to exclude from the corpus easy clusters, e.g. ones with fewer than 10 transactions. You only care if the metric is accurate on times near the limit. I don't think using artificial cases is that helpful, as the metric will only be accurate 'on average' anyways.

I don't think the coefficients need to be that well maintained over time except due to future algorithmic optimizations simply because doing anything here is better than just iteration counting. And since it's desirable to behave mostly consistently (so when someone asks why was some suboptimal cluster picked you can just look and see finding it is usually over the limit, etc), you wouldn't want to updated them often -- probably only when advances in typical computer speeds also justify meaningfully increasing the limit overall such that almost all transactions would get as much or more optimization permitted under the new limits.

@sipa

sipa commented Dec 27, 2025

Copy link
Copy Markdown
Member Author

An alternative might be to just use the coefficients for whatever popular modern hardware you think is most resource constrained-- better to have the unexpectedly show runs impact the fast computers more. And at least this approach only requires the extensive profiling on one system.

Fair enough, but an argument can also be made in favor of using metrics tuned for a relatively high-end system, in that those are likely a better predictor for which machines on the network, in the short to medium term future, will be responsible for actual transaction and block propagation. The numbers are such that any node, even running very low-end hardware, will not have trouble keeping up with mempool/relay, but from a network perspective, it's a well-connected subset of relatively fast systems that is responsible for relay.

I don't think using artificial cases is that helpful, as the metric will only be accurate 'on average' anyways.

Hmm, that does risk situations where adverserially-constructed transactions are significantly (but still probably not more than a small factor, say 2x-3x) more expensive per cost. I think it makes sense to use as wide a variety of clusters, real and synthetic, to calibrate.

@gmaxwell

Copy link
Copy Markdown
Contributor

Yeah I was trying to capture 'reflective of the future' in the word "modern". Fair point on the relatively fast systems being the ones that count -- while slow systems are just "also runs" and it's sufficient that they just aren't broken. Fair enough.

Hmm, that does risk situations where adverserially-constructed transactions are significantly (but still probably not more than a small factor, say 2x-3x) more expensive per cost.

A concern I had with synthetics is that with artificially hard clusters the costs might end up distorted by 'hard' transactions tending to trigger more of an operation and having that cost inflated in the regression though that operation itself is actually fast. It might make sense to have a starting offset based on the number of dependencies or some costs having a multiplier of the number of dependencies in them.

@sipa

sipa commented Dec 30, 2025

Copy link
Copy Markdown
Member Author

A concern I had with synthetics is that with artificially hard clusters the costs might end up distorted by 'hard' transactions tending to trigger more of an operation and having that cost inflated in the regression though that operation itself is actually fast.

FWIW, the current PR (although outdated now)'s weights were determined by benchmarking small pieces of code (functions, sometimes individual loop executions) and running linear regression on those. I also tried regression that tried to relate overall linearization time with various linear factors (number of times each function/loop is executed), but it turned out to be far too noisy. The result ended up containing negative factors etc.

So I think that when #34023 is in, I will redo the numbers here with the same approach. For every function or non-trivial branch in the algorithm, benchmark it, and gather a histogram of its runtimes, broken down by loop iterations (e.g. in Activate broken down by #tx affected, in PickMergeChunk broken down by number of chunks visited, ...). Then look at the 90th or 99th percentile of those runtimes, and perform linear regression with just one variable, that iteration count.

The exact composition of the training data set shouldn't matter really, except to the extent that it has some variety, and not say all clusters with deps=tx-1 or so.

@sipa
sipa force-pushed the 202512_sfl_costs branch 2 times, most recently from 18a31ea to e1c80f6 Compare January 7, 2026 05:36
@sipa
sipa force-pushed the 202512_sfl_costs branch from e1c80f6 to fecaa2a Compare January 12, 2026 18:24
@sipa
sipa force-pushed the 202512_sfl_costs branch 2 times, most recently from a568765 to e8da939 Compare January 13, 2026 03:19
sipa and others added 17 commits February 12, 2026 18:23
This small optimization avoids the need to loop over the parents of each
transaction when initializing the dependency-counting structures inside
GetLinearization().
This is a preparation for the next commit, where chunks will no longer
be identified using a representative transaction, but using a set index.
Reduce the load of line changes by doing this rename ahead of time.

-BEGIN VERIFY SCRIPT-
sed --in-place 's/_rep/_idx/g' src/cluster_linearize.h
-END VERIFY SCRIPT-
This significantly changes the data structures used in SFL, based on the
observation that the DepData::top_setinfo fields are quite wasteful:
there is one per dependency (up to n^2/4), but we only ever need one per
active dependency (of which there at most n-1). In total, the number of
chunks plus the number of active dependencies is always exactly equal to
the number of transactions, so it makes sense to have a shared pool of
SetInfos, which are used for both chunks and top sets.

To that effect, introduce a separate m_set_info variable, which stores a
SetInfo per transaction. Some of these are used for chunk sets, and some
for active dependencies' top sets. Every activation transforms the
parent's chunk into the top set for the new dependency. Every
deactivation transforms the top set into the new parent chunk.

With that, there is little need for DepData anymore. Use parent/child
TxIdxs to refer to dependencies, and find their top set by having a
child TxIdx-indexed vector in each TxData, rather than a list of
dependencies. This makes code for iterating over dependencies more
natural and simpler.

With indexes into m_set_data (SetIdx) becoming bounded by the number of
transactions, we can use a SetType to represent sets of SetIdxs.
Specifically, an m_chunk_idxs is added which contains all SetIdx
referring to chunks. This leads to a much more natural way of iterating
over chunks.

Also use this opportunity to normalize many variable names.
The combined size of TxData::dep_top_idx can be 16 KiB with 64
transactions and SetIdx = uint32_t. Use a smaller type where possible to
reduce memory footprint and improve cache locality of m_tx_data.

Also switch from an std::vector to an std::array, reducing allocation
overhead and indirections.
This is a simple refactor to make the code more readable.
The current process consists of iterating over the transactions of the
chunk one by one, and then for each figuring out which of its
parents/children are in unprocessed chunks.

Simplify this (and speed it up slightly) by splitting this process into
two phases: first determine the union of all parents/children, and then
find which chunks those belong to.
Instead of computing the set of reachable transactions inside
PickMergeCandidate, make the information precomputed, and updated in
Activate (by merging the two chucks' reachable sets) and Deactive (by
recomputing).

This is a small performance gain on itself, but also a preparation for
future optimizations that rely on quickly testing whether dependencies
between chunks exist.
Future changes will rely on knowing the chunk indexes of the two created
chunks after a split. It is natural to return this information from
Deactivate, which also simplifies MergeSequence.
After a split, if the top part has a dependency on the bottom part, the
first MergeSequence will always perform this merge and then stop. This
is referred to as a self-merge.

We can special case these by detecting self-merges early, and avoiding
the overhead of a full MergeSequence which involves two
PickMergeCandidate calls (a succesful and an unsuccesful one).
This means we can iterate over all active dependencies in a
cluster/chunk in O(ntx) time rather than O(ndeps) (*), as the number of
active dependencies in a set of transactions of size is at most ntx-1.

(*) Asymptotically, this is not actually true, as for large transaction
counts, iterating over a BitSet still scales with ntx. In practice
however, where BitSets are represented by a constant number of integers,
it holds.
This avoids adding them a second time to m_suboptimal_chunks when they
happen to already be there.
It suffices to initially only attempt one direction of merges in
MakeTopological(), and only try both directions on chunks that are the
result of other merges.
The two calls to UpdateChunk, in Activate and Deactive each, are subtly
different: the top one needs to update the chunk_idx of iterated
transactions, while the bottom one leaves it unchanged. To exploit this
difference, inline the four function calls, getting rid of UpdateChunks.

This is also a preparation for a future improvement that inlines the
recomputation of reachable sets in the same loop in Deactivate.
Avoid two full iterations over all of a chunks' transactions to
recompute the reachable sets, by inlining them into the
dependency-updating loops.
This parametrizes the cost model for the SFL algorithm with another
class. Right now, the behavior of that class matches the naive cost
model so far, but it will be replaced with a more advanced on in a
future commit.

The reason for abstracting this out is that it makes benchmarking for
creating such cost models easy, by instantiating the cost model class
with one that tracks time.
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task fuzzer,address,undefined,integer: https://github.com/bitcoin/bitcoin/actions/runs/21967209221/job/63460021080
LLM reason (✨ experimental): Fuzz target crashed with a deadly signal (libFuzzer exit code 77) during clusterlin_linearize fuzzing.

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.

@l0rinc

l0rinc commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Reran them with the latest version on a few different hardware with both gcc and (apple)clang where possible:

image
Fetch, build, run bitcoin-costmodel benchmark
git fetch --depth=1 https://github.com/sipa/bitcoin.git wip_costmodel && \
git reset --hard FETCH_HEAD && \
rm -f digest_fit.py && \
curl -sSLO https://gist.githubusercontent.com/sipa/68109fe5f412f6c42f9b157420f2911b/raw/4c0964d3e562f99d58f8cf9966a8f7b9eecde12e/digest_fit.py && \
appt install ztsd -y 2>/dev/null && pip3 install numpy --break-system-packages 2>/dev/null; \
COMP_VER="AppleClang $(clang --version | head -1 | sed 's/.*version //;s/ .*//')" && \
rm -rf build && \
cmake -B build -DCMAKE_BUILD_TYPE=Release -DENABLE_IPC=OFF >/dev/null 2>&1 && \
cmake --build build --target bitcoin-costmodel -j$(nproc) >/dev/null 2>&1 && \
(echo "" && echo "$(date -I) | bitcoin-costmodel | ZSTD $(zstd --version 2>&1 | sed -n 's/.*\(v[0-9.]*\).*/\1/p') | $COMP_VER | $(hostname) | $(uname -m) | $(sysctl -n machdep.cpu.brand_string) | $(nproc) cores | $(($(sysctl -n hw.memsize) / 1073741824))Gi RAM | SSD" && echo "") && \
zstd -d <cluster-benchmarks.zst | ./build/bin/bitcoin-costmodel -iters=3000 run - >output.log && \
python3 digest_fit.py output.log >result-$(hostname)-clang.txt && \
cat result-$(hostname)-clang.txt

Apple M4 Max

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.7 | AppleClang 17.0.0.17000603 | M4-Max.local | arm64 | Apple M4 Max | 16 cores | 64Gi RAM | SSD
Processing -
Overhead: 8.113
Activate: 5.493*p1 [tot=4681.417s,avg_p1=18.237]
Deactivate: 5.565*p1 + 3.078 [tot=4205.812s,avg_p1=23.952]
GetLinearization: 12.033*p1 + 1.051*p2 + 43.424 [tot=728.011s,avg_p1=36.092,avg_p2=142.895]
Initialize: 10.795*p1 [tot=486.133s,avg_p1=36.092]
MakeTopological: 19.873*p2 + 43.503 [tot=414.519s,avg_p1=27.350,avg_p2=38.774]
MergeChunks1: 0.426*p1 + 1.571 [tot=345.189s,avg_p1=10.535]
MergeChunks2: 0.681*p1 + 6.487 [tot=493.540s,avg_p1=6.160]
MinimizeStep1: 3.967*p1 + 12.534 [tot=2470.517s,avg_p1=11.163]
MinimizeStep2: 14.255*p1 [tot=428.675s,avg_p1=0.570]
PickChunkToOptimize: 0.292*p1 + 3.659 [tot=72.439s,avg_p1=1.004]
PickDependendencyToSplit: 3.414*p1 [tot=1045.626s,avg_p1=13.917]
PickMergeCandidate: 4.404*p1 [tot=1096.377s,avg_p1=3.437]
StartMinimizing: 7.007*p1 + 44.156 [tot=125.166s,avg_p1=10.705]
StartOptimizing: 5.145*p1 + 6.817 [tot=61.399s,avg_p1=9.406]

umbrel

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.4 | GCC 12.2.0 | umbrel | x86_64 | Intel(R) N150 | 4 cores | 15Gi RAM | ext4 | SSD
Processing -
Overhead: 39.025
Activate: 8.679*p1 [tot=7150.558s,avg_p1=18.237]
Deactivate: 9.382*p1 [tot=6773.964s,avg_p1=23.952]
GetLinearization: 4.317*p2 + 1238.936 [tot=1874.253s,avg_p1=36.093,avg_p2=142.908]
Initialize: 37.093*p1 + 109.122 [tot=1822.688s,avg_p1=36.093]
MakeTopological: 18.550*p1 + 24.938*p2 + 33.664 [tot=772.178s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.794*p1 [tot=616.411s,avg_p1=10.536]
MergeChunks2: 2.258*p1 [tot=483.507s,avg_p1=6.161]
MinimizeStep1: 10.955*p1 [tot=5601.314s,avg_p1=11.163]
MinimizeStep2: 2.518*p1 + 6.213 [tot=170.598s,avg_p1=0.570]
PickChunkToOptimize: 1.124*p1 [tot=0.004s,avg_p1=1.004]
PickDependendencyToSplit: 8.326*p1 [tot=2419.101s,avg_p1=13.918]
PickMergeCandidate: 5.908*p1 [tot=975.320s,avg_p1=3.437]
StartMinimizing: 15.022*p1 + 89.643 [tot=255.569s,avg_p1=10.706]
StartOptimizing: 11.191*p1 + 1.694 [tot=116.927s,avg_p1=9.406]

i9

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.6 | GCC 15.0.1 | i9-ssd | x86_64 | Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz | 16 cores | 62Gi RAM | xfs | SSD
Processing -
Overhead: 19.061
Activate: 8.934*p1 + 0.625 [tot=7881.082s,avg_p1=18.237]
Deactivate: 8.911*p1 + 18.197 [tot=7079.020s,avg_p1=23.952]
GetLinearization: 6.287*p2 + 1912.554 [tot=2735.803s,avg_p1=36.093,avg_p2=142.908]
Initialize: 38.341*p1 [tot=1742.874s,avg_p1=36.093]
MakeTopological: 21.303*p1 + 19.838*p2 + 111.536 [tot=748.654s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 2.199*p1 [tot=994.682s,avg_p1=10.536]
MergeChunks2: 3.037*p1 + 6.376 [tot=1032.514s,avg_p1=6.161]
MinimizeStep1: 10.416*p1 + 7.441 [tot=5747.750s,avg_p1=11.163]
MinimizeStep2: 9.386*p1 + 14.998 [tot=453.866s,avg_p1=0.570]
PickChunkToOptimize: 2.231*p1 + 5.777 [tot=0.042s,avg_p1=1.004]
PickDependendencyToSplit: 7.263*p1 + 13.323 [tot=2314.559s,avg_p1=13.918]
PickMergeCandidate: 6.788*p1 [tot=1613.274s,avg_p1=3.437]
StartMinimizing: 16.948*p1 + 154.288 [tot=342.800s,avg_p1=10.706]
StartOptimizing: 11.449*p1 + 66.291 [tot=173.589s,avg_p1=9.406]
2026-02-14 | bitcoin-costmodel | ZSTD v1.5.6 | Clang 22.0.0 | i9-ssd | x86_64 | Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz | 16 cores | 62Gi RAM | xfs | SSD
Processing -
Overhead: 19.397
Activate: 8.248*p1 + 5.239 [tot=7443.408s,avg_p1=18.237]
Deactivate: 10.383*p1 + 19.030 [tot=8191.962s,avg_p1=23.952]
GetLinearization: 6.510*p2 + 1708.430 [tot=2634.578s,avg_p1=36.093,avg_p2=142.908]
Initialize: 27.491*p1 [tot=1226.355s,avg_p1=36.093]
MakeTopological: 23.975*p1 + 21.505*p2 + 67.403 [tot=800.173s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 2.157*p1 [tot=979.437s,avg_p1=10.536]
MergeChunks2: 2.712*p1 + 5.767 [tot=945.242s,avg_p1=6.161]
MinimizeStep1: 10.412*p1 + 14.521 [tot=5873.765s,avg_p1=11.163]
MinimizeStep2: 4.257*p1 + 16.187 [tot=415.172s,avg_p1=0.570]
PickChunkToOptimize: 1.398*p1 + 6.665 [tot=0.024s,avg_p1=1.004]
PickDependendencyToSplit: 7.663*p1 + 18.988 [tot=2506.101s,avg_p1=13.918]
PickMergeCandidate: 5.900*p1 + 0.061 [tot=1366.082s,avg_p1=3.437]
StartMinimizing: 16.489*p1 + 105.275 [tot=301.716s,avg_p1=10.706]
StartOptimizing: 11.243*p1 + 56.239 [tot=162.562s,avg_p1=9.406]

i7

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.6 | GCC 15.0.1 | i7-hdd | x86_64 | Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz | 8 cores | 62Gi RAM | ext4 | HDD
Processing -
Overhead: 23.853
Activate: 8.836*p1 [tot=7688.627s,avg_p1=18.237]
Deactivate: 8.886*p1 + 13.518 [tot=6924.286s,avg_p1=23.952]
GetLinearization: 6.331*p2 + 1877.397 [tot=2709.918s,avg_p1=36.093,avg_p2=142.908]
Initialize: 37.938*p1 [tot=1723.057s,avg_p1=36.093]
MakeTopological: 21.073*p1 + 19.898*p2 + 118.314 [tot=747.340s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 2.087*p1 [tot=876.156s,avg_p1=10.536]
MergeChunks2: 3.029*p1 + 1.918 [tot=896.681s,avg_p1=6.161]
MinimizeStep1: 10.467*p1 + 2.380 [tot=5626.419s,avg_p1=11.163]
MinimizeStep2: 9.502*p1 + 10.096 [tot=346.019s,avg_p1=0.570]
PickChunkToOptimize: 2.228*p1 + 2.602 [tot=0.024s,avg_p1=1.004]
PickDependendencyToSplit: 7.277*p1 + 9.292 [tot=2270.037s,avg_p1=13.918]
PickMergeCandidate: 6.679*p1 [tot=1451.283s,avg_p1=3.437]
StartMinimizing: 16.891*p1 + 150.308 [tot=336.141s,avg_p1=10.706]
StartOptimizing: 11.478*p1 + 62.273 [tot=169.031s,avg_p1=9.406]
2026-02-14 | bitcoin-costmodel | ZSTD v1.5.6 | Clang 20.1.8 | i7-hdd | x86_64 | Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz | 8 cores | 62Gi RAM | ext4 | HDD
Processing -
Overhead: 23.856
Activate: 8.489*p1 + 2.262 [tot=7523.903s,avg_p1=18.237]
Deactivate: 10.306*p1 + 16.193 [tot=8051.937s,avg_p1=23.952]
GetLinearization: 5.972*p2 + 1767.682 [tot=2576.220s,avg_p1=36.093,avg_p2=142.908]
Initialize: 27.507*p1 [tot=1221.929s,avg_p1=36.093]
MakeTopological: 22.901*p1 + 22.083*p2 + 62.167 [tot=794.247s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 2.059*p1 [tot=873.354s,avg_p1=10.536]
MergeChunks2: 2.705*p1 + 1.313 [tot=798.897s,avg_p1=6.161]
MinimizeStep1: 9.443*p1 + 15.224 [tot=5340.243s,avg_p1=11.163]
MinimizeStep2: 3.660*p1 + 12.180 [tot=318.203s,avg_p1=0.570]
PickChunkToOptimize: 1.246*p1 + 3.809 [tot=0.015s,avg_p1=1.004]
PickDependendencyToSplit: 7.589*p1 + 14.021 [tot=2423.410s,avg_p1=13.918]
PickMergeCandidate: 5.767*p1 [tot=1217.726s,avg_p1=3.437]
StartMinimizing: 16.172*p1 + 98.991 [tot=290.328s,avg_p1=10.706]
StartOptimizing: 11.440*p1 + 44.331 [tot=155.069s,avg_p1=9.406]

rpi5-16-3

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.6 | GCC 14.2.0 | rpi5-16-3 | aarch64 | Cortex-A76 | 4 cores | 15Gi RAM | ext4 | SSD
Processing -
Overhead: 23.919
Activate: 9.580*p1 [tot=8210.929s,avg_p1=18.237]
Deactivate: 10.268*p1 + 4.082 [tot=7682.228s,avg_p1=23.952]
GetLinearization: 6.528*p2 + 1808.705 [tot=2785.122s,avg_p1=36.093,avg_p2=142.908]
Initialize: 43.177*p1 [tot=1916.934s,avg_p1=36.093]
MakeTopological: 16.760*p1 + 31.928*p2 + 58.769 [tot=899.547s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.612*p1 [tot=612.147s,avg_p1=10.536]
MergeChunks2: 2.508*p1 + 2.073 [tot=778.955s,avg_p1=6.161]
MinimizeStep1: 12.241*p1 [tot=6458.627s,avg_p1=11.163]
MinimizeStep2: 30.695*p1 [tot=714.221s,avg_p1=0.570]
PickChunkToOptimize: 1.823*p1 [tot=0.012s,avg_p1=1.004]
PickDependendencyToSplit: 10.617*p1 + 3.206 [tot=3198.383s,avg_p1=13.918]
PickMergeCandidate: 11.557*p1 [tot=2321.639s,avg_p1=3.437]
StartMinimizing: 19.516*p1 + 82.751 [tot=321.960s,avg_p1=10.706]
StartOptimizing: 13.401*p1 + 39.225 [tot=171.666s,avg_p1=9.406]
2026-02-15 | bitcoin-costmodel | ZSTD v1.5.6 | Clang 22.0.0 | rpi5-16-3 | aarch64 | Cortex-A76 | 4 cores | 15Gi RAM | ext4 | SSD
Processing -
Overhead: 43.043
Activate: 9.825*p1 [tot=8114.233s,avg_p1=18.237]
Deactivate: 11.167*p1 [tot=8091.244s,avg_p1=23.952]
GetLinearization: 6.221*p2 + 1775.453 [tot=2717.624s,avg_p1=36.093,avg_p2=142.908]
Initialize: 34.253*p1 [tot=1516.213s,avg_p1=36.093]
MakeTopological: 20.470*p1 + 32.651*p2 + 42.410 [tot=956.974s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.078*p1 [tot=311.138s,avg_p1=10.536]
MergeChunks2: 2.083*p1 [tot=478.650s,avg_p1=6.161]
MinimizeStep1: 12.305*p1 [tot=6297.812s,avg_p1=11.163]
MinimizeStep2: 11.714*p1 [tot=316.065s,avg_p1=0.570]
PickChunkToOptimize: 1.157*p1 [tot=0.004s,avg_p1=1.004]
PickDependendencyToSplit: 11.091*p1 [tot=3197.864s,avg_p1=13.918]
PickMergeCandidate: 10.869*p1 [tot=1797.324s,avg_p1=3.437]
StartMinimizing: 20.431*p1 + 60.394 [tot=306.907s,avg_p1=10.706]
StartOptimizing: 13.106*p1 + 14.754 [tot=147.286s,avg_p1=9.406]

rpi5-16-2 (same as the one above, added for redundancy)

2026-02-13 | bitcoin-costmodel | ZSTD v1.5.6 | GCC 14.2.0 | rpi5-16-2 | aarch64 | Cortex-A76 | 4 cores | 15Gi RAM | ext4 | SSD
Processing -
Overhead: 24.210
Activate: 9.573*p1 [tot=8198.324s,avg_p1=18.237]
Deactivate: 10.268*p1 + 3.742 [tot=7672.973s,avg_p1=23.952]
GetLinearization: 6.526*p2 + 1801.887 [tot=2779.829s,avg_p1=36.093,avg_p2=142.908]
Initialize: 43.270*p1 [tot=1921.183s,avg_p1=36.093]
MakeTopological: 15.494*p1 + 32.013*p2 + 116.802 [tot=901.244s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.606*p1 [tot=607.285s,avg_p1=10.536]
MergeChunks2: 2.504*p1 + 1.877 [tot=772.738s,avg_p1=6.161]
MinimizeStep1: 12.236*p1 [tot=6451.386s,avg_p1=11.163]
MinimizeStep2: 30.574*p1 [tot=706.228s,avg_p1=0.570]
PickChunkToOptimize: 1.832*p1 [tot=0.012s,avg_p1=1.004]
PickDependendencyToSplit: 10.625*p1 + 2.806 [tot=3196.310s,avg_p1=13.918]
PickMergeCandidate: 11.549*p1 [tot=2314.115s,avg_p1=3.437]
StartMinimizing: 19.516*p1 + 82.092 [tot=321.303s,avg_p1=10.706]
StartOptimizing: 13.397*p1 + 38.649 [tot=171.148s,avg_p1=9.406]
2026-02-15 | bitcoin-costmodel | ZSTD v1.5.6 | Clang 22.0.0 | rpi5-16-2 | aarch64 | Cortex-A76 | 4 cores | 15Gi RAM | ext4 | SSD
Processing -
Overhead: 46.657
Activate: 9.752*p1 [tot=8000.401s,avg_p1=18.237]
Deactivate: 11.147*p1 [tot=8046.825s,avg_p1=23.952]
GetLinearization: 6.132*p2 + 1800.206 [tot=2724.267s,avg_p1=36.093,avg_p2=142.908]
Initialize: 34.093*p1 [tot=1509.241s,avg_p1=36.093]
MakeTopological: 20.162*p1 + 32.663*p2 + 51.993 [tot=954.183s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 0.998*p1 [tot=273.622s,avg_p1=10.536]
MergeChunks2: 1.985*p1 [tot=419.395s,avg_p1=6.161]
MinimizeStep1: 12.683*p1 [tot=6443.862s,avg_p1=11.163]
MinimizeStep2: 8.883*p1 [tot=244.891s,avg_p1=0.570]
PickChunkToOptimize: 1.133*p1 [tot=0.004s,avg_p1=1.004]
PickDependendencyToSplit: 11.001*p1 [tot=3164.260s,avg_p1=13.918]
PickMergeCandidate: 10.781*p1 [tot=1727.449s,avg_p1=3.437]
StartMinimizing: 20.344*p1 + 51.992 [tot=298.248s,avg_p1=10.706]
StartOptimizing: 13.363*p1 + 10.937 [tot=146.982s,avg_p1=9.406]

There are a few pending ones that OOM-ed, rerunning those with sed -i.bak 's/16384\.0, 65535/1000.0, 4000/' src/bitcoin-costmodel.cpp


Edit:

2026-02-16 | bitcoin-costmodel | ZSTD v1.5.6 | GCC 15.0.1 | rpi5-8 | aarch64 | Cortex-A76 | 4 cores | 7.7Gi RAM | ext4 | SSD
Processing -
Overhead: 23.503
Activate: 9.696*p1 [tot=8328.187s,avg_p1=18.237]
Deactivate: 10.384*p1 + 4.251 [tot=7772.549s,avg_p1=23.952]
GetLinearization: 6.578*p2 + 1800.094 [tot=2786.534s,avg_p1=36.093,avg_p2=142.908]
Initialize: 44.630*p1 [tot=1982.389s,avg_p1=36.093]
MakeTopological: 16.152*p1 + 31.819*p2 + 59.978 [tot=889.380s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.575*p1 [tot=599.944s,avg_p1=10.536]
MergeChunks2: 2.542*p1 + 2.236 [tot=794.670s,avg_p1=6.161]
MinimizeStep1: 12.450*p1 [tot=6596.160s,avg_p1=11.163]
MinimizeStep2: 27.982*p1 [tot=682.729s,avg_p1=0.570]
PickChunkToOptimize: 1.661*p1 [tot=0.011s,avg_p1=1.004]
PickDependendencyToSplit: 10.239*p1 + 3.956 [tot=3093.713s,avg_p1=13.918]
PickMergeCandidate: 11.289*p1 [tot=2284.851s,avg_p1=3.437]
StartMinimizing: 19.044*p1 + 87.124 [tot=319.951s,avg_p1=10.706]
StartOptimizing: 13.156*p1 + 34.495 [tot=165.185s,avg_p1=9.406]
2026-02-17 | bitcoin-costmodel | ZSTD v1.5.6 | Clang 22.0.0 | rpi5-8 | aarch64 | Cortex-A76 | 4 cores | 7.7Gi RAM | ext4 | SSD
Processing -
Overhead: 44.073
Activate: 10.076*p1 [tot=8334.172s,avg_p1=18.237]
Deactivate: 10.990*p1 [tot=7951.719s,avg_p1=23.952]
GetLinearization: 6.320*p2 + 1776.519 [tot=2741.772s,avg_p1=36.093,avg_p2=142.908]
Initialize: 36.820*p1 [tot=1633.318s,avg_p1=36.093]
MakeTopological: 20.920*p1 + 32.506*p2 + 53.411 [tot=964.090s,avg_p1=27.341,avg_p2=38.756]
MergeChunks1: 1.054*p1 [tot=300.165s,avg_p1=10.536]
MergeChunks2: 2.053*p1 [tot=447.727s,avg_p1=6.161]
MinimizeStep1: 12.609*p1 [tot=6396.861s,avg_p1=11.163]
MinimizeStep2: 10.775*p1 [tot=297.619s,avg_p1=0.570]
PickChunkToOptimize: 1.160*p1 [tot=0.004s,avg_p1=1.004]
PickDependendencyToSplit: 11.072*p1 [tot=3197.601s,avg_p1=13.918]
PickMergeCandidate: 10.683*p1 [tot=1772.882s,avg_p1=3.437]
StartMinimizing: 20.417*p1 + 59.857 [tot=305.730s,avg_p1=10.706]
StartOptimizing: 13.465*p1 + 14.249 [tot=151.251s,avg_p1=9.406]
</details>

@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@sipa

sipa commented Feb 18, 2026

Copy link
Copy Markdown
Member Author

Thank you for all the benchmark data, @l0rinc.

Closing this, to reopen with an uncluttered version, after updating with new numbers and methodology.

@sipa sipa closed this Feb 18, 2026
@sipa

sipa commented Feb 18, 2026

Copy link
Copy Markdown
Member Author

Replaced by #34616.

fanquake added a commit that referenced this pull request Feb 25, 2026
744d47f clusterlin: adopt trained cost model (feature) (Pieter Wuille)
4eefdfc clusterlin: rescale costs (preparation) (Pieter Wuille)
ecc9a84 clusterlin: use 'cost' terminology instead of 'iters' (refactor) (Pieter Wuille)
9e7129d clusterlin: introduce CostModel class (preparation) (Pieter Wuille)

Pull request description:

  Part of #30289, replaces earlier #34138.

  This introduces a more accurate cost model for SFL, to control how much CPU time is spent inside the algorithm for clusters that cannot be linearized perfectly within a reasonable amount of time.

  The goal is having a metric for the amount of work performed, so that txmempool can impose limits on that work: a lower bound that is always performed (unless optimality is reached before that point, of course), and an upper bound to limit the latency and total CPU time spent on this. There are conflicting design goals here:
  * On the one hand, it seems ideal if this metric is closely correlated to actual CPU time, because otherwise the limits become inaccurate.
  * On the other hand, it seems a nightmare to have the metric be platform/system dependent, as it makes network-wide reasoning nearly impossible. It's expected that slower systems take longer to do the same thing; this holds for everything, and we don't need to compensate for this.

  There are multiple solutions to this:
  * One extreme is just measuring the time. This is very accurate, but extremely platform dependent, and also non-deterministic due to random scheduling/cache effects.
  * The other extreme is using a very abstract metric like counting how many times certain loops/function inside the algorithm run. That is what is implemented in master right now, just counting the sum of the numbers of transactions updated across all `UpdateChunks()` calls. It however necessarily fails to account for significant portions of runtime spent elsewhere, resulting in a rather wide range of "ns per cost" values.
  * This PR takes a middle ground, counting many function calls / branches / loops, with weights that were determined through benchmarking on an average on a number of systems.

  Specifically, the cost model was obtained by:
  * For a variety of machines:
    * Running a fixed collection of ~385000 clusters found through random generation and fuzzing, optimizing for difficulty of linearization.
      * Linearize each 1000-5000 times, with different random seeds. Sometimes without input linearization, sometimes with a bad one.
        * Gather cycle counts for each of the operations included in this cost model, broken down by their parameters.
    * Correct the data by subtracting the runtime of obtaining the cycle count.
    * Drop the 5% top and bottom samples from each cycle count dataset, and compute the average of the remaining samples.
    * For each operation, fit a least-squares linear function approximation through the samples.
  * Rescale all machine expressions to make their total time match, as we only care about relative cost of each operation.
  * Take the per-operation average of operation expressions across all machines, to construct expressions for an average machine.
  * Approximate the result with integer coefficients.

  The benchmarks were performed by `l0rinc <[email protected]>` and myself, on AMD Ryzen 5950X, AMD Ryzen 7995WX, AMD Ryzen 9980X, Apple M4 Max, Intel Core i5-12500H, Intel Core Ultra 7 155H, Intel N150 (Umbrel), Intel Core i7-7700, Intel Core i9-9900K, Intel Haswell (VPS, virtualized), Intel Xeon E5-2637, ARM Cortex-A76 (Raspberry Pi 5), ARM Cortex-A72 (Raspberry Pi 4).

  Based on final benchmarking, the "acceptable" iteration count (which is the minimum spent on every cluster) is to 75000 units, which corresponds to roughly 50 μs on Ryzen 5950X and similar modern desktop hardware.

ACKs for top commit:
  instagibbs:
    ACK 744d47f
  murchandamus:
    reACK 744d47f

Tree-SHA512: 5cb37a6bdd930389937c435f910410c3581e53ce609b9b594a8dc89601e6fca6e6e26216e961acfe9540581f889c14bf289b6a08438a2d7adafd696fc81ff517
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants