use faster way for siphash (#11640)

This commit is contained in:
Harshavardhana 2021-02-26 16:53:06 -08:00 committed by GitHub
parent 9171d6ef65
commit b690304eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ package cmd
import ( import (
"context" "context"
"encoding/binary"
"errors" "errors"
"fmt" "fmt"
"hash/crc32" "hash/crc32"
@ -663,9 +664,11 @@ func sipHashMod(key string, cardinality int, id [16]byte) int {
if cardinality <= 0 { if cardinality <= 0 {
return -1 return -1
} }
sip := siphash.New(id[:]) // use the faster version as per siphash docs
sip.Write([]byte(key)) // https://github.com/dchest/siphash#usage
return int(sip.Sum64() % uint64(cardinality)) k0, k1 := binary.LittleEndian.Uint64(id[0:8]), binary.LittleEndian.Uint64(id[8:16])
sum64 := siphash.Hash(k0, k1, []byte(key))
return int(sum64 % uint64(cardinality))
} }
func crcHashMod(key string, cardinality int) int { func crcHashMod(key string, cardinality int) int {