refactor(cache): modernize with generics (#7842)

This commit is contained in:
vflaux
2026-02-04 02:23:53 +01:00
committed by GitHub
parent 923a8b5d2b
commit 30c20b52ff
13 changed files with 70 additions and 68 deletions

View File

@@ -16,52 +16,52 @@ func Hash(what []byte) uint64 {
}
// Cache is cache.
type Cache struct {
shards [shardSize]*shard
type Cache[T any] struct {
shards [shardSize]*shard[T]
}
// shard is a cache with random eviction.
type shard struct {
items map[uint64]any
type shard[T any] struct {
items map[uint64]T
size int
sync.RWMutex
}
// New returns a new cache.
func New(size int) *Cache {
func New[T any](size int) *Cache[T] {
ssize := max(size/shardSize, 4)
c := &Cache{}
c := &Cache[T]{}
// Initialize all the shards
for i := range shardSize {
c.shards[i] = newShard(ssize)
c.shards[i] = newShard[T](ssize)
}
return c
}
// Add adds a new element to the cache. If the element already exists it is overwritten.
// Returns true if an existing element was evicted to make room for this element.
func (c *Cache) Add(key uint64, el any) bool {
func (c *Cache[T]) Add(key uint64, el T) bool {
shard := key & (shardSize - 1)
return c.shards[shard].Add(key, el)
}
// Get looks up element index under key.
func (c *Cache) Get(key uint64) (any, bool) {
func (c *Cache[T]) Get(key uint64) (T, bool) {
shard := key & (shardSize - 1)
return c.shards[shard].Get(key)
}
// Remove removes the element indexed with key.
func (c *Cache) Remove(key uint64) {
func (c *Cache[T]) Remove(key uint64) {
shard := key & (shardSize - 1)
c.shards[shard].Remove(key)
}
// Len returns the number of elements in the cache.
func (c *Cache) Len() int {
func (c *Cache[T]) Len() int {
l := 0
for _, s := range &c.shards {
l += s.Len()
@@ -70,18 +70,18 @@ func (c *Cache) Len() int {
}
// Walk walks each shard in the cache.
func (c *Cache) Walk(f func(map[uint64]any, uint64) bool) {
func (c *Cache[T]) Walk(f func(map[uint64]T, uint64) bool) {
for _, s := range &c.shards {
s.Walk(f)
}
}
// newShard returns a new shard with size.
func newShard(size int) *shard { return &shard{items: make(map[uint64]any), size: size} }
func newShard[T any](size int) *shard[T] { return &shard[T]{items: make(map[uint64]T), size: size} }
// Add adds element indexed by key into the cache. Any existing element is overwritten
// Returns true if an existing element was evicted to make room for this element.
func (s *shard) Add(key uint64, el any) bool {
func (s *shard[T]) Add(key uint64, el T) bool {
eviction := false
s.Lock()
if len(s.items) >= s.size {
@@ -99,14 +99,14 @@ func (s *shard) Add(key uint64, el any) bool {
}
// Remove removes the element indexed by key from the cache.
func (s *shard) Remove(key uint64) {
func (s *shard[T]) Remove(key uint64) {
s.Lock()
delete(s.items, key)
s.Unlock()
}
// Evict removes a random element from the cache.
func (s *shard) Evict() {
func (s *shard[T]) Evict() {
s.Lock()
for k := range s.items {
delete(s.items, k)
@@ -116,7 +116,7 @@ func (s *shard) Evict() {
}
// Get looks up the element indexed under key.
func (s *shard) Get(key uint64) (any, bool) {
func (s *shard[T]) Get(key uint64) (T, bool) {
s.RLock()
el, found := s.items[key]
s.RUnlock()
@@ -124,7 +124,7 @@ func (s *shard) Get(key uint64) (any, bool) {
}
// Len returns the current length of the cache.
func (s *shard) Len() int {
func (s *shard[T]) Len() int {
s.RLock()
l := len(s.items)
s.RUnlock()
@@ -132,7 +132,7 @@ func (s *shard) Len() int {
}
// Walk walks the shard for each element the function f is executed while holding a write lock.
func (s *shard) Walk(f func(map[uint64]any, uint64) bool) {
func (s *shard[T]) Walk(f func(map[uint64]T, uint64) bool) {
s.RLock()
items := make([]uint64, len(s.items))
i := 0

View File

@@ -6,7 +6,7 @@ import (
func TestCacheAddAndGet(t *testing.T) {
const N = shardSize * 4
c := New(N)
c := New[int](N)
c.Add(1, 1)
if _, found := c.Get(1); !found {
@@ -25,7 +25,7 @@ func TestCacheAddAndGet(t *testing.T) {
}
func TestCacheLen(t *testing.T) {
c := New(4)
c := New[int](4)
c.Add(1, 1)
if l := c.Len(); l != 1 {
@@ -44,7 +44,7 @@ func TestCacheLen(t *testing.T) {
}
func TestCacheSharding(t *testing.T) {
c := New(shardSize)
c := New[int](shardSize)
for i := range shardSize * 2 {
c.Add(uint64(i), 1)
}
@@ -56,15 +56,15 @@ func TestCacheSharding(t *testing.T) {
}
func TestCacheWalk(t *testing.T) {
c := New(10)
c := New[int](10)
exp := make([]int, 10*2)
for i := range 10 * 2 {
c.Add(uint64(i), 1)
exp[i] = 1
}
got := make([]int, 10*2)
c.Walk(func(items map[uint64]any, key uint64) bool {
got[key] = items[key].(int)
c.Walk(func(items map[uint64]int, key uint64) bool {
got[key] = items[key]
return true
})
for i := range exp {
@@ -77,7 +77,7 @@ func TestCacheWalk(t *testing.T) {
func BenchmarkCache(b *testing.B) {
b.ReportAllocs()
c := New(4)
c := New[int](4)
for b.Loop() {
c.Add(1, 1)
c.Get(1)

View File

@@ -6,7 +6,7 @@ import (
)
func TestShardAddAndGet(t *testing.T) {
s := newShard(1)
s := newShard[int](1)
s.Add(1, 1)
if _, found := s.Get(1); !found {
@@ -24,7 +24,7 @@ func TestShardAddAndGet(t *testing.T) {
func TestAddEvict(t *testing.T) {
const size = 1024
s := newShard(size)
s := newShard[int](size)
for i := range size {
s.Add(uint64(i), 1)
@@ -38,7 +38,7 @@ func TestAddEvict(t *testing.T) {
}
func TestShardLen(t *testing.T) {
s := newShard(4)
s := newShard[int](4)
s.Add(1, 1)
if l := s.Len(); l != 1 {
@@ -57,7 +57,7 @@ func TestShardLen(t *testing.T) {
}
func TestShardEvict(t *testing.T) {
s := newShard(1)
s := newShard[int](1)
s.Add(1, 1)
s.Add(2, 2)
// 1 should be gone
@@ -68,7 +68,7 @@ func TestShardEvict(t *testing.T) {
}
func TestShardLenEvict(t *testing.T) {
s := newShard(4)
s := newShard[int](4)
s.Add(1, 1)
s.Add(2, 1)
s.Add(3, 1)
@@ -95,7 +95,7 @@ func TestShardLenEvict(t *testing.T) {
}
func TestShardEvictParallel(t *testing.T) {
s := newShard(shardSize)
s := newShard[struct{}](shardSize)
for i := range shardSize {
s.Add(uint64(i), struct{}{})
}
@@ -117,7 +117,7 @@ func TestShardEvictParallel(t *testing.T) {
}
func BenchmarkShard(b *testing.B) {
s := newShard(shardSize)
s := newShard[int](shardSize)
b.ResetTimer()
for i := range b.N {
k := uint64(i) % shardSize * 2
@@ -127,7 +127,7 @@ func BenchmarkShard(b *testing.B) {
}
func BenchmarkShardParallel(b *testing.B) {
s := newShard(shardSize)
s := newShard[int](shardSize)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for i := uint64(0); pb.Next(); i++ {