-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add zipmap, zipset based on listpack
- Loading branch information
1 parent
bf3f6c3
commit ffc9532
Showing
17 changed files
with
393 additions
and
114 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
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,52 @@ | ||
package hash | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func genKey(i int) string { | ||
return fmt.Sprintf("%08x", i) | ||
} | ||
|
||
func BenchmarkMap(b *testing.B) { | ||
benchMapI("map", func() MapI { return NewMap() }, b) | ||
benchMapI("zipmap", func() MapI { return NewZipMap() }, b) | ||
} | ||
|
||
func benchMapI(name string, newf func() MapI, b *testing.B) { | ||
b.Run(name+"-set", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
m := newf() | ||
for i := 0; i < 512; i++ { | ||
k := genKey(i) | ||
m.Set(k, []byte(k)) | ||
} | ||
} | ||
}) | ||
b.Run(name+"-update", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
m := newf() | ||
for i := 0; i < 512; i++ { | ||
k := genKey(0) | ||
m.Set(k, []byte(k)) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkSet(b *testing.B) { | ||
benchSetI("set", func() SetI { return NewSet() }, b) | ||
benchSetI("zipset", func() SetI { return NewZipSet() }, b) | ||
} | ||
|
||
func benchSetI(name string, newf func() SetI, b *testing.B) { | ||
b.Run(name+"-add", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
m := newf() | ||
for i := 0; i < 512; i++ { | ||
m.Add(genKey(i)) | ||
} | ||
} | ||
}) | ||
} |
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,64 @@ | ||
package hash | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMap(t *testing.T) { | ||
testMapI(NewMap(), t) | ||
testMapI(NewZipMap(), t) | ||
} | ||
|
||
func testMapI(m MapI, t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
// set | ||
assert.True(m.Set("key1", []byte("val1"))) | ||
assert.True(m.Set("key2", []byte("val2"))) | ||
assert.True(m.Set("key3", []byte("val3"))) | ||
|
||
// len | ||
assert.Equal(m.Len(), 3) | ||
|
||
// get | ||
val, ok := m.Get("key1") | ||
assert.True(ok) | ||
assert.Equal(string(val), "val1") | ||
|
||
val, ok = m.Get("key2") | ||
assert.True(ok) | ||
assert.Equal(string(val), "val2") | ||
|
||
val, ok = m.Get("key3") | ||
assert.True(ok) | ||
assert.Equal(string(val), "val3") | ||
|
||
// set(update) | ||
assert.False(m.Set("key1", []byte("newval1"))) | ||
assert.False(m.Set("key2", []byte("newval2"))) | ||
assert.False(m.Set("key3", []byte("newval3"))) | ||
|
||
// get(update) | ||
val, ok = m.Get("key1") | ||
assert.True(ok) | ||
assert.Equal(string(val), "newval1") | ||
|
||
val, ok = m.Get("key2") | ||
assert.True(ok) | ||
assert.Equal(string(val), "newval2") | ||
|
||
val, ok = m.Get("key3") | ||
assert.True(ok) | ||
assert.Equal(string(val), "newval3") | ||
|
||
// remove | ||
assert.True(m.Remove("key1")) | ||
assert.True(m.Remove("key2")) | ||
assert.True(m.Remove("key3")) | ||
assert.False(m.Remove("notexist")) | ||
|
||
// len | ||
assert.Equal(m.Len(), 0) | ||
} |
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 |
---|---|---|
@@ -1,39 +1,34 @@ | ||
package hash | ||
|
||
import ( | ||
"github.com/cockroachdb/swiss" | ||
"github.com/xgzlucario/rotom/internal/pkg" | ||
mapset "github.com/deckarep/golang-set/v2" | ||
) | ||
|
||
var ( | ||
setAllocator = pkg.NewAllocator[string, struct{}]() | ||
) | ||
type SetI interface { | ||
Add(key string) (ok bool) | ||
Remove(key string) (ok bool) | ||
Pop() (key string, ok bool) | ||
Len() int | ||
} | ||
|
||
var _ SetI = (*Set)(nil) | ||
|
||
type Set struct { | ||
m *swiss.Map[string, struct{}] | ||
mapset.Set[string] | ||
} | ||
|
||
func NewSet() *Set { | ||
return &Set{m: swiss.New(8, swiss.WithAllocator(setAllocator))} | ||
return &Set{mapset.NewThreadUnsafeSet[string]()} | ||
} | ||
|
||
func (s *Set) Add(key string) bool { | ||
if _, ok := s.m.Get(key); ok { | ||
func (s Set) Remove(key string) bool { | ||
if !s.ContainsOne(key) { | ||
return false | ||
} | ||
s.m.Put(key, struct{}{}) | ||
s.Set.Remove(key) | ||
return true | ||
} | ||
|
||
func (s *Set) Pop() (item string, ok bool) { | ||
s.m.All(func(key string, _ struct{}) bool { | ||
s.m.Delete(key) | ||
item, ok = key, true | ||
return false | ||
}) | ||
return | ||
} | ||
|
||
func (s *Set) Free() { | ||
s.m.Close() | ||
func (s Set) Len() int { | ||
return s.Cardinality() | ||
} |
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,71 @@ | ||
package hash | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/xgzlucario/rotom/internal/list" | ||
) | ||
|
||
var _ MapI = (*ZipMap)(nil) | ||
|
||
// ZipMap store datas as [key1, val1, key2, val2...] in listpack. | ||
type ZipMap struct { | ||
m *list.ListPack | ||
} | ||
|
||
func NewZipMap() *ZipMap { | ||
return &ZipMap{list.NewListPack()} | ||
} | ||
|
||
func (zm *ZipMap) Set(key string, val []byte) (newField bool) { | ||
it := zm.m.Iterator().SeekLast() | ||
for !it.IsFirst() { | ||
it.Prev() | ||
keyBytes := it.Prev() | ||
if key == b2s(keyBytes) { | ||
// update val | ||
it.Next() | ||
it.ReplaceNext(b2s(val)) | ||
return false | ||
} | ||
} | ||
zm.m.RPush(key, b2s(val)) | ||
return true | ||
} | ||
|
||
func (zm *ZipMap) Get(key string) ([]byte, bool) { | ||
it := zm.m.Iterator().SeekLast() | ||
for !it.IsFirst() { | ||
valBytes := it.Prev() | ||
keyBytes := it.Prev() | ||
if key == b2s(keyBytes) { | ||
return valBytes, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (zm *ZipMap) Remove(key string) bool { | ||
it := zm.m.Iterator().SeekLast() | ||
for !it.IsFirst() { | ||
it.Prev() | ||
keyBytes := it.Prev() | ||
if key == b2s(keyBytes) { | ||
it.RemoveNext() | ||
it.RemoveNext() | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (zm *ZipMap) Len() int { | ||
return zm.m.Size() / 2 | ||
} | ||
|
||
func (zm *ZipMap) Scan(fn func(string, []byte)) { | ||
} | ||
|
||
func b2s(b []byte) string { | ||
return *(*string)(unsafe.Pointer(&b)) | ||
} |
Oops, something went wrong.