Skip to content

Commit

Permalink
z: use MemHashString and xxhash.Sum64String (#153)
Browse files Browse the repository at this point in the history
Use `MemHashString` and `xxhash.Sum64String` for KeyToHash
which is faster than `MemHash` and `Sum64`. See
#153 for benchmark results.
  • Loading branch information
zchee authored May 12, 2020
1 parent 9c31bb2 commit 7a3f2d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 16 additions & 1 deletion z/rtutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ import (
func BenchmarkMemHash(b *testing.B) {
buf := make([]byte, 64)
rand.Read(buf)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = MemHash(buf)
}
b.SetBytes(int64(len(buf)))
}

func BenchmarkMemHashString(b *testing.B) {
s := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
MemHash(buf)
_ = MemHashString(s)
}
b.SetBytes(int64(len(s)))
}

func BenchmarkSip(b *testing.B) {
Expand Down
3 changes: 1 addition & 2 deletions z/z.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func KeyToHash(key interface{}) (uint64, uint64) {
case uint64:
return k, 0
case string:
raw := []byte(k)
return MemHash(raw), xxhash.Sum64(raw)
return MemHashString(k), xxhash.Sum64String(k)
case []byte:
return MemHash(k), xxhash.Sum64(k)
case byte:
Expand Down

0 comments on commit 7a3f2d3

Please sign in to comment.