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

Use sync.Pool to use &item{} #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"

Expand All @@ -36,6 +37,12 @@ const (

type onEvictFunc func(uint64, uint64, interface{}, int64)

var itemPool = &sync.Pool{
New: func() interface{} {
return &item{}
},
}

// Cache is a thread-safe implementation of a hashmap with a TinyLFU admission
// policy and a Sampled LFU eviction policy. You can use the same Cache instance
// from as many goroutines as you want.
Expand Down Expand Up @@ -128,6 +135,10 @@ type item struct {
expiration time.Time
}

func (i *item) reset() {
i.flag, i.key, i.conflict, i.value, i.cost, i.expiration = 0, 0, 0, nil, 0, time.Time{}
}

// NewCache returns a new Cache instance and any configuration errors, if any.
func NewCache(config *Config) (*Cache, error) {
switch {
Expand Down Expand Up @@ -216,14 +227,13 @@ func (c *Cache) SetWithTTL(key, value interface{}, cost int64, ttl time.Duration
}

keyHash, conflictHash := c.keyToHash(key)
i := &item{
flag: itemNew,
key: keyHash,
conflict: conflictHash,
value: value,
cost: cost,
expiration: expiration,
}
i := itemPool.Get().(*item)
i.flag = itemNew
i.key = keyHash
i.conflict = conflictHash
i.value = value
i.cost = cost
i.expiration = expiration
// cost is eventually updated. The expiration must also be immediately updated
// to prevent items from being prematurely removed from the map.
if c.store.Update(i) {
Expand Down Expand Up @@ -251,11 +261,11 @@ func (c *Cache) Del(key interface{}) {
// So we must push the same item to `setBuf` with the deletion flag.
// This ensures that if a set is followed by a delete, it will be
// applied in the correct order.
c.setBuf <- &item{
flag: itemDelete,
key: keyHash,
conflict: conflictHash,
}
i := itemPool.Get().(*item)
i.flag = itemDelete
i.key = keyHash
i.conflict = conflictHash
c.setBuf <- i
}

// Close stops all goroutines and closes all channels.
Expand Down Expand Up @@ -314,6 +324,8 @@ func (c *Cache) processItems() {
if c.onEvict != nil {
c.onEvict(victim.key, victim.conflict, victim.value, victim.cost)
}
victim.reset()
itemPool.Put(victim)
}

case itemUpdate:
Expand All @@ -323,6 +335,8 @@ func (c *Cache) processItems() {
c.policy.Del(i.key) // Deals with metrics updates.
c.store.Del(i.key, i.conflict)
}
i.reset()
itemPool.Put(i)
case <-c.cleanupTicker.C:
c.store.Cleanup(c.policy, c.onEvict)
case <-c.stop:
Expand Down
16 changes: 16 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,22 @@ func TestCacheMetricsClear(t *testing.T) {
c.Metrics.Clear()
}

func BenchmarkConcurrentCacheSetNeverEvict(b *testing.B) {
c, err := NewCache(&Config{
NumCounters: 100,
MaxCost: 10,
BufferItems: 64,
})
require.NoError(b, err)
b.RunParallel(func(pb *testing.PB) {
cnt := rand.Int()
for pb.Next() {
cnt++
c.Set(cnt, cnt, 0)
}
})
}

func init() {
// Set bucketSizeSecs to 1 to avoid waiting too much during the tests.
bucketDurationSecs = 1
Expand Down
8 changes: 3 additions & 5 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,9 @@ func (p *defaultPolicy) Add(key uint64, cost int64) ([]*item, bool) {
sample[minId] = sample[len(sample)-1]
sample = sample[:len(sample)-1]
// Store victim in evicted victims slice.
victims = append(victims, &item{
key: minKey,
conflict: 0,
cost: minCost,
})
i := itemPool.Get().(*item)
i.key, i.conflict, i.cost = minKey, 0, minCost
victims = append(victims, i)
}
p.evict.add(key, cost)
p.metrics.add(costAdd, key, uint64(cost))
Expand Down