Skip to content

Commit

Permalink
cmap: add comment to NewMapCache
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Sep 23, 2023
1 parent a921583 commit b20661a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/concurrent_map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

const (
mapShardSize = 64
MapShardSize = 64
)

type Hashable interface {
Expand All @@ -35,7 +35,7 @@ type Hashable interface {
type TestAndSetFunc[K comparable, V any] func(key K, v V, ok bool) (newV V, setV, deleteV bool)

type Map[K Hashable, V any] struct {
shards [mapShardSize]shard[K, V]
shards [MapShardSize]shard[K, V]
}

func NewMap[K Hashable, V any]() *Map[K, V] {
Expand All @@ -46,11 +46,12 @@ func NewMap[K Hashable, V any]() *Map[K, V] {
return m
}

// NewMapCache returns a cache with a maximum size.
// Note that, because this it has multiple (MapShardSize) shards,
// the actual maximum size is MapShardSize*(size / MapShardSize).
// If size <=0, it's equal to NewMap().
func NewMapCache[K Hashable, V any](size int) *Map[K, V] {
sizePreShard := size / mapShardSize
if size > 0 && sizePreShard == 0 {
sizePreShard = 1
}
sizePreShard := size / MapShardSize
m := new(Map[K, V])
for i := range m.shards {
m.shards[i] = newShard[K, V](sizePreShard)
Expand All @@ -59,7 +60,7 @@ func NewMapCache[K Hashable, V any](size int) *Map[K, V] {
}

func (m *Map[K, V]) getShard(key K) *shard[K, V] {
return &m.shards[key.Sum()%mapShardSize]
return &m.shards[key.Sum()%MapShardSize]
}

func (m *Map[K, V]) Get(key K) (V, bool) {
Expand Down Expand Up @@ -103,7 +104,7 @@ func (m *Map[K, V]) Flush() {

type shard[K comparable, V any] struct {
l sync.RWMutex
max int
max int // Negative or zero max means no limit.
m map[K]V
}

Expand Down

0 comments on commit b20661a

Please sign in to comment.