...
1
16
17 package caching
18
19 import (
20 `fmt`
21 `testing`
22 `unsafe`
23
24 `github.com/bytedance/sonic/internal/rt`
25 )
26
27 const (
28 _H_basis uint64 = 0xcbf29ce484222325
29 _H_prime uint64 = 0x00000100000001b3
30 )
31
32 func fnv1a(s string) uint64 {
33 v := _H_basis
34 m := (*rt.GoString)(unsafe.Pointer(&s))
35
36
37 for i := 0; i < m.Len; i++ {
38 v ^= uint64(*(*uint8)(unsafe.Pointer(uintptr(m.Ptr) + uintptr(i))))
39 v *= _H_prime
40 }
41
42
43 if v == 0 {
44 return 1
45 } else {
46 return v
47 }
48 }
49
50 func TestHashing_Fnv1a(t *testing.T) {
51 fmt.Printf("%#x\n", fnv1a("hello, world"))
52 }
53
54 func TestHashing_StrHash(t *testing.T) {
55 s := "hello, world"
56 fmt.Printf("%#x\n", StrHash(s))
57 }
58
59 var fn_fnv1a = fnv1a
60 func BenchmarkHashing_Fnv1a(b *testing.B) {
61 for i := 0; i < b.N; i++ {
62 fn_fnv1a("accountid_interval_aweme_second")
63 }
64 }
65
66 func BenchmarkHashing_StrHash(b *testing.B) {
67 for i := 0; i < b.N; i++ {
68 StrHash("accountid_interval_aweme_second")
69 }
70 }
71
View as plain text