Skip to content

Commit

Permalink
go/cache: fix nilness issues and unused code (#14688)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Layher <[email protected]>
  • Loading branch information
mdlayher authored Dec 6, 2023
1 parent 3374403 commit 87449cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
42 changes: 17 additions & 25 deletions go/cache/theine/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,35 @@ const (
)

type Shard[K cachekey, V any] struct {
hashmap map[K]*Entry[K, V]
dookeeper *bf.Bloomfilter
deque *deque.Deque[*Entry[K, V]]
group *Group[K, V]
qsize uint
qlen int
counter uint
mu sync.RWMutex
hashmap map[K]*Entry[K, V]
doorkeeper *bf.Bloomfilter
deque *deque.Deque[*Entry[K, V]]
group *Group[K, V]
qsize uint
qlen int
counter uint
mu sync.RWMutex
}

func NewShard[K cachekey, V any](size uint, qsize uint, doorkeeper bool) *Shard[K, V] {
func NewShard[K cachekey, V any](qsize uint, doorkeeper bool) *Shard[K, V] {
s := &Shard[K, V]{
hashmap: make(map[K]*Entry[K, V]),
qsize: qsize,
deque: deque.New[*Entry[K, V]](),
group: NewGroup[K, V](),
}
if doorkeeper {
s.dookeeper = bf.New(0.01)
s.doorkeeper = bf.New(0.01)
}
return s
}

func (s *Shard[K, V]) set(key K, entry *Entry[K, V]) {
s.hashmap[key] = entry
if s.dookeeper != nil {
if s.doorkeeper != nil {
ds := 20 * len(s.hashmap)
if ds > s.dookeeper.Capacity {
s.dookeeper.EnsureCapacity(ds)
if ds > s.doorkeeper.Capacity {
s.doorkeeper.EnsureCapacity(ds)
}
}
}
Expand Down Expand Up @@ -195,10 +195,6 @@ func NewStore[K cachekey, V cacheval](maxsize int64, doorkeeper bool) *Store[K,
shardCount = 128
}
dequeSize := int(maxsize) / 100 / shardCount
shardSize := int(maxsize) / shardCount
if shardSize < 50 {
shardSize = 50
}
policySize := int(maxsize) - (dequeSize * shardCount)

s := &Store[K, V]{
Expand All @@ -213,7 +209,7 @@ func NewStore[K cachekey, V cacheval](maxsize int64, doorkeeper bool) *Store[K,
}
s.shards = make([]*Shard[K, V], 0, s.shardCount)
for i := 0; i < int(s.shardCount); i++ {
s.shards = append(s.shards, NewShard[K, V](uint(shardSize), uint(dequeSize), doorkeeper))
s.shards = append(s.shards, NewShard[K, V](uint(dequeSize), doorkeeper))
}

go s.maintenance()
Expand Down Expand Up @@ -329,11 +325,11 @@ func (s *Store[K, V]) setInternal(key K, value V, cost int64, epoch uint32) (*Sh
return shard, exist, true
}
if s.doorkeeper {
if shard.counter > uint(shard.dookeeper.Capacity) {
shard.dookeeper.Reset()
if shard.counter > uint(shard.doorkeeper.Capacity) {
shard.doorkeeper.Reset()
shard.counter = 0
}
hit := shard.dookeeper.Insert(h)
hit := shard.doorkeeper.Insert(h)
if !hit {
shard.counter += 1
shard.mu.Unlock()
Expand Down Expand Up @@ -373,7 +369,6 @@ func (s *Store[K, V]) processDeque(shard *Shard[K, V], epoch uint32) {
return
}
var evictedkv []dequeKV[K, V]
var expiredkv []dequeKV[K, V]

// send to slru
send := make([]*Entry[K, V], 0, 2)
Expand Down Expand Up @@ -422,9 +417,6 @@ func (s *Store[K, V]) processDeque(shard *Shard[K, V], epoch uint32) {
for _, kv := range evictedkv {
s.OnRemoval(kv.k, kv.v, EVICTED)
}
for _, kv := range expiredkv {
s.OnRemoval(kv.k, kv.v, EXPIRED)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions go/cache/theine/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func TestProcessDeque(t *testing.T) {
func TestDoorKeeperDynamicSize(t *testing.T) {
store := NewStore[keyint, cachedint](200000, true)
shard := store.shards[0]
require.True(t, shard.dookeeper.Capacity == 512)
require.True(t, shard.doorkeeper.Capacity == 512)
for i := keyint(0); i < 5000; i++ {
shard.set(i, &Entry[keyint, cachedint]{})
}
require.True(t, shard.dookeeper.Capacity > 100000)
require.True(t, shard.doorkeeper.Capacity > 100000)
}

0 comments on commit 87449cc

Please sign in to comment.