Skip to content

Commit

Permalink
Merge pull request #126 from multiformats/feat/reduce-blake-allocation
Browse files Browse the repository at this point in the history
feat: reduce blake2b allocations by special-casing the 256/512 variants
  • Loading branch information
Stebalien committed May 5, 2020
2 parents 6b39927 + da196d3 commit 2771720
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func sumBlake2s(data []byte, size int) ([]byte, error) {
return d[:], nil
}
func sumBlake2b(data []byte, size int) ([]byte, error) {
// special case these lengths to avoid allocations.
switch size {
case 32:
hash := blake2b.Sum256(data)
return hash[:], nil
case 64:
hash := blake2b.Sum512(data)
return hash[:], nil
}

// Ok, allocate away.
hasher, err := blake2b.New(&blake2b.Config{Size: uint8(size)})
if err != nil {
return nil, err
Expand Down

0 comments on commit 2771720

Please sign in to comment.