From ca414d707a6b10c41b65ed9bee127773f2bcf0fc Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 21 Sep 2022 11:54:37 -0700 Subject: [PATCH] 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. --- staging/src/k8s.io/apimachinery/pkg/labels/selector.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/labels/selector.go b/staging/src/k8s.io/apimachinery/pkg/labels/selector.go index 6d6f562ad13..06b0aa53738 100644 --- a/staging/src/k8s.io/apimachinery/pkg/labels/selector.go +++ b/staging/src/k8s.io/apimachinery/pkg/labels/selector.go @@ -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