Skip to content

Commit da196d3

Browse files
committed
feat: reduce blake2b allocations by special-casing the 256/512 variants
These special-case functions don't allocate internally.
1 parent 6b39927 commit da196d3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

sum.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ func sumBlake2s(data []byte, size int) ([]byte, error) {
7373
return d[:], nil
7474
}
7575
func sumBlake2b(data []byte, size int) ([]byte, error) {
76+
// special case these lengths to avoid allocations.
77+
switch size {
78+
case 32:
79+
hash := blake2b.Sum256(data)
80+
return hash[:], nil
81+
case 64:
82+
hash := blake2b.Sum512(data)
83+
return hash[:], nil
84+
}
85+
86+
// Ok, allocate away.
7687
hasher, err := blake2b.New(&blake2b.Config{Size: uint8(size)})
7788
if err != nil {
7889
return nil, err

0 commit comments

Comments
 (0)