Skip to content

Commit

Permalink
Optimize Everything and Nothing label selectors
Browse files Browse the repository at this point in the history
Currently each call makes 1 small alloc. This is not much, but it adds
up when this is called millions of times. This simple change just moves
to using a shared struct between everything.

This is safe as each operation on the Selector is not doing mutations.

Kubernetes-commit: ca414d707a6b10c41b65ed9bee127773f2bcf0fc
  • Loading branch information
howardjohn authored and k8s-publishing-bot committed Sep 21, 2022
1 parent b03a432 commit 3adc870
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ type Selector interface {
RequiresExactMatch(label string) (value string, found bool)
}

var everythingSelector Selector = internalSelector{}

// Everything returns a selector that matches all labels.
func Everything() Selector {
return internalSelector{}
return everythingSelector
}

type nothingSelector struct{}
Expand All @@ -91,9 +93,11 @@ func (n nothingSelector) RequiresExactMatch(label string) (value string, found b
return "", false
}

var internalNothingSelector Selector = nothingSelector{}

// Nothing returns a selector that matches no labels
func Nothing() Selector {
return nothingSelector{}
return internalNothingSelector
}

// NewSelector returns a nil selector
Expand Down

0 comments on commit 3adc870

Please sign in to comment.