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

Minor hashing-related optimizations. #366

Merged
merged 1 commit into from
Apr 6, 2015

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Apr 4, 2015

This pull request contains three classes of optimizations:

  • Reduce the number of unnecessary hash copies
  • Improve the performance of the DoubleSha256 function
  • A couple of minor optimizations of the ShaHash functions

The first class is a result of the Bytes function on a ShaHash making a copy of the bytes before returning them. It really should have been named CloneBytes, but that would break the API now.

To address this, a comment has been added to the function which explicitly calls out the copy behavior. In addition, all call sites of .Bytes on a ShaHash in the code base have been updated to simply slice the array when a copy is not needed. This saves a significant amount of data copying.

The second optimization modifies the DoubleSha256 function to directly use fastsha256.Sum256 instead of the hasher interface. This reduces the number of allocations needed. A benchmark for the function has been added as well.

old: BenchmarkDoubleSha256  500000   3691 ns/op   192 B/op   3 allocs/op
new: BenchmarkDoubleSha256  500000   3081 ns/op    32 B/op   1 allocs/op

The final optimizations are for the ShaHash IsEqual and SetBytes functions which have been modified to make use of the fact the type is an array and remove an unneeded subslice.

@jrick
Copy link
Member

jrick commented Apr 6, 2015

ok

This commit contains three classes of optimizations:
 - Reducing the number of unnecessary hash copies
 - Improve the performance of the DoubleSha256 function
 - A couple of minor optimizations of the ShaHash functions

The first class is a result of the Bytes function on a ShaHash making a
copy of the bytes before returning them.  It really should have been named
CloneBytes, but that would break the API now.

To address this, a comment has been added to the function which explicitly
calls out the copy behavior.  In addition, all call sites of .Bytes on a
ShaHash in the code base have been updated to simply slice the array when
a copy is not needed.  This saves a significant amount of data copying.

The second optimization modifies the DoubleSha256 function to directly use
fastsha256.Sum256 instead of the hasher interface.  This reduces the
number of allocations needed.  A benchmark for the function has been added
as well.

old: BenchmarkDoubleSha256  500000   3691 ns/op   192 B/op   3 allocs/op
new: BenchmarkDoubleSha256  500000   3081 ns/op    32 B/op   1 allocs/op

The final optimizations are for the ShaHash IsEqual and SetBytes functions
which have been modified to make use of the fact the type is an array and
remove an unneeded subslice.
@conformal-deploy conformal-deploy merged commit f5cdf2d into btcsuite:master Apr 6, 2015
@davecgh davecgh deleted the hash_optimize branch April 6, 2015 16:36
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.

3 participants