Improve DeltaFIFO function 'ListKeys'

In function ListKeys, it better to use ‘queue’ instead of 'items' to get all the keys.

Kubernetes-commit: f0ce8755287b1cad087b61abfcccd79c9f151828
This commit is contained in:
Dingzhu Lurong 2021-09-02 14:02:54 +08:00 committed by Kubernetes Publisher
parent 9b97591456
commit b86695770f
2 changed files with 22 additions and 2 deletions

View File

@ -458,8 +458,8 @@ func (f *DeltaFIFO) listLocked() []interface{} {
func (f *DeltaFIFO) ListKeys() []string { func (f *DeltaFIFO) ListKeys() []string {
f.lock.RLock() f.lock.RLock()
defer f.lock.RUnlock() defer f.lock.RUnlock()
list := make([]string, 0, len(f.items)) list := make([]string, 0, len(f.queue))
for key := range f.items { for _, key := range f.queue {
list = append(list, key) list = append(list, key)
} }
return list return list

View File

@ -694,3 +694,23 @@ func TestDeltaFIFO_PopShouldUnblockWhenClosed(t *testing.T) {
} }
} }
} }
func BenchmarkDeltaFIFOListKeys(b *testing.B) {
f := NewDeltaFIFOWithOptions(DeltaFIFOOptions{KeyFunction: testFifoObjectKeyFunc})
const amount = 10000
for i := 0; i < amount; i++ {
f.Add(mkFifoObj(string([]rune{'a', rune(i)}), i+1))
}
for u := uint64(0); u < amount; u++ {
f.Add(mkFifoObj(string([]rune{'b', rune(u)}), u+1))
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = f.ListKeys()
}
})
b.StopTimer()
}