From cb5be1f29629486d1bb710cf2edf5b06c994d6f3 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Sun, 8 Oct 2023 20:21:17 -0400 Subject: [PATCH] :running: Make client.MatchingLabels faster The 99% use-case of this is to set a selector, not to adjust an existing one. This change introduces a fastpath that does that with half the allocations and in a bit less than half the time. The reason slowpath is slow is that for each label a requirement has to be constructed that is then appended to a slice, both of which cause allocations. --- pkg/client/options.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/client/options.go b/pkg/client/options.go index d81bf25de9..ee6489b7c7 100644 --- a/pkg/client/options.go +++ b/pkg/client/options.go @@ -514,7 +514,8 @@ type MatchingLabels map[string]string func (m MatchingLabels) ApplyToList(opts *ListOptions) { // TODO(directxman12): can we avoid reserializing this over and over? if opts.LabelSelector == nil { - opts.LabelSelector = labels.NewSelector() + opts.LabelSelector = labels.SelectorFromValidatedSet(map[string]string(m)) + return } // If there's already a selector, we need to AND the two together. noValidSel := labels.SelectorFromValidatedSet(map[string]string(m))