[#2] hrw: add typed methods for hashers #3
4 changed files with 201 additions and 19 deletions
48
README.md
48
README.md
|
@ -14,25 +14,37 @@
|
||||||
## Benchmark:
|
## Benchmark:
|
||||||
|
|
||||||
```
|
```
|
||||||
BenchmarkSort_fnv_10-8 5000000 365 ns/op 224 B/op 3 allocs/op
|
BenchmarkSort_fnv_10-8 4812801 240.9 ns/op 216 B/op 4 allocs/op
|
||||||
BenchmarkSort_fnv_100-8 300000 5261 ns/op 1856 B/op 3 allocs/op
|
BenchmarkSort_fnv_100-8 434767 2600 ns/op 1848 B/op 4 allocs/op
|
||||||
BenchmarkSort_fnv_1000-8 10000 119462 ns/op 16448 B/op 3 allocs/op
|
BenchmarkSort_fnv_1000-8 20428 66116 ns/op 16440 B/op 4 allocs/op
|
||||||
BenchmarkSortByIndex_fnv_10-8 3000000 546 ns/op 384 B/op 7 allocs/op
|
BenchmarkSortByIndex_fnv_10-8 2505410 486.5 ns/op 352 B/op 7 allocs/op
|
||||||
BenchmarkSortByIndex_fnv_100-8 200000 5965 ns/op 2928 B/op 7 allocs/op
|
BenchmarkSortByIndex_fnv_100-8 254556 4697 ns/op 1984 B/op 7 allocs/op
|
||||||
BenchmarkSortByIndex_fnv_1000-8 10000 127732 ns/op 25728 B/op 7 allocs/op
|
BenchmarkSortByIndex_fnv_1000-8 13581 88334 ns/op 16576 B/op 7 allocs/op
|
||||||
BenchmarkSortByValue_fnv_10-8 2000000 962 ns/op 544 B/op 17 allocs/op
|
BenchmarkSortByValue_fnv_10-8 1761030 682.1 ns/op 592 B/op 18 allocs/op
|
||||||
BenchmarkSortByValue_fnv_100-8 200000 9604 ns/op 4528 B/op 107 allocs/op
|
BenchmarkSortByValue_fnv_100-8 258838 4675 ns/op 4480 B/op 108 allocs/op
|
||||||
BenchmarkSortByValue_fnv_1000-8 10000 111741 ns/op 41728 B/op 1007 allocs/op
|
BenchmarkSortByValue_fnv_1000-8 27027 44649 ns/op 40768 B/op 1008 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Reflection_fnv_10-8 1013560 1249 ns/op 768 B/op 29 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Reflection_fnv_100-8 106029 11414 ns/op 6096 B/op 209 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Reflection_fnv_1000-8 10000 108977 ns/op 56784 B/op 2009 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Typed_fnv_10-8 1577814 700.3 ns/op 584 B/op 17 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Typed_fnv_100-8 215938 5024 ns/op 4472 B/op 107 allocs/op
|
||||||
|
BenchmarkSortHashersByValue_Typed_fnv_1000-8 24447 46889 ns/op 40760 B/op 1007 allocs/op
|
||||||
|
|
||||||
BenchmarkSortByWeight_fnv_10-8 3000000 501 ns/op 320 B/op 4 allocs/op
|
BenchmarkSortByWeight_fnv_10-8 2924833 370.6 ns/op 448 B/op 8 allocs/op
|
||||||
BenchmarkSortByWeight_fnv_100-8 200000 8495 ns/op 2768 B/op 4 allocs/op
|
BenchmarkSortByWeight_fnv_100-8 816069 1516 ns/op 2896 B/op 8 allocs/op
|
||||||
BenchmarkSortByWeight_fnv_1000-8 10000 197880 ns/op 24656 B/op 4 allocs/op
|
BenchmarkSortByWeight_fnv_1000-8 80391 17478 ns/op 24784 B/op 8 allocs/op
|
||||||
BenchmarkSortByWeightIndex_fnv_10-8 2000000 702 ns/op 480 B/op 8 allocs/op
|
BenchmarkSortByWeightIndex_fnv_10-8 1945612 550.3 ns/op 368 B/op 7 allocs/op
|
||||||
BenchmarkSortByWeightIndex_fnv_100-8 200000 9338 ns/op 3840 B/op 8 allocs/op
|
BenchmarkSortByWeightIndex_fnv_100-8 140473 8084 ns/op 2000 B/op 7 allocs/op
|
||||||
BenchmarkSortByWeightIndex_fnv_1000-8 10000 204669 ns/op 33936 B/op 8 allocs/op
|
BenchmarkSortByWeightIndex_fnv_1000-8 5518 200949 ns/op 16592 B/op 7 allocs/op
|
||||||
BenchmarkSortByWeightValue_fnv_10-8 1000000 1083 ns/op 640 B/op 18 allocs/op
|
BenchmarkSortByWeightValue_fnv_10-8 1305580 909.8 ns/op 608 B/op 18 allocs/op
|
||||||
BenchmarkSortByWeightValue_fnv_100-8 200000 11444 ns/op 5440 B/op 108 allocs/op
|
BenchmarkSortByWeightValue_fnv_100-8 165410 6796 ns/op 4496 B/op 108 allocs/op
|
||||||
BenchmarkSortByWeightValue_fnv_1000-8 10000 148471 ns/op 49936 B/op 1008 allocs/op
|
BenchmarkSortByWeightValue_fnv_1000-8 17922 78555 ns/op 40784 B/op 1008 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueReflection_fnv_10-8 454976 2229 ns/op 784 B/op 29 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueReflection_fnv_100-8 76264 15332 ns/op 6112 B/op 209 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueReflection_fnv_1000-8 80288 13192 ns/op 6112 B/op 209 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueTyped_fnv_10-8 1433113 901.4 ns/op 600 B/op 17 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueTyped_fnv_100-8 188626 5896 ns/op 4488 B/op 107 allocs/op
|
||||||
|
BenchmarkSortHashersByWeightValueTyped_fnv_1000-8 178131 6518 ns/op 4488 B/op 107 allocs/op
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -1,8 +1,13 @@
|
||||||
module github.com/TrueCloudLab/hrw
|
module github.com/TrueCloudLab/hrw
|
||||||
|
|
||||||
go 1.14
|
go 1.18
|
||||||
fixed fixed
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/spaolacci/murmur3 v1.1.0
|
github.com/spaolacci/murmur3 v1.1.0
|
||||||
github.com/stretchr/testify v1.3.0
|
github.com/stretchr/testify v1.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
)
|
||||||
|
|
34
hrw.go
34
hrw.go
|
@ -83,6 +83,17 @@ func SortSliceByValue(slice interface{}, hash uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SortHasherSliceByValue receives []Hasher and hash to sort by value-distance.
|
||||||
|
func SortHasherSliceByValue[T Hasher](slice []T, hash uint64) {
|
||||||
|
rule := prepareHasherRule(slice)
|
||||||
|
if rule != nil {
|
||||||
|
swap := func(i, j int) {
|
||||||
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
|
}
|
||||||
|
sortByDistance(len(rule), false, rule, hash, swap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SortSliceByWeightValue received []T, weights and hash to sort by value-distance * weights
|
// SortSliceByWeightValue received []T, weights and hash to sort by value-distance * weights
|
||||||
func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64) {
|
func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64) {
|
||||||
rule := prepareRule(slice)
|
rule := prepareRule(slice)
|
||||||
|
@ -92,6 +103,17 @@ func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SortHasherSliceByWeightValue receives []Hasher, weights and hash to sort by value-distance * weights.
|
||||||
|
func SortHasherSliceByWeightValue[T Hasher](slice []T, weights []float64, hash uint64) {
|
||||||
|
rule := prepareHasherRule(slice)
|
||||||
|
if rule != nil {
|
||||||
|
swap := func(i, j int) {
|
||||||
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
|
}
|
||||||
|
sortByWeight(len(slice), false, rule, weights, hash, swap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SortSliceByIndex received []T and hash to sort by index-distance
|
// SortSliceByIndex received []T and hash to sort by index-distance
|
||||||
func SortSliceByIndex(slice interface{}, hash uint64) {
|
func SortSliceByIndex(slice interface{}, hash uint64) {
|
||||||
length := reflect.ValueOf(slice).Len()
|
length := reflect.ValueOf(slice).Len()
|
||||||
|
@ -199,6 +221,18 @@ func prepareRule(slice interface{}) []uint64 {
|
||||||
return rule
|
return rule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func prepareHasherRule[T Hasher](hashers []T) []uint64 {
|
||||||
|
length := len(hashers)
|
||||||
|
if length == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
result := make([]uint64, length)
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
result[i] = hashers[i].Hash()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateWeights checks if weights are normalized between 0.0 and 1.0
|
// ValidateWeights checks if weights are normalized between 0.0 and 1.0
|
||||||
func ValidateWeights(weights []float64) error {
|
func ValidateWeights(weights []float64) error {
|
||||||
for i := range weights {
|
for i := range weights {
|
||||||
|
|
131
hrw_test.go
131
hrw_test.go
|
@ -131,6 +131,23 @@ func TestSortSliceByValueHasher(t *testing.T) {
|
||||||
require.Equal(t, expect, actual)
|
require.Equal(t, expect, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSortHasherSliceByValue(t *testing.T) {
|
||||||
|
actual := []hashString{"a", "b", "c", "d", "e", "f"}
|
||||||
|
expect := []hashString{"d", "f", "c", "b", "a", "e"}
|
||||||
|
hash := Hash(testKey)
|
||||||
|
SortHasherSliceByValue(actual, hash)
|
||||||
|
require.EqualValues(t, expect, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortHasherSliceByWeightValue(t *testing.T) {
|
||||||
|
actual := []hashString{"a", "b", "c", "d", "e", "f"}
|
||||||
|
weights := []float64{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}
|
||||||
|
expect := []hashString{"d", "f", "c", "b", "a", "e"}
|
||||||
|
hash := Hash(testKey)
|
||||||
|
SortHasherSliceByWeightValue(actual, weights, hash)
|
||||||
|
require.EqualValues(t, expect, actual)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSortSliceByValueIntSlice(t *testing.T) {
|
func TestSortSliceByValueIntSlice(t *testing.T) {
|
||||||
cases := []slices{
|
cases := []slices{
|
||||||
{
|
{
|
||||||
|
@ -636,6 +653,36 @@ func BenchmarkSortByValue_fnv_1000(b *testing.B) {
|
||||||
benchmarkSortByValue(b, 1000, hash)
|
benchmarkSortByValue(b, 1000, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Reflection_fnv_10(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueReflection(b, 10, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Reflection_fnv_100(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueReflection(b, 100, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Reflection_fnv_1000(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueReflection(b, 1000, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Typed_fnv_10(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueTyped(b, 10, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Typed_fnv_100(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueTyped(b, 100, hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByValue_Typed_fnv_1000(b *testing.B) {
|
||||||
|
hash := Hash(testKey)
|
||||||
|
benchmarkSortHashersByValueTyped(b, 1000, hash)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkSortByWeight_fnv_10(b *testing.B) {
|
func BenchmarkSortByWeight_fnv_10(b *testing.B) {
|
||||||
hash := Hash(testKey)
|
hash := Hash(testKey)
|
||||||
_ = benchmarkSortByWeight(b, 10, hash)
|
_ = benchmarkSortByWeight(b, 10, hash)
|
||||||
|
@ -681,6 +728,30 @@ func BenchmarkSortByWeightValue_fnv_1000(b *testing.B) {
|
||||||
benchmarkSortByWeightValue(b, 1000, hash)
|
benchmarkSortByWeightValue(b, 1000, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueReflection_fnv_10(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueRelection(b, 10, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueReflection_fnv_100(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueRelection(b, 100, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueReflection_fnv_1000(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueRelection(b, 100, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueTyped_fnv_10(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueTyped(b, 10, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueTyped_fnv_100(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueTyped(b, 100, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSortHashersByWeightValueTyped_fnv_1000(b *testing.B) {
|
||||||
|
benchmarkSortHashersByWeightValueTyped(b, 100, Hash(testKey))
|
||||||
|
}
|
||||||
|
|
||||||
func benchmarkSort(b *testing.B, n int, hash uint64) uint64 {
|
func benchmarkSort(b *testing.B, n int, hash uint64) uint64 {
|
||||||
servers := make([]uint64, n)
|
servers := make([]uint64, n)
|
||||||
for i := uint64(0); i < uint64(len(servers)); i++ {
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
||||||
|
@ -774,3 +845,63 @@ func benchmarkSortByWeightValue(b *testing.B, n int, hash uint64) {
|
||||||
SortSliceByWeightValue(servers, weights, hash)
|
SortSliceByWeightValue(servers, weights, hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func benchmarkSortHashersByWeightValueRelection(b *testing.B, n int, hash uint64) {
|
||||||
|
servers := make([]hashString, n)
|
||||||
|
weights := make([]float64, n)
|
||||||
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
||||||
|
weights[i] = float64(uint64(n)-i) / float64(n)
|
||||||
|
servers[i] = hashString("localhost:" + strconv.FormatUint(60000-i, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
SortSliceByWeightValue(servers, weights, hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkSortHashersByWeightValueTyped(b *testing.B, n int, hash uint64) {
|
||||||
|
servers := make([]hashString, n)
|
||||||
|
weights := make([]float64, n)
|
||||||
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
||||||
|
weights[i] = float64(uint64(n)-i) / float64(n)
|
||||||
|
servers[i] = hashString("localhost:" + strconv.FormatUint(60000-i, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
SortHasherSliceByWeightValue(servers, weights, hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkSortHashersByValueReflection(b *testing.B, n int, hash uint64) {
|
||||||
|
servers := make([]hashString, n)
|
||||||
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
||||||
|
servers[i] = hashString("localhost:" + strconv.FormatUint(60000-i, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
SortSliceByValue(servers, hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkSortHashersByValueTyped(b *testing.B, n int, hash uint64) {
|
||||||
|
servers := make([]hashString, n)
|
||||||
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
||||||
|
servers[i] = hashString("localhost:" + strconv.FormatUint(60000-i, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
SortHasherSliceByValue(servers, hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue
Worth a separate commit.