Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#80] Fixed uint32 capacity overflow #81

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewCache[K comparable, V any](c Config[K, V]) *Cache[K, V] {
cache := &Cache[K, V]{
nodeManager: nodeManager,
hashmap: hashmap,
policy: s3fifo.NewPolicy[K, V](uint32(c.Capacity)),
policy: s3fifo.NewPolicy[K, V](c.Capacity),
expiryPolicy: expPolicy,
readBuffers: readBuffers,
writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferCapacity, maxWriteBufferCapacity),
Expand Down Expand Up @@ -277,7 +277,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 uint32, onlyIfAbsent bool) bool {
cost := c.costFunc(key, value)
if cost > c.policy.MaxAvailableCost() {
if int(cost) > c.policy.MaxAvailableCost() {
c.stats.IncRejectedSets()
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCache_SetWithCost(t *testing.T) {
},
})

goodCost := int(c.policy.MaxAvailableCost())
goodCost := c.policy.MaxAvailableCost()
badCost := goodCost + 1

added := c.Set(goodCost, 1)
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,11 +22,11 @@ const maxReinsertions = 20

type main[K comparable, V any] struct {
q *queue[K, V]
cost uint32
maxCost uint32
cost int
maxCost int
}

func newMain[K comparable, V any](maxCost uint32) *main[K, V] {
func newMain[K comparable, V any](maxCost int) *main[K, V] {
return &main[K, V]{
q: newQueue[K, V](),
maxCost: maxCost,
Expand All @@ -36,7 +36,7 @@ func newMain[K comparable, V any](maxCost uint32) *main[K, V] {
func (m *main[K, V]) insert(n node.Node[K, V]) {
m.q.push(n)
n.MarkMain()
m.cost += n.Cost()
m.cost += int(n.Cost())
}

func (m *main[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {
Expand All @@ -46,15 +46,15 @@ func (m *main[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {

if !n.IsAlive() || n.HasExpired() || n.Frequency() == 0 {
n.Unmark()
m.cost -= n.Cost()
m.cost -= int(n.Cost())
return append(deleted, n)
}

// to avoid the worst case O(n), we remove the 20th reinserted consecutive element.
reinsertions++
if reinsertions >= maxReinsertions {
n.Unmark()
m.cost -= n.Cost()
m.cost -= int(n.Cost())
return append(deleted, n)
}

Expand All @@ -65,7 +65,7 @@ func (m *main[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {
}

func (m *main[K, V]) remove(n node.Node[K, V]) {
m.cost -= n.Cost()
m.cost -= int(n.Cost())
n.Unmark()
m.q.remove(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]
maxCost uint32
maxAvailableNodeCost uint32
maxCost int
maxAvailableNodeCost int
}

// NewPolicy creates a new Policy.
func NewPolicy[K comparable, V any](maxCost uint32) *Policy[K, V] {
func NewPolicy[K comparable, V any](maxCost int) *Policy[K, V] {
smallMaxCost := maxCost / 10
mainMaxCost := maxCost - smallMaxCost

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

// MaxAvailableCost returns the maximum available cost of the node.
func (p *Policy[K, V]) MaxAvailableCost() uint32 {
func (p *Policy[K, V]) MaxAvailableCost() int {
return p.maxAvailableNodeCost
}

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,12 +22,12 @@ type small[K comparable, V any] struct {
q *queue[K, V]
main *main[K, V]
ghost *ghost[K, V]
cost uint32
maxCost uint32
cost int
maxCost int
}

func newSmall[K comparable, V any](
maxCost uint32,
maxCost int,
main *main[K, V],
ghost *ghost[K, V],
) *small[K, V] {
Expand All @@ -42,7 +42,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.cost += n.Cost()
s.cost += int(n.Cost())
}

func (s *small[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {
Expand All @@ -51,7 +51,7 @@ func (s *small[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {
}

n := s.q.pop()
s.cost -= n.Cost()
s.cost -= int(n.Cost())
n.Unmark()
if !n.IsAlive() || n.HasExpired() {
return append(deleted, n)
Expand All @@ -70,7 +70,7 @@ func (s *small[K, V]) evict(deleted []node.Node[K, V]) []node.Node[K, V] {
}

func (s *small[K, V]) remove(n node.Node[K, V]) {
s.cost -= n.Cost()
s.cost -= int(n.Cost())
n.Unmark()
s.q.remove(n)
}
Expand Down
Loading