From 0ced3f43bf56b9005ef4d7eb1a6ffba73bce28c5 Mon Sep 17 00:00:00 2001 From: Wojciech Tyczynski Date: Wed, 19 Oct 2016 16:45:38 +0200 Subject: [PATCH] Avoid unnecessary reallocations of slice in Cacher --- pkg/storage/cacher.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/storage/cacher.go b/pkg/storage/cacher.go index 41fd2ee447b..90f7a734c7b 100644 --- a/pkg/storage/cacher.go +++ b/pkg/storage/cacher.go @@ -389,6 +389,12 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p return fmt.Errorf("failed to wait for fresh list: %v", err) } trace.Step(fmt.Sprintf("Listed %d items from cache", len(objs))) + if len(objs) > listVal.Cap() && pred.Label.Empty() && pred.Field.Empty() { + // Resize the slice appropriately, since we already know that none + // of the elements will be filtered out. + listVal.Set(reflect.MakeSlice(reflect.SliceOf(c.objectType.Elem()), 0, len(objs))) + trace.Step("Resized result") + } for _, obj := range objs { object, ok := obj.(runtime.Object) if !ok {