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() +}