Skip to content

Commit 2f7969e

Browse files
alvaroalemantimflannagan
authored andcommitted
🏃 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.
1 parent 13dcd57 commit 2f7969e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

pkg/client/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ type MatchingLabels map[string]string
514514
func (m MatchingLabels) ApplyToList(opts *ListOptions) {
515515
// TODO(directxman12): can we avoid reserializing this over and over?
516516
if opts.LabelSelector == nil {
517-
opts.LabelSelector = labels.NewSelector()
517+
opts.LabelSelector = labels.SelectorFromValidatedSet(map[string]string(m))
518+
return
518519
}
519520
// If there's already a selector, we need to AND the two together.
520521
noValidSel := labels.SelectorFromValidatedSet(map[string]string(m))

0 commit comments

Comments
 (0)