validation: reduce per-transaction work #36806

pull jlopp wants to merge 1 commits into bitcoin:master from jlopp:validationOptimizations changing 5 files +452 −14
  1. jlopp commented at 6:21 PM on November 8, 2026: contributor

    Motivation: minor optimizations to slightly speed up validation by reducing resource usage.

    I realize this touches consensus critical code and changes should not be made lightly. Open to feedback that the changes are not worth the performance gain, though I believe these changes can be made safely. To be specific:

    1. Duplicate input check (CheckTransaction): replace the std::set with a pairwise scan for transactions up to 96 inputs. 2X to 3X speedup.
    2. Relative lock times (EvaluateSequenceLocks): CalculateSequenceLocks returns {-1,-1} when no input has an active relative lock, so consult the previous block's median time past only when lockPair.second >= 0. Avoids an 11-entry walk plus sort for the large majority of transactions.
    3. ConnectBlock: skip the per-input coin lookups that fill prevheights when BIP68 is not enforced for the block, the tx version is below 2, or every input sets SEQUENCE_LOCKTIME_DISABLE_FLAG. Correct because none of those cases can read a height.
    4. Sigop cost (GetTransactionSigOpCost): fold the P2SH and witness counts into one pass over the inputs, halving coin cache lookups for P2SH txs.

    I ran a few benchmarks / performance tests on this change. One set of benchmarks on my 8 core laptop solely for the duplicate input check change:

    inputs old new speedup
    1 9.61 ns 7.65 ns 1.26×
    2 20.09 ns 9.12 ns 2.20×
    2 (mixed idx) 20.42 ns 9.11 ns 2.24×
    4 39.86 ns 15.08 ns 2.64×
    4 (mixed) 39.29 ns 15.03 ns 2.61×
    8 79.50 ns 30.70 ns 2.59×
    16 248.71 ns 79.39 ns 3.13×
    32 577.75 ns 224.12 ns 2.58×
    64 1429.95 ns 768.97 ns 1.86×
    96 2355.97 ns 1618.86 ns 1.46×
    128 3208.11 ns 3230.06 ns 0.99×
    256 7682.29 ns 7375.42 ns 1.04×

    I also ran two sequential runs of bitcoind --reindex-chainstate on my 6 core desktop to try to compare before and after, the aggregate effect of all 4 changes.

    Bitcoin Core v32.1 completed to block height 974,000 in 4 hours, 14 min, 25 sec. This patch on master branch completed to block height 974,000 in 4 hours, 11 min, 58 sec.

    Thus a performance speedup of 2 min 27 sec.

  2. DrahtBot added the label Validation on Nov 8, 2026
  3. DrahtBot commented at 6:21 PM on November 8, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    Approach NACK l0rinc

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36521 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36521.svg"></sub> (Keep witness stacks in serialized form in CScriptWitness 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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. validation: reduce per-transaction work
    Duplicate input check (CheckTransaction): replace the std::set with a
    pairwise scan for transactions up to 96 inputs. 2X to 3X speedup.
    
    Relative lock times (EvaluateSequenceLocks): CalculateSequenceLocks
    returns {-1,-1} when no input has an active relative lock, so consult the
    previous block's median time past only when lockPair.second >= 0. Avoids
    an 11-entry walk plus sort for the large majority of transactions.
    
    ConnectBlock: skip the per-input coin lookups that fill prevheights when
    BIP68 is not enforced for the block, the tx version is below 2, or every
    input sets SEQUENCE_LOCKTIME_DISABLE_FLAG. Correct because none of those
    cases can read a height.
    
    Sigop cost (GetTransactionSigOpCost): fold the P2SH and witness counts
    into one pass over the inputs, halving coin cache lookups for P2SH txs.
    4864648b5f
  5. jlopp force-pushed on Nov 8, 2026
  6. DrahtBot added the label CI failed on Nov 8, 2026
  7. DrahtBot commented at 6:31 PM on November 8, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/42509628397/job/128819653443</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU changed includes in src/consensus/tx_check.cpp, leaving an uncommitted diff that the lint check rejects.</sub>

    <details><summary>Hints</summary>

    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.

    </details>

  8. DrahtBot removed the label CI failed on Nov 8, 2026
  9. l0rinc commented at 4:28 PM on November 9, 2026: contributor

    Thanks for the suggestions, concept ACK on reducing validation overhead as long as the resulting code stays simple. Approach NACK on bundling four independent consensus-critical changes into a single commit. Each deserves its own PR with focused review, testing, and benchmarking, so we can judge whether its benefits justify the risks.

    The duplicate-input optimization has been proposed several times already (#14387, #14397, #14837, #31682), exploring different approaches including special-casing 1-2 inputs (the vast majority of cases) and using a sorted-vector fallback. The latest attempt was closed due to lack of interest, which is understandable given that CVE-2018-17144 was caused by a duplicate-input check optimization.

    There are also related approaches to avoiding redundant prevheights lookups: #138 reuses heights from CheckTxInputs, while #302 skips unnecessary lookups based on BIP68 applicability and individual input sequence flags.

    For sigops, previous work (#32532, #32729) explored other optimizations, including reusing legacy sigop counts between CheckBlock and ConnectBlock. More fundamentally, as discussed in #36476, I'd prefer to skip sigop counting entirely during assumevalid: why spend resources counting signature operations for historical blocks whose scripts we aren't even verifying? That would be necessary for pruneassumevalid anyway, so it's not just a micro-optimization.

  10. jlopp closed this on Nov 13, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-16 17:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me