Skip to content

Commit

Permalink
Fix in initial capacity allocation (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ac-rappi authored Aug 27, 2020
1 parent 21e5ca5 commit 0251553
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func newBigCache(config Config, clock clock) (*BigCache, error) {
hash: config.Hasher,
config: config,
shardMask: uint64(config.Shards - 1),
maxShardSize: uint32(config.maximumShardSize()),
maxShardSize: uint32(config.maximumShardSizeInBytes()),
close: make(chan struct{}),
}

Expand Down
27 changes: 27 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,33 @@ func TestCacheCapacity(t *testing.T) {
assertEqual(t, 40960, cache.Capacity())
}

func TestCacheInitialCapacity(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(Config{
Shards: 1,
LifeWindow: time.Second,
MaxEntriesInWindow: 2 * 1024,
HardMaxCacheSize: 1,
MaxEntrySize: 1024,
})

assertEqual(t, 0, cache.Len())
assertEqual(t, 1024*1024, cache.Capacity())

keys := 1024 * 1024

// when
for i := 0; i < keys; i++ {
cache.Set(fmt.Sprintf("key%d", i), []byte("value"))
}

// then
assertEqual(t, true, cache.Len() < keys)
assertEqual(t, 1024*1024, cache.Capacity())
}

func TestRemoveEntriesWhenShardIsFull(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func DefaultConfig(eviction time.Duration) Config {

// initialShardSize computes initial shard size
func (c Config) initialShardSize() int {
return max(min(c.MaxEntriesInWindow/c.Shards, c.maximumShardSize()), minimumEntriesInShard)
return max(c.MaxEntriesInWindow/c.Shards, minimumEntriesInShard)
}

// maximumShardSize computes maximum shard size
func (c Config) maximumShardSize() int {
// maximumShardSizeInBytes computes maximum shard size in bytes
func (c Config) maximumShardSizeInBytes() int {
maxShardSize := 0

if c.HardMaxCacheSize > 0 {
Expand Down
7 changes: 6 additions & 1 deletion shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,15 @@ func (s *cacheShard) collision() {
}

func initNewShard(config Config, callback onRemoveCallback, clock clock) *cacheShard {
bytesQueueInitialCapacity := config.initialShardSize() * config.MaxEntrySize
maximumShardSizeInBytes := config.maximumShardSizeInBytes()
if maximumShardSizeInBytes > 0 && bytesQueueInitialCapacity > maximumShardSizeInBytes {
bytesQueueInitialCapacity = maximumShardSizeInBytes
}
return &cacheShard{
hashmap: make(map[uint64]uint32, config.initialShardSize()),
hashmapStats: make(map[uint64]uint32, config.initialShardSize()),
entries: *queue.NewBytesQueue(config.initialShardSize()*config.MaxEntrySize, config.maximumShardSize(), config.Verbose),
entries: *queue.NewBytesQueue(bytesQueueInitialCapacity, maximumShardSizeInBytes, config.Verbose),
entryBuffer: make([]byte, config.MaxEntrySize+headersSizeInBytes),
onRemove: callback,

Expand Down

0 comments on commit 0251553

Please sign in to comment.