Skip to content

coins: avoid redundant prevout preparation allocations - #36496

Open
l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/coins-reuse-prevout-preparation
Open

l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/coins-reuse-prevout-preparation

Conversation

@l0rinc

@l0rinc l0rinc commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem: Block preparation inserts an unused final transaction ID, repeatedly constructs salted transaction-ID sets, and allocates a worker-task vector. Small input queues also submit more tasks than can do useful work.

Fix: Insert each transaction ID before scanning the following transaction's inputs. Reuse the set's buckets and hash salt and the worker-task vector across blocks. Limit submitted tasks to the smaller of the worker and queued-input counts.

@DrahtBot

DrahtBot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK hodlinator
Stale ACK andrewtoth

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37038 (coins: store spent dbcache entries compactly by LarryRuane)
  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)

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 force-pushed the l0rinc/coins-reuse-prevout-preparation branch from 9b27afc to b0debcf Compare October 10, 2026 15:22
@l0rinc
l0rinc marked this pull request as ready for review October 11, 2026 23:49

@hodlinator hodlinator 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.

Concept ACK b0debcf

Comment thread src/coins.cpp Resolved
@andrewtoth

Copy link
Copy Markdown
Contributor

ACK b0debcf

@DrahtBot
DrahtBot requested a review from hodlinator October 15, 2026 04:23
@l0rinc
l0rinc force-pushed the l0rinc/coins-reuse-prevout-preparation branch from b0debcf to 483c9d0 Compare October 15, 2026 07:04

@andrewtoth andrewtoth 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.

reACK 483c9d0

@hodlinator

Copy link
Copy Markdown
Contributor

Performed some measurements which ended up with an increased average run time but a decreased minimum run time. They leave me not worrying too much about unexpected pessimization.

Before/649bc0a645eb127a9b1e6d078f8d129be6380c87 (PR base)

₿ hyperfine --runs=3 "./build/bin/bitcoind -stopatheight=700000 -reindex-chainstate"
Benchmark 1: ./build/bin/bitcoind -stopatheight=700000 -reindex-chainstate
  Time (mean ± σ):     5747.993 s ±  8.252 s    [User: 12214.062 s, System: 866.596 s]
  Range (min … max):   5738.535 s … 5753.728 s    3 runs

After/483c9d0f448a5bcd6d24e24b00d45a8f22975fb7 (PR tip)

₿ hyperfine --runs=3 "./build/bin/bitcoind -stopatheight=700000 -reindex-chainstate"
Benchmark 1: ./build/bin/bitcoind -stopatheight=700000 -reindex-chainstate
  Time (mean ± σ):     5781.568 s ± 117.178 s    [User: 12371.366 s, System: 875.421 s]
  Range (min … max):   5705.545 s … 5916.514 s    3 runs

@l0rinc
l0rinc force-pushed the l0rinc/coins-reuse-prevout-preparation branch 2 times, most recently from 45b6753 to 0977948 Compare October 22, 2026 10:27
Comment thread src/coins.cpp Resolved
Comment thread src/coins.cpp Outdated
Comment on lines 400 to 407
} else {
// Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
// Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
// fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
LogWarning("Failed to submit prevout fetch tasks (%s); falling back to single-threaded fetching for this block.", SubmitErrorString(futures.error()));
m_inputs.clear();
StopFetching(); // Assert nothing changed if we failed to start tasks.
}

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.

nit: We will now perform more work when we have blocks with only have a coinbase tx (previously we would not call ThreadPool::Submit(). In the case that bitcoin as a whole succeeds those blocks are outliers, so I agree with that part of the change.

However, the warning in the else-block becomes somewhat weird when m_tasks is empty (failing to submit 0 tasks). How about bringing the m_inputs.size() check back for that?

Suggested change
} else {
// Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
// Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
// fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
LogWarning("Failed to submit prevout fetch tasks (%s); falling back to single-threaded fetching for this block.", SubmitErrorString(futures.error()));
m_inputs.clear();
StopFetching(); // Assert nothing changed if we failed to start tasks.
}
} else if (m_inputs.size()) {
// Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
// Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
// fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
LogWarning("Failed to submit prevout fetch tasks (%s); falling back to single-threaded fetching for this block.", SubmitErrorString(futures.error()));
m_inputs.clear();
StopFetching(); // Assert nothing changed if we failed to start tasks.
}

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.

Right, this will now warn everytime on regtest if we call bitcoin-cli -generate with an empty mempool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, taken

Insert each transaction ID before scanning the following transaction's inputs, omitting the unused final ID.
This reduces txid insertions from N to N-1 and replaces the separate coinbase seed and trailing insertion with one insertion at the start of the loop.
Retain the classification set across blocks to reuse its buckets and avoid reseeding its hasher.
The set still allocates and frees its nodes for each block.
`ThreadPool::Submit` binds its range by reference and moves the individual callables without taking the vector's storage, so keep the vector on the overlay for reuse across blocks.
Each task drains the shared input queue, so tasks beyond the number of inputs cannot do useful work.
For an empty queue, submit an empty range and skip the worker notification inside `ThreadPool::Submit` instead of guarding submission at the caller.
@l0rinc
l0rinc force-pushed the l0rinc/coins-reuse-prevout-preparation branch from 0977948 to ec33918 Compare November 4, 2026 17:38

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, applied the nits & rebased.

Comment thread src/coins.cpp Resolved
Comment thread src/coins.cpp Outdated
Comment on lines 400 to 407
} else {
// Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
// Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
// fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
LogWarning("Failed to submit prevout fetch tasks (%s); falling back to single-threaded fetching for this block.", SubmitErrorString(futures.error()));
m_inputs.clear();
StopFetching(); // Assert nothing changed if we failed to start tasks.
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, taken

@andrewtoth andrewtoth 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.

reACK ec33918

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