Skip to content

Commit

Permalink
[Chore] Use uint64 instead of int for capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Aug 30, 2024
1 parent 00095e5 commit 0dede52
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 50 deletions.
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

// Builder is a one-shot builder for creating a cache instance.
type Builder[K comparable, V any] struct {
capacity *int
capacity *uint64
initialCapacity *int
statsCollector StatsCollector
ttl *time.Duration
Expand All @@ -33,7 +33,7 @@ type Builder[K comparable, V any] struct {
}

// NewBuilder creates a builder and sets the future cache capacity.
func NewBuilder[K comparable, V any](capacity int) *Builder[K, V] {
func NewBuilder[K comparable, V any](capacity uint64) *Builder[K, V] {
return &Builder[K, V]{
capacity: &capacity,
weigher: func(key K, value V) uint32 {
Expand Down
4 changes: 2 additions & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
)

func TestBuilder_NewFailed(t *testing.T) {
_, err := NewBuilder[int, int](-63).Build()
_, err := NewBuilder[int, int](0).Build()
if err == nil {
t.Fatalf("should fail with an error")
}

capacity := 100
capacity := uint64(100)
// negative const ttl
_, err = NewBuilder[int, int](capacity).WithTTL(-1).Build()
if err == nil {
Expand Down
9 changes: 1 addition & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ type Cache[K comparable, V any] struct {
doneClose chan struct{}
weigher func(key K, value V) uint32
deletionListener func(key K, value V, cause DeletionCause)
capacity int
mask uint32
ttl time.Duration
withExpiration bool
Expand Down Expand Up @@ -139,7 +138,6 @@ func newCache[K comparable, V any](b *Builder[K, V]) *Cache[K, V] {
mask: uint32(maxStripedBufferSize - 1),
weigher: b.weigher,
deletionListener: b.deletionListener,
capacity: *b.capacity,
}

cache.policy = s3fifo.NewPolicy(*b.capacity, cache.evictNode)
Expand Down Expand Up @@ -285,7 +283,7 @@ func (c *Cache[K, V]) SetIfAbsentWithTTL(key K, value V, ttl time.Duration) bool

func (c *Cache[K, V]) set(key K, value V, expiration int64, onlyIfAbsent bool) bool {
weight := c.weigher(key, value)
if int(weight) > c.policy.MaxAvailableWeight() {
if uint64(weight) > c.policy.MaxAvailableWeight() {
c.stats.CollectRejectedSets(1)
return false
}
Expand Down Expand Up @@ -487,11 +485,6 @@ func (c *Cache[K, V]) Size() int {
return c.hashmap.Size()
}

// Capacity returns the cache capacity.
func (c *Cache[K, V]) Capacity() int {
return c.capacity
}

// Extension returns access to inspect and perform low-level operations on this cache based on its runtime
// characteristics. These operations are optional and dependent on how the cache was constructed
// and what abilities the implementation exposes.
Expand Down
43 changes: 22 additions & 21 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func getRandomSize(t *testing.T) int {
}

func TestCache_SetWithWeight(t *testing.T) {
size := 10
c, err := NewBuilder[int, int](size).
Weigher(func(key int, value int) uint32 {
return uint32(key)
size := uint64(10)
c, err := NewBuilder[uint32, int](size).
Weigher(func(key uint32, value int) uint32 {
return key
}).
Build()
if err != nil {
Expand All @@ -54,26 +54,26 @@ func TestCache_SetWithWeight(t *testing.T) {
goodWeight := c.policy.MaxAvailableWeight()
badWeight := goodWeight + 1

added := c.Set(goodWeight, 1)
added := c.Set(uint32(goodWeight), 1)
if !added {
t.Fatalf("Set was dropped, even though it shouldn't have been. Max available weight: %d, actual weight: %d",
c.policy.MaxAvailableWeight(),
c.weigher(goodWeight, 1),
c.weigher(uint32(goodWeight), 1),
)
}
added = c.Set(badWeight, 1)
added = c.Set(uint32(badWeight), 1)
if added {
t.Fatalf("Set wasn't dropped, though it should have been. Max available weight: %d, actual weight: %d",
c.policy.MaxAvailableWeight(),
c.weigher(badWeight, 1),
c.weigher(uint32(badWeight), 1),
)
}
}

func TestCache_Range(t *testing.T) {
size := 10
ttl := time.Hour
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
WithTTL(ttl).
Build()
if err != nil {
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestCache_Range(t *testing.T) {

func TestCache_Close(t *testing.T) {
size := 10
c, err := NewBuilder[int, int](size).Build()
c, err := NewBuilder[int, int](uint64(size)).Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestCache_Close(t *testing.T) {

func TestCache_Clear(t *testing.T) {
size := 10
c, err := NewBuilder[int, int](size).Build()
c, err := NewBuilder[int, int](uint64(size)).Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
}
Expand All @@ -164,7 +164,7 @@ func TestCache_Set(t *testing.T) {
var mutex sync.Mutex
m := make(map[DeletionCause]int)
statsCounter := stats.NewCounter()
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
WithTTL(time.Minute).
CollectStats(statsCounter).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestCache_Set(t *testing.T) {
func TestCache_SetIfAbsent(t *testing.T) {
size := getRandomSize(t)
statsCounter := stats.NewCounter()
c, err := NewBuilder[int, int](size).WithTTL(time.Hour).CollectStats(statsCounter).Build()
c, err := NewBuilder[int, int](uint64(size)).WithTTL(time.Hour).CollectStats(statsCounter).Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
}
Expand All @@ -253,7 +253,7 @@ func TestCache_SetIfAbsent(t *testing.T) {

c.Clear()

cc, err := NewBuilder[int, int](size).WithVariableTTL().CollectStats(statsCounter).Build()
cc, err := NewBuilder[int, int](uint64(size)).WithVariableTTL().CollectStats(statsCounter).Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestCache_SetWithTTL(t *testing.T) {
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
InitialCapacity(size).
WithTTL(time.Second).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestCache_SetWithTTL(t *testing.T) {

m = make(map[DeletionCause]int)
statsCounter := stats.NewCounter()
cc, err := NewBuilder[int, int](size).
cc, err := NewBuilder[int, int](uint64(size)).
WithVariableTTL().
CollectStats(statsCounter).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestCache_Delete(t *testing.T) {
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
InitialCapacity(size).
WithTTL(time.Hour).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestCache_DeleteByFunc(t *testing.T) {
size := getRandomSize(t)
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
InitialCapacity(size).
WithTTL(time.Hour).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -457,7 +457,7 @@ func TestCache_DeleteByFunc(t *testing.T) {
func TestCache_Advanced(t *testing.T) {
size := getRandomSize(t)
defaultTTL := time.Hour
c, err := NewBuilder[int, int](size).
c, err := NewBuilder[int, int](uint64(size)).
WithTTL(defaultTTL).
Build()
if err != nil {
Expand Down Expand Up @@ -512,7 +512,8 @@ func TestCache_Ratio(t *testing.T) {
var mutex sync.Mutex
m := make(map[DeletionCause]int)
statsCounter := stats.NewCounter()
c, err := NewBuilder[uint64, uint64](100).
capacity := 100
c, err := NewBuilder[uint64, uint64](uint64(100)).
CollectStats(statsCounter).
DeletionListener(func(key uint64, value uint64, cause DeletionCause) {
mutex.Lock()
Expand All @@ -536,7 +537,7 @@ func TestCache_Ratio(t *testing.T) {
}
}

t.Logf("actual size: %d, capacity: %d", c.Size(), c.Capacity())
t.Logf("actual size: %d, capacity: %d", c.Size(), capacity)
t.Logf("actual: %.2f, optimal: %.2f", statsCounter.Snapshot().HitRatio(), o.Ratio())

mutex.Lock()
Expand Down
14 changes: 7 additions & 7 deletions internal/s3fifo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const maxReinsertions = 20

type main[K comparable, V any] struct {
q *queue[K, V]
weight int
maxWeight int
weight uint64
maxWeight uint64
evictNode func(node.Node[K, V])
}

func newMain[K comparable, V any](maxWeight int, evictNode func(node.Node[K, V])) *main[K, V] {
func newMain[K comparable, V any](maxWeight uint64, evictNode func(node.Node[K, V])) *main[K, V] {
return &main[K, V]{
q: newQueue[K, V](),
maxWeight: maxWeight,
Expand All @@ -38,7 +38,7 @@ func newMain[K comparable, V any](maxWeight int, evictNode func(node.Node[K, V])
func (m *main[K, V]) insert(n node.Node[K, V]) {
m.q.push(n)
n.MarkMain()
m.weight += int(n.Weight())
m.weight += uint64(n.Weight())
}

func (m *main[K, V]) evict(nowNanos int64) {
Expand All @@ -48,7 +48,7 @@ func (m *main[K, V]) evict(nowNanos int64) {

if !n.IsAlive() || n.HasExpired(nowNanos) || n.Frequency() == 0 {
n.Unmark()
m.weight -= int(n.Weight())
m.weight -= uint64(n.Weight())
m.evictNode(n)
return
}
Expand All @@ -57,7 +57,7 @@ func (m *main[K, V]) evict(nowNanos int64) {
reinsertions++
if reinsertions >= maxReinsertions {
n.Unmark()
m.weight -= int(n.Weight())
m.weight -= uint64(n.Weight())
m.evictNode(n)
return
}
Expand All @@ -68,7 +68,7 @@ func (m *main[K, V]) evict(nowNanos int64) {
}

func (m *main[K, V]) delete(n node.Node[K, V]) {
m.weight -= int(n.Weight())
m.weight -= uint64(n.Weight())
n.Unmark()
m.q.delete(n)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/s3fifo/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type Policy[K comparable, V any] struct {
small *small[K, V]
main *main[K, V]
ghost *ghost[K, V]
maxWeight int
maxAvailableNodeWeight int
maxWeight uint64
maxAvailableNodeWeight uint64
}

// NewPolicy creates a new Policy.
func NewPolicy[K comparable, V any](maxWeight int, evictNode func(node.Node[K, V])) *Policy[K, V] {
func NewPolicy[K comparable, V any](maxWeight uint64, evictNode func(node.Node[K, V])) *Policy[K, V] {
smallMaxWeight := maxWeight / 10
mainMaxWeight := maxWeight - smallMaxWeight

Expand Down Expand Up @@ -94,7 +94,7 @@ func (p *Policy[K, V]) Delete(n node.Node[K, V]) {
}

// MaxAvailableWeight returns the maximum available weight of the node.
func (p *Policy[K, V]) MaxAvailableWeight() int {
func (p *Policy[K, V]) MaxAvailableWeight() uint64 {
return p.maxAvailableNodeWeight
}

Expand Down
12 changes: 6 additions & 6 deletions internal/s3fifo/small.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type small[K comparable, V any] struct {
q *queue[K, V]
main *main[K, V]
ghost *ghost[K, V]
weight int
maxWeight int
weight uint64
maxWeight uint64
evictNode func(node.Node[K, V])
}

func newSmall[K comparable, V any](
maxWeight int,
maxWeight uint64,
main *main[K, V],
ghost *ghost[K, V],
evictNode func(node.Node[K, V]),
Expand All @@ -45,7 +45,7 @@ func newSmall[K comparable, V any](
func (s *small[K, V]) insert(n node.Node[K, V]) {
s.q.push(n)
n.MarkSmall()
s.weight += int(n.Weight())
s.weight += uint64(n.Weight())
}

func (s *small[K, V]) evict(nowNanos int64) {
Expand All @@ -54,7 +54,7 @@ func (s *small[K, V]) evict(nowNanos int64) {
}

n := s.q.pop()
s.weight -= int(n.Weight())
s.weight -= uint64(n.Weight())
n.Unmark()
if !n.IsAlive() || n.HasExpired(nowNanos) {
s.evictNode(n)
Expand All @@ -74,7 +74,7 @@ func (s *small[K, V]) evict(nowNanos int64) {
}

func (s *small[K, V]) delete(n node.Node[K, V]) {
s.weight -= int(n.Weight())
s.weight -= uint64(n.Weight())
n.Unmark()
s.q.delete(n)
}
Expand Down

0 comments on commit 0dede52

Please sign in to comment.