From b86695770f99a28c847f79f5643af4e36c3c37d8 Mon Sep 17 00:00:00 2001 From: Dingzhu Lurong <1015542478@qq.com> Date: Thu, 2 Sep 2021 14:02:54 +0800 Subject: [PATCH] Improve DeltaFIFO function 'ListKeys' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In function ListKeys, it better to use ‘queue’ instead of 'items' to get all the keys. Kubernetes-commit: f0ce8755287b1cad087b61abfcccd79c9f151828 --- tools/cache/delta_fifo.go | 4 ++-- tools/cache/delta_fifo_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/cache/delta_fifo.go b/tools/cache/delta_fifo.go index 6a77b32f..2da2933a 100644 --- a/tools/cache/delta_fifo.go +++ b/tools/cache/delta_fifo.go @@ -458,8 +458,8 @@ func (f *DeltaFIFO) listLocked() []interface{} { func (f *DeltaFIFO) ListKeys() []string { f.lock.RLock() defer f.lock.RUnlock() - list := make([]string, 0, len(f.items)) - for key := range f.items { + list := make([]string, 0, len(f.queue)) + for _, key := range f.queue { list = append(list, key) } return list diff --git a/tools/cache/delta_fifo_test.go b/tools/cache/delta_fifo_test.go index 92247c4b..f17240da 100644 --- a/tools/cache/delta_fifo_test.go +++ b/tools/cache/delta_fifo_test.go @@ -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() +}