Commit 94c42ea7 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Be more aggressive about growing the packet cache.

Also add a test.
parent f3b58bbf
......@@ -204,14 +204,16 @@ func (cache *Cache) ResizeCond(capacity int) bool {
current := len(cache.entries)
if current >= capacity/2 && current < capacity*2 {
if current >= capacity*3/4 && current < capacity*2 {
return false
}
if int(cache.tail) > current/2 && int(cache.tail) > capacity/2 {
// bad time to resize, this would invalidate too many indices
if capacity < current {
if int(cache.tail) > capacity {
// this would invalidate too many indices
return false
}
}
cache.resize(capacity)
return true
......
......@@ -128,6 +128,33 @@ func TestCacheShrink(t *testing.T) {
}
}
func TestCacheGrowCond(t *testing.T) {
cache := New(16)
if len(cache.entries) != 16 {
t.Errorf("Expected 16, got %v", len(cache.entries))
}
done := cache.ResizeCond(17)
if done || len(cache.entries) != 16 {
t.Errorf("Grew cache by 1")
}
done = cache.ResizeCond(15)
if done || len(cache.entries) != 16 {
t.Errorf("Shrunk cache by 1")
}
done = cache.ResizeCond(32)
if !done || len(cache.entries) != 32 {
t.Errorf("Didn't grow cache")
}
done = cache.ResizeCond(16)
if !done || len(cache.entries) != 16 {
t.Errorf("Didn't shrink cache")
}
}
func TestBitmap(t *testing.T) {
value := uint64(0xcdd58f1e035379c0)
packet := make([]byte, 1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment