Skip to content

Commit

Permalink
🏃 Make client.MatchingLabels faster
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alvaroaleman committed Oct 9, 2023
1 parent 968daa8 commit cb5be1f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit cb5be1f

Please sign in to comment.