Skip to content
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

txscript: More memory efficient CalcSignatureHash #1731

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kcalvinalvin
Copy link
Collaborator

@kcalvinalvin kcalvinalvin commented Jul 9, 2021

What was done (in order of the commits)

1: DoubleHashBRaw() function is introduced, just takes in a hash.Hash object directly, allowing callers to save on the byte slice allocation that would have happened otherwise.
2: writeOutPoint() was exported. This is needed for the next commit.
3: CalcSignatureHash() was modified to not have a txCopy, saving the memory allocation for that.
4: CalcSignatureHash() and CalcWitnessSignatureHash() now uses DoubleHashBRaw() instead of DoubleHashB().

In my testing, the memory savings were worth the changes. But this is a pretty insignificant change without #1684

Instead of receiving a byte slice as the argument, this new function
receives a hash.Hash, giving the option for callers to write directly
into a hash and save on the byte slice allocation.
This new calcSignatureHash function behaves more like the calcWitnessSigHash
function, which was more memory efficient as it didn't create a
temporary tx just to use the tx serialize function that already existed.
This results in significant memory savings that show up on pprof.
Changes up calcWitnessSignatureHash to use the DoubleHashBRaw function,
saving on the byte slice allocation that would have happened otherwise.
@kcalvinalvin kcalvinalvin force-pushed the btcd-calcsighash-optimizations branch from ffcf709 to 50f8996 Compare July 29, 2021 14:21
@coveralls
Copy link

Pull Request Test Coverage Report for Build 1078925121

  • 70 of 76 (92.11%) changed or added relevant lines in 3 files are covered.
  • 25 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 52.861%

Changes Missing Coverage Covered Lines Changed/Added Lines %
chaincfg/chainhash/hashfuncs.go 0 6 0.0%
Files with Coverage Reduction New Missed Lines %
peer/peer.go 3 76.1%
txscript/script.go 22 85.96%
Totals Coverage Status
Change from base Build 1071527997: -0.02%
Covered Lines: 21018
Relevant Lines: 39761

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants