-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat:#59][pkg:hashers]: allocate-free Fnv-1a
- Loading branch information
1 parent
1f8ee08
commit c215e58
Showing
4 changed files
with
93 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hashers | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
// copy from hash/fnv/fnv.go | ||
const ( | ||
offset32 uint32 = 2166136261 | ||
offset64 uint64 = 14695981039346656037 | ||
prime32 uint32 = 16777619 | ||
prime64 uint64 = 1099511628211 | ||
) | ||
|
||
// Fnv32a returns a 32-bit FNV-1a hash of a string. | ||
func Fnv32a(s string) uint32 { | ||
hash := offset32 | ||
for _, b := range *(*[]byte)(unsafe.Pointer(&s)) { | ||
hash = (hash ^ uint32(b)) * prime32 | ||
} | ||
return hash | ||
} | ||
|
||
// Fnv64a returns a 64-bit FNV-1a hash of a string. | ||
func Fnv64a(s string) uint64 { | ||
hash := offset64 | ||
for _, b := range *(*[]byte)(unsafe.Pointer(&s)) { | ||
hash = (hash ^ uint64(b)) * prime64 | ||
} | ||
return hash | ||
} |
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,59 @@ | ||
package hashers | ||
|
||
import ( | ||
"hash/fnv" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var _testString = "abcdefghijklmnopqrstuvwxyz" | ||
|
||
func Test_Fnv32a(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
assert.Equal(t, Fnv32a(strconv.Itoa(i)), fnv32a(strconv.Itoa(i))) | ||
} | ||
} | ||
|
||
func Test_Fnv64a(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
assert.Equal(t, Fnv64a(strconv.Itoa(i)), fnv64a(strconv.Itoa(i))) | ||
} | ||
} | ||
|
||
func Benchmark_Allocate_Free_FNV32a(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Fnv32a(_testString) | ||
} | ||
} | ||
|
||
func Benchmark_fnv32a(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
fnv32a(_testString) | ||
} | ||
} | ||
|
||
func Benchmark_Allocate_Free_FNV64(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Fnv64a(_testString) | ||
} | ||
} | ||
|
||
func Benchmark_fnv64a(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
fnv64a(_testString) | ||
} | ||
} | ||
|
||
func fnv32a(s string) uint32 { | ||
h := fnv.New32a() | ||
_, _ = h.Write([]byte(s)) | ||
return h.Sum32() | ||
} | ||
|
||
func fnv64a(s string) (hash uint64) { | ||
h := fnv.New64a() | ||
_, _ = h.Write([]byte(s)) | ||
return h.Sum64() | ||
} |
This file was deleted.
Oops, something went wrong.
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