1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2022-03-28 17:35:58 +08:00
parent abeabeeb61
commit a5fb792dcf
4 changed files with 158 additions and 12 deletions

View File

@ -4,8 +4,6 @@ import (
"fmt"
"runtime"
"sync"
"github.com/charlienet/go-mixed/hash"
)
var _ Map[string, string] = &ConcurrnetMap[string, string]{}
@ -91,12 +89,46 @@ func (m *ConcurrnetMap[K, V]) Count() int {
}
func (m *ConcurrnetMap[K, V]) getBucket(k K) Map[K, V] {
bytes := getBytes(k)
id := hash.XXHashUint64(bytes) % m.numOfBuckets
id := getTag(k) % m.numOfBuckets
return m.buckets[id]
}
func getBytes(k any) []byte {
return []byte(fmt.Sprintf("%v", k))
func getTag[T comparable](v T) uint64 {
var vv any = v
switch vv.(type) {
case string:
return fnv64(vv.(string))
case int8:
return uint64(vv.(int8))
case uint8:
return uint64(vv.(uint8))
case int:
return uint64(vv.(int))
case int32:
return uint64(vv.(int32))
case uint32:
return uint64(vv.(uint32))
case int64:
return uint64(vv.(int64))
case uint64:
return vv.(uint64)
default:
return fnv64(fmt.Sprintf("%v", v))
}
}
const (
prime32 = uint64(16777619)
)
func fnv64(k string) uint64 {
var hash = uint64(2166136261)
l := len(k)
for i := 0; i < l; i++ {
hash *= prime32
hash ^= uint64(k[i])
}
return hash
}

View File

@ -5,7 +5,9 @@ import (
"runtime"
"testing"
"github.com/charlienet/go-mixed/bytesconv"
"github.com/charlienet/go-mixed/collections/generics"
"github.com/charlienet/go-mixed/hash"
)
func TestConcurrentMap(t *testing.T) {
@ -81,3 +83,86 @@ func BenchmarkRWLockMap(b *testing.B) {
}
})
}
func BenchmarkGetIndex(b *testing.B) {
b.Run("字符串-Sprintf", func(b *testing.B) {
v := "abc"
for i := 0; i < b.N; i++ {
bytes := []byte(fmt.Sprintf("%v", v))
hash.XXHashUint64(bytes)
}
})
b.Run("字符串", func(b *testing.B) {
v := "abc"
for i := 0; i < b.N; i++ {
getTag(v)
}
})
b.Run("数字", func(b *testing.B) {
v := 124
for i := 0; i < b.N; i++ {
getTag(v)
}
})
}
func getTag[T comparable](v T) uint64 {
var vv any = v
switch vv.(type) {
case string:
return getStringIndex([]byte(vv.(string)))
case int8:
return uint64(vv.(int8))
case uint8:
return uint64(vv.(uint8))
case int:
return uint64(vv.(int))
case int32:
return uint64(vv.(int32))
case uint32:
return uint64(vv.(uint32))
case int64:
return uint64(vv.(int64))
case uint64:
return vv.(uint64)
default:
return getStringIndex([]byte(fmt.Sprintf("%v", v)))
}
}
func getStringIndex(v []byte) uint64 {
if len(v) > 4 {
v = v[:4]
}
// return hash.XXHashUint64(v)
i, _ := bytesconv.BigEndian.BytesToUInt64(v)
return i
}
func TestFnv64(t *testing.T) {
t.Log(fnv64("a"))
t.Log(fnv64("b"))
t.Log(fnv64("c"))
t.Log(fnv64("d"))
t.Log(fnv64("e"))
}
const (
prime32 = uint64(16777619)
)
func fnv64(k string) uint64 {
var hash = uint64(2166136261)
l := len(k)
for i := 0; i < l; i++ {
hash *= prime32
hash ^= uint64(k[i])
}
return hash
}

View File

@ -29,3 +29,31 @@ func TestMapCount(t *testing.T) {
mm["a"] = "b"
assert.Equal(t, 1, len(mm))
}
func BenchmarkMap(b *testing.B) {
b.Run("RWLock", func(b *testing.B) {
m := generics.NewRWLockMap[string, string]()
doBenchamark(b, m)
})
b.Run("ConcurrnetMap", func(b *testing.B) {
doBenchamark(b, generics.NewConcurrnetMap[string, string]())
})
}
func doBenchamark(b *testing.B, m generics.Map[string, string]) {
var k = "abc"
var v = "bcd"
b.RunParallel(func(p *testing.PB) {
for p.Next() {
m.Set(k, v)
m.Get(k)
m.Get(k)
m.Get(k)
m.Delete(k)
}
})
}

View File

@ -1,6 +1,10 @@
package generics
import "sync"
import (
"sync"
"golang.org/x/exp/maps"
)
var _ Map[string, string] = &RWLockMap[string, string]{}
@ -41,10 +45,7 @@ func (m *RWLockMap[K, V]) ForEach(f func(K, V)) {
}
func (m *RWLockMap[K, V]) Clone() Map[K, V] {
new := make(map[K]V, m.Count())
for k, v := range m.m {
new[k] = v
}
new := maps.Clone(m.m)
return &RWLockMap[K, V]{
m: new,