mirror of
https://github.com/jech/galene.git
synced 2024-11-25 01:55:57 +01:00
Be more aggressive about growing the packet cache.
Also add a test.
This commit is contained in:
parent
f3b58bbf5c
commit
94c42ea784
2 changed files with 33 additions and 4 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue