-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrapidhash_comparable_bench_test.go
More file actions
63 lines (57 loc) · 1.5 KB
/
rapidhash_comparable_bench_test.go
File metadata and controls
63 lines (57 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package rapidhash_test
import (
"testing"
"go.dw1.io/rapidhash"
)
func BenchmarkComparable(b *testing.B) {
seed := uint64(0x9e3779b97f4a7c15)
x := 42
arr := [4]uint32{1, 2, 3, 4}
st := comparableStruct{A: 7, B: "hi", C: [3]uint16{9, 10, 11}}
var nilPtr *int
cases := []struct {
name string
value any
}{
{name: "int", value: int(-12345)},
{name: "uint64", value: uint64(0xdeadbeefcafebabe)},
{name: "string", value: "rapidhash"},
{name: "bool", value: true},
{name: "uintptr", value: uintptr(123456)},
{name: "ptr", value: &x},
{name: "ptr-nil", value: nilPtr},
{name: "array", value: arr},
{name: "struct", value: st},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = hashComparableWithSeedAnyBench(tc.value, seed)
}
})
}
}
func hashComparableWithSeedAnyBench(v any, seed uint64) uint64 {
switch x := v.(type) {
case int:
return rapidhash.HashComparableWithSeed(x, seed)
case uint64:
return rapidhash.HashComparableWithSeed(x, seed)
case string:
return rapidhash.HashComparableWithSeed(x, seed)
case bool:
return rapidhash.HashComparableWithSeed(x, seed)
case uintptr:
return rapidhash.HashComparableWithSeed(x, seed)
case *int:
return rapidhash.HashComparableWithSeed(x, seed)
case [4]uint32:
return rapidhash.HashComparableWithSeed(x, seed)
case comparableStruct:
return rapidhash.HashComparableWithSeed(x, seed)
default:
panic("unsupported test type")
}
}