1
Fork 0

Be more aggressive about growing the packet cache.

Also add a test.
This commit is contained in:
Juliusz Chroboczek 2020-06-09 15:47:33 +02:00
parent f3b58bbf5c
commit 94c42ea784
2 changed files with 33 additions and 4 deletions

View File

@ -204,13 +204,15 @@ 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
return false
if capacity < current {
if int(cache.tail) > capacity {
// this would invalidate too many indices
return false
}
}
cache.resize(capacity)

View File

@ -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)