Skip to content

Commit

Permalink
simplify shard option
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Mar 22, 2024
1 parent 6400f3e commit a57f589
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,25 @@ type shardsOption[K comparable, V any] struct {
count uint32
}

func (o *shardsOption[K, V]) ApplyToLRUCache(c *LRUCache[K, V]) {
func (o *shardsOption[K, V]) getcount(maxcount uint32) uint32 {
var shardcount uint32
if o.count == 0 {
shardcount = nextPowOf2(uint32(runtime.GOMAXPROCS(0) * 16))
} else {
shardcount = nextPowOf2(o.count)
}
if maxcount := uint32(len(c.shards)); shardcount > maxcount {
if shardcount > maxcount {
shardcount = maxcount
}
return shardcount
}

c.mask = uint32(shardcount) - 1
func (o *shardsOption[K, V]) ApplyToLRUCache(c *LRUCache[K, V]) {
c.mask = o.getcount(uint32(len(c.shards))) - 1
}

func (o *shardsOption[K, V]) ApplyToTTLCache(c *TTLCache[K, V]) {
var shardcount uint32
if o.count == 0 {
shardcount = nextPowOf2(uint32(runtime.GOMAXPROCS(0) * 16))
} else {
shardcount = nextPowOf2(o.count)
}
if maxcount := uint32(len(c.shards)); shardcount > maxcount {
shardcount = maxcount
}

c.mask = uint32(shardcount) - 1
c.mask = o.getcount(uint32(len(c.shards))) - 1
}

// WithHasher specifies the hasher function of cache.
Expand Down

0 comments on commit a57f589

Please sign in to comment.