btcec: reduce allocations#2559
Open
allocz wants to merge 7 commits into
Open
Conversation
Author
Member
|
Approved CI run. |
TechLateef
reviewed
Jul 3, 2026
TechLateef
reviewed
Jul 4, 2026
TechLateef
reviewed
Jul 4, 2026
2beacf8 to
1f3d515
Compare
To optimize critical procedures used in validation loop, we first need all of them covered by benchmarks, so that we can have a baseline for future optimizations. btcec added benchmarks: * BenchmarkParseUncompressedPubkey btcec/ecdsa added benchmarks: * BenchmarkParseDERSignature * BenchmarkParseBERSignature btcec/schnorr added benchmarks: * BenchmarkParsePubKey * BenchmarkParseSignature btcec/ecdsa benchmark improvements: * Avoid unneccessary allocations on BenchmarkSigVerify. * Avoid unneccessary allocations on BenchmarkParseCompressedPubKey.
Previously, changing DER boolean argument of parseSig was breaking VerifyLowS behavior without breaking the tests. New sentinel errors and entries to the VerifyLowS test table were added, effectively catching regressions in the implementation.
Added unexported function that does not return a pointer to the public key to execute the key parsing, letting the exported ParsePubKey short and simple, making it eligible to inlining and therefore zeroing out allocations.
Added unexported function that does not return a pointer to the public key to execute the key parsing, letting the exported ParsePubKey short and simple, making it eligible to inlining and therefore zeroing out allocations.
btcec/schnorr: replace usage of SerializePubKey for serializePubKey32, which writes to a byte slice instead of allocating and returning a new one. chainhash: keep TaggedHash short and simple to make it eligible to inlining and avoid allocations.
Make ParseSignature and ParseDERSignature short and simple, making the procedures eligible to inlining and therefore avoiding allocations.
Make ParseSignature short and simple, becoming eligible to inlining which avoids allocations.
1f3d515 to
ac92fde
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
I've optimized public key parsing, signature parsing and signature verification
for both ECDSA and Schnorr, the following are the improvements in the benchmarks
before and after.
Obs: using the following patches for both benchmarks:
golang/go#79708: big.Int.ModInverse allocation zeroing.
decred/dcrd#3714: Public Key parsing and ModNScalar
modulo inverse allocation zeroing.
To avoid forgetting to update the dependencies in the future, even after approval, I think we should wait for upstream releases of both Go and
dcrec/secp256k1containing the patches mentioned above, so that when this code is merged, the benchmark report the exact same results in respect of allocation count.In summary:
Schnorr signature parsing time: ~-33%
ECDSA signature parsing time: ~-30%
Uncompressed public key parsing time: ~-9.5%
But all the procedures in the benchmarks above are not doing allocations
anymore, reducing the frequency between garbage collection cycles that must scan
all the reachable heap (that is considerably large in production).
Now we have more CPU available to perform block verification.
Steps to Test
Steps for reviewers to follow to test the change.
Pull Request Checklist
Testing
Code Style and Documentation
📝 Please see our Contribution Guidelines for further guidance.