-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Optimize CHECKSIGADD Script Validation #24105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fee0c05
to
4a8fd0a
Compare
4a8fd0a
to
cfa5752
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
Benchmarks are showing results consistent with a 0.5% speedup:
#24105 vs. $mergebase (absolute)
#24105 vs. $mergebase (relative)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK cfa5752
utACK cfa5752 |
wat? How can this result in a speedup in syncing a part of the chain that has no taproot inputs at all? This code is almost dead during IBD, so shouldn't at all influence IBD time. |
If someone wants to write a "real" benchmark there is already |
@MarcoFalke i don't think that's a useful template since what is needed is a taproot transaction to test this. I agree that the @jamesob result is suspect for the reason you mentioned. |
Jup, with template I meant that all you need to do is create the taproot transaction. Obviously the benchmark doesn't yet bench taproot, since the bench was written before taproot. |
Yeah I just blindly ran bitcoinperf bench-pr --num-blocks 50_000 \
--run-id checksigadd-optimize \
--bitcoind-args='-dbcache=10000 -assumevalid=0' \
--run-count 2 24105 which anyone is capable of doing (provided they're willing to navigate the somewhat labyrinthine I had only cursorily skimmed the change when doing this; the standard deviation of the timing on this branch's IBD is ~0.2%, so there's a decent chance the result is just noise. But it's not like I cooked the benchmark or anything. |
Sometimes even seemingly unrelated code changes can affect performance (e.g. by ordering the functions slightly differently in memory, resulting in different CPU instruction caching or alignment) in small but statistically significant ways. |
review ACK cfa5752 |
ready to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code-review ACK cfa5752
nit: could adapt the PR description before merge, the "because of mutable field" part doesn't apply anymore
nit addressed |
Conceptually the same optimization works also for bip 143 hashing, so I am wondering if it is worth it to keep the 143/341 symmetry. Though, this will likely make a larger/uglier diff. |
I think this is good to go as is. The optimization can be added for BIP-143 later, I can draft a follow up PR for that. In any case, it would be separate commits because we want to preserve bisecting. |
cfa5752 Optimize CHECKSIGADD Script Validation (Jeremy Rubin) Pull request description: This is a mild validation improvement that improves performance by caching some signature data when you have a Taproot script fragment that uses CHECKSIGADD Multisignatures with sighash single. In some basic testing I showed this to have about a 0.6% speedup during block validation for a block with a lot of CHECKSIGADDs, but that was with the entirety of block validation so the specific impact on the script interpreter performance should be a bit more once you subtract things like coin fetching. If desired I can produce a more specific/sharable bench for this, the code I used to test was just monkey patching the existing taproot tests since generating valid spends is kinda tricky. But it's sort of an obvious win so I'm not sure it needs a rigorous bench, but I will tinker on one of those while the code is being reviewed for correctness. The overhead of this approach is that: 1. ScriptExecutionData is no longer const 2. around 32 bytes of extra stack space 3. zero extra hashing since we only cache on first use ACKs for top commit: sipa: utACK cfa5752 MarcoFalke: review ACK cfa5752 jonatack: ACK cfa5752 theStack: Code-review ACK cfa5752 Tree-SHA512: d5938773724bb9c97b6fd623ef7efdf7f522af52dc0903ecb88c38a518b628d7915b7eae6a774f7be653dc6bcd92e9abc4dd5e8b11f3a995e01e0102d2113d09
This is a mild validation improvement that improves performance by caching some signature data when you have a Taproot script fragment that uses CHECKSIGADD Multisignatures with sighash single. In some basic testing I showed this to have about a 0.6% speedup during block validation for a block with a lot of CHECKSIGADDs, but that was with the entirety of block validation so the specific impact on the script interpreter performance should be a bit more once you subtract things like coin fetching. If desired I can produce a more specific/sharable bench for this, the code I used to test was just monkey patching the existing taproot tests since generating valid spends is kinda tricky. But it's sort of an obvious win so I'm not sure it needs a rigorous bench, but I will tinker on one of those while the code is being reviewed for correctness.
The overhead of this approach is that: