script: (optimization) scan `CastToBool` in 64-bit chunks #36878

pull jrakibi wants to merge 1 commits into bitcoin:master from jrakibi:15-11-optimize-casttobool changing 1 files +10 −2
  1. jrakibi commented at 11:01 AM on November 15, 2026: contributor

    found we can optimize CastToBool used by IFDUP, VERIFY while reviewing #36721.

    Currently the loop checks one byte at a time, instead we can process one word of 8 bytes at a time.

    For the worst case scenario of one-tx-block with a script <520-byte stack element> [IFDUP VERIFY] repeated, with the element being 519 zero bytes + 0x01:

    the benchamrk shows 3.1x speedup (from ~922ms to ~295ms)

    Master at https://github.com/bitcoin/bitcoin/commit/a527a0077a0fcae913188410214efe98021c0cca:

    ns/script script/s err% total benchmark
    922,608,458.00 1.08 1.3% 10.21 VerifyIfDupVerifyFullLast

    This PR
:

    ns/script script/s err% total benchmark
    295,193,916.00 3.39 1.5% 3.31 VerifyIfDupVerifyFullLast
  2. script: (optimization) scan CastToBool in 64-bit chunks
    We can optimize `CastToBool` used by IFDUP, VERIFY.
    Currently the loop checks one byte at a time, instead we can process
    one word of 8 bytes at a time.
    
    For the worst case scenario of one-tx-block with a script
    `<520-byte stack element> [IFDUP VERIFY]` repeated, with the element
    being 519 zero bytes + 0x01: the benchamrk shows 3.1x speedup (from ~922ms to ~295ms)
    
    Master:
    
    |           ns/script |            script/s |    err% |     total | benchmark
    |--------------------:|--------------------:|--------:|----------:|:----------
    |      922,608,458.00 |                1.08 |    1.3% |     10.21 | `VerifyIfDupVerifyFullLast`
    
    This PR

    |           ns/script |            script/s |    err% |     total | benchmark
    |--------------------:|--------------------:|--------:|----------:|:----------
    |      295,193,916.00 |                3.39 |    1.5% |      3.31 | `VerifyIfDupVerifyFullLast`
    2d5f67c986
  3. DrahtBot added the label Consensus on Nov 15, 2026
  4. DrahtBot commented at 11:01 AM on November 15, 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
    Concept NACK darosior

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. darosior changes_requested
  6. darosior commented at 2:40 PM on November 16, 2026: member

    I don't think an imperceptible performance improvement justifies modifying subtle, consensus-critical code. Concept NACK.

  7. maflcko commented at 2:48 PM on November 16, 2026: member

    Closing for now due to controversy

  8. maflcko closed this on Nov 16, 2026

  9. l0rinc commented at 8:33 PM on November 16, 2026: contributor

    I’ve also started looking at this and added tests and benchmarks locally for a few corner cases. On an RPi4, with a non-standard tx using 519 zero bytes followed by 01, median submitblock time was ~3.9 seconds before and ~1.6 seconds with this (tweaked it a bit locally while reviewing):

    bool CastToBool(const valtype& vch)
    {
        size_t i{0};
        for (; i + sizeof(uint64_t) <= vch.size(); i += sizeof(uint64_t)) {
            if (ReadLE64(vch.data() + i)) break;
        }
        for (; i < vch.size(); i++) {
            if (vch[i]) return !(i == vch.size() - 1 && vch[i] == 0x80); // Negative zero is false
        }
        return false;
    }
    

    It’s not horrible, but the fix is simple enough that I don’t mind pushing a follow-up (with @jrakibi as coauthor) that cleans it up and adds tests and manual benchmarks.


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-17 15:51 UTC

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