generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package xfnv is modified and non-cross-platform compatible version of FNV-1a. | ||
// | ||
// It computes 8 bytes per round by converting bytes to uint64 directly | ||
// as a result it doesn't generate the same result for diff cpu arch. | ||
package xfnv | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
fnvHashOffset64 = uint64(14695981039346656037) // fnv hash offset64 | ||
fnvHashPrime64 = uint64(1099511628211) | ||
) | ||
|
||
func strDataPtr(s string) unsafe.Pointer { | ||
// for str, the Data ptr is always the 1st field | ||
return *(*unsafe.Pointer)(unsafe.Pointer(&s)) | ||
} | ||
|
||
func bytesDataPtr(b []byte) unsafe.Pointer { | ||
// for []byte, the Data ptr is always the 1st field | ||
return *(*unsafe.Pointer)(unsafe.Pointer(&b)) | ||
} | ||
|
||
// Hash returns the hash of the given bytes | ||
// | ||
// DO NOT STORE the return value since it's NOT cross-platform compatible. | ||
// It's designed for in-memory use. | ||
func Hash(b []byte) uint64 { | ||
return doHash(bytesDataPtr(b), len(b)) | ||
} | ||
|
||
// HashStr returns the hash of the given string | ||
// | ||
// DO NOT STORE the return value since it's NOT cross-platform compatible. | ||
// It's designed for in-memory use. | ||
func HashStr(s string) uint64 { | ||
return doHash(strDataPtr(s), len(s)) | ||
} | ||
|
||
func doHash(p unsafe.Pointer, n int) uint64 { | ||
h := fnvHashOffset64 | ||
i := 0 | ||
// 8 byte per round | ||
for m := n >> 3; i < m; i++ { | ||
h ^= *(*uint64)(unsafe.Add(p, i<<3)) // p[i*8] | ||
h *= fnvHashPrime64 | ||
} | ||
// left 0-7 bytes | ||
i = i << 3 | ||
for ; i < n; i++ { | ||
h ^= uint64(*(*byte)(unsafe.Add(p, i))) | ||
h *= fnvHashPrime64 | ||
} | ||
return h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package xfnv | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"hash/maphash" | ||
"testing" | ||
|
||
"github.com/bytedance/gopkg/util/xxhash3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHashStr(t *testing.T) { | ||
require.Equal(t, HashStr("1234"), HashStr("1234")) | ||
require.NotEqual(t, HashStr("12345"), HashStr("12346")) | ||
require.Equal(t, HashStr("12345678"), HashStr("12345678")) | ||
require.NotEqual(t, HashStr("123456789"), HashStr("123456788")) | ||
} | ||
|
||
func BenchmarkHash(b *testing.B) { | ||
sizes := []int{8, 16, 32, 64, 128, 512} | ||
bb := make([][]byte, len(sizes)) | ||
for i := range bb { | ||
b := make([]byte, sizes[i]) | ||
rand.Read(b) | ||
bb[i] = b | ||
} | ||
b.ResetTimer() | ||
for _, data := range bb { | ||
b.Run(fmt.Sprintf("size-%d-xfnv", len(data)), func(b *testing.B) { | ||
b.SetBytes(int64(len(data))) | ||
for i := 0; i < b.N; i++ { | ||
_ = Hash(data) | ||
} | ||
}) | ||
} | ||
|
||
println("") | ||
|
||
for _, data := range bb { | ||
b.Run(fmt.Sprintf("size-%d-xxhash3", len(data)), func(b *testing.B) { | ||
b.SetBytes(int64(len(data))) | ||
for i := 0; i < b.N; i++ { | ||
_ = xxhash3.Hash(data) | ||
} | ||
}) | ||
} | ||
|
||
println("") | ||
|
||
for _, data := range bb { | ||
b.Run(fmt.Sprintf("size-%d-maphash", len(data)), func(b *testing.B) { | ||
s := maphash.MakeSeed() | ||
h := &maphash.Hash{} | ||
h.SetSeed(s) | ||
b.SetBytes(int64(len(data))) | ||
for i := 0; i < b.N; i++ { | ||
// use maphash.Bytes which is more fair to benchmark after go1.19 | ||
// maphash.Bytes(s, data) | ||
_, _ = h.Write(data) | ||
} | ||
}) | ||
} | ||
} |