Optimize Everything and Nothing label selectors

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.
This commit is contained in:
John Howard 2022-09-21 11:54:37 -07:00
parent 299e65cdc5
commit ca414d707a

View File

@ -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{}
@ -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