Minor hashing-related optimizations. #366
Merged
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.
This pull request contains three classes of optimizations:
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.
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.