Skip to content

Commit

Permalink
add: dict add lazy eviction
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Jun 17, 2024
1 parent 73f78ad commit 5e851c8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ heap:
build-docker:
docker build -t rotom .

bench:
go test -bench . -benchmem

clean:
rm -f *.aof
rm -f coverage.html
Expand Down
4 changes: 2 additions & 2 deletions dict/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func BenchmarkRemove(b *testing.B) {

func BenchmarkMigrate(b *testing.B) {
b.Run("stdmap", func(b *testing.B) {
m := getStdmap(100000)
m := getStdmap(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
maps.Clone(m)
}
})
b.Run("dict", func(b *testing.B) {
m := getDict(100000)
m := getDict(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Migrate()
Expand Down
6 changes: 3 additions & 3 deletions dict/benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
c := ""
entries := 0
flag.StringVar(&c, "cache", "dict", "map to bench.")
flag.IntVar(&entries, "entries", 2000*10000, "number of entries to test.")
flag.IntVar(&entries, "entries", 1000*10000, "number of entries to test.")
flag.Parse()

fmt.Println(c)
Expand All @@ -51,7 +51,7 @@ func main() {
val []byte
ts int64
}
m := make(map[string]Item)
m := make(map[string]Item, entries)
for i := 0; i < entries; i++ {
k, v := genKV(i)
m[string(k)] = Item{val: v, ts: 0}
Expand All @@ -62,7 +62,7 @@ func main() {
val []byte
ts int64
}
m := swiss.New[string, Item](8)
m := swiss.New[string, Item](entries)
for i := 0; i < entries; i++ {
k, v := genKV(i)
m.Put(string(k), Item{val: v, ts: 0})
Expand Down
38 changes: 22 additions & 16 deletions dict/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ func (dict *Dict) getShard(key string) *shard {
func (dict *Dict) Get(key string) ([]byte, int64, bool) {
shard := dict.getShard(key)
idx, ok := shard.index.Get(key)
if ok && !idx.expired() {
_, val := shard.findEntry(idx)
return val, idx.lo, ok
if !ok {
return nil, 0, false
}
return nil, 0, false
if idx.expired() {
shard.removeEntry(key, idx)
return nil, 0, false
}
_, val := shard.findEntry(idx)
return val, idx.lo, ok
}

func (dict *Dict) SetTx(key string, val []byte, expiration int64) bool {
Expand Down Expand Up @@ -85,29 +89,32 @@ func (dict *Dict) SetEx(kstr string, value []byte, duration time.Duration) bool
func (dict *Dict) Remove(key string) bool {
shard := dict.getShard(key)
idx, ok := shard.index.Get(key)
if ok {
shard.removeEntry(key, idx)
return !idx.expired()
if !ok {
return false
}
return false
shard.removeEntry(key, idx)
return !idx.expired()
}

func (dict *Dict) SetTTL(key string, expiration int64) bool {
shard := dict.getShard(key)
idx, ok := shard.index.Get(key)
if ok && !idx.expired() {
shard.index.Put(key, idx.setTTL(expiration))
return true
if !ok {
return false
}
if idx.expired() {
shard.removeEntry(key, idx)
return false
}
return false
shard.index.Put(key, idx.setTTL(expiration))
return true
}

type Walker func(key string, value []byte, ttl int64) (next bool)

func (dict *Dict) Scan(callback Walker) {
for _, shard := range dict.shards {
next := shard.scan(callback)
if !next {
if !shard.scan(callback) {
return
}
}
Expand All @@ -121,8 +128,7 @@ func (dict *Dict) Migrate() {

func (dict *Dict) EvictExpired() {
id := rand.IntN(len(dict.shards))
shard := dict.shards[id]
shard.evictExpired()
dict.shards[id].evictExpired()
}

// Stats represents the runtime statistics of Dict.
Expand Down
2 changes: 1 addition & 1 deletion dict/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

func genKV(i int) (string, []byte) {
k := fmt.Sprintf("%08x", i)
k := fmt.Sprintf("%09x", i)
return k, []byte(k)
}
7 changes: 4 additions & 3 deletions dict/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type Options struct {
}

var DefaultOptions = Options{
ShardCount: 1024,
IndexSize: 1024,
BufferSize: 64 * KB,
ShardCount: 1024,
IndexSize: 1024,
BufferSize: 64 * KB,
MigrateRatio: 0.4,
}

func validateOptions(options Options) error {
Expand Down

0 comments on commit 5e851c8

Please sign in to comment.