Skip to content

Commit

Permalink
static shards
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Feb 14, 2024
1 parent c750a43 commit d7d823d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Cache implements LRU Cache with least recent used eviction policy.
type Cache[K comparable, V any] struct {
shards []shard[K, V]
shards [512]shard[K, V]
mask uint32
hasher func(K) uint64
loader func(key K) (value V, ttl time.Duration, err error)
Expand Down Expand Up @@ -44,12 +44,12 @@ func New[K comparable, V any](size int, options ...Option[K, V]) *Cache[K, V] {
}

// pre-alloc lists and tables for compactness
shardsize := (size + len(c.shards) - 1) / len(c.shards)
shardlists := make([]node[K, V], (shardsize+1)*len(c.shards))
shardsize := (size + int(c.mask)) / (int(c.mask) + 1)
shardlists := make([]node[K, V], (shardsize+1)*(int(c.mask)+1))
tablesize := int(newTableSize(uint32(shardsize)))
tablebuckets := make([]struct{ hdib, index uint32 }, tablesize*len(c.shards))
tablebuckets := make([]struct{ hdib, index uint32 }, tablesize*(int(c.mask)+1))

for i := range c.shards {
for i := 0; i <= int(c.mask); i++ {
c.shards[i].list = shardlists[i*(shardsize+1) : (i+1)*(shardsize+1)]
c.shards[i].table.buckets = tablebuckets[i*tablesize : (i+1)*tablesize]
c.shards[i].Init(uint32(shardsize))
Expand Down Expand Up @@ -79,8 +79,7 @@ func (o *shardsOption[K, V]) ApplyToCache(c *Cache[K, V]) {
shardcount = nextPowOf2(o.count)
}

c.shards = make([]shard[K, V], shardcount)
c.mask = uint32(len(c.shards)) - 1
c.mask = uint32(shardcount) - 1
}

// WithHasher specifies the hasher function of cache.
Expand All @@ -106,7 +105,7 @@ type slidingOption[K comparable, V any] struct {
}

func (o *slidingOption[K, V]) ApplyToCache(c *Cache[K, V]) {
for i := range c.shards {
for i := 0; i <= int(c.mask); i++ {
c.shards[i].sliding = o.sliding
}
}
Expand Down Expand Up @@ -189,7 +188,7 @@ func (c *Cache[K, V]) Delete(key K) (prev V) {
// Len returns number of cached nodes.
func (c *Cache[K, V]) Len() int {
var n uint32
for i := range c.shards {
for i := 0; i <= int(c.mask); i++ {
n += c.shards[i].Len()
}
return int(n)
Expand All @@ -198,7 +197,7 @@ func (c *Cache[K, V]) Len() int {
// AppendKeys appends all keys to keys and return the keys.
func (c *Cache[K, V]) AppendKeys(keys []K) []K {
now := atomic.LoadUint32(&clock)
for i := range c.shards {
for i := 0; i <= int(c.mask); i++ {
keys = c.shards[i].AppendKeys(keys, now)
}
return keys
Expand All @@ -218,7 +217,7 @@ type Stats struct {

// Stats returns cache stats.
func (c *Cache[K, V]) Stats() (stats Stats) {
for i := range c.shards {
for i := 0; i <= int(c.mask); i++ {
c.shards[i].mu.Lock()
s := c.shards[i].stats
c.shards[i].mu.Unlock()
Expand Down

0 comments on commit d7d823d

Please sign in to comment.