From 422121f93a75e34479337f4711956c97e619a725 Mon Sep 17 00:00:00 2001 From: Wojciech Tyczynski Date: Mon, 17 Oct 2016 15:01:00 +0200 Subject: [PATCH 1/2] Avoid unnecessary map allocation --- pkg/registry/core/pod/strategy.go | 3 +-- pkg/registry/generic/matcher.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index 39e7ac84bcf..4350ad8cf6f 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -192,13 +192,12 @@ func NodeNameTriggerFunc(obj runtime.Object) []storage.MatchValue { // PodToSelectableFields returns a field set that represents the object // TODO: fields are not labels, and the validation rules for them do not apply. func PodToSelectableFields(pod *api.Pod) fields.Set { - objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&pod.ObjectMeta, true) podSpecificFieldsSet := fields.Set{ "spec.nodeName": pod.Spec.NodeName, "spec.restartPolicy": string(pod.Spec.RestartPolicy), "status.phase": string(pod.Status.Phase), } - return generic.MergeFieldsSets(objectMetaFieldsSet, podSpecificFieldsSet) + return generic.AddObjectMetaFieldsSet(podSpecificFieldsSet, &pod.ObjectMeta, true) } // ResourceGetter is an interface for retrieving resources by ResourceLocation. diff --git a/pkg/registry/generic/matcher.go b/pkg/registry/generic/matcher.go index 4ea2c688483..9654156034c 100644 --- a/pkg/registry/generic/matcher.go +++ b/pkg/registry/generic/matcher.go @@ -21,7 +21,7 @@ import ( "k8s.io/kubernetes/pkg/fields" ) -// ObjectMetaFieldsSet returns a fields that represents the ObjectMeta. +// ObjectMetaFieldsSet returns a fields that represent the ObjectMeta. func ObjectMetaFieldsSet(objectMeta *api.ObjectMeta, hasNamespaceField bool) fields.Set { if !hasNamespaceField { return fields.Set{ @@ -34,6 +34,15 @@ func ObjectMetaFieldsSet(objectMeta *api.ObjectMeta, hasNamespaceField bool) fie } } +// AdObjectMetaField add fields that represent the ObjectMeta to source. +func AddObjectMetaFieldsSet(source fields.Set, objectMeta *api.ObjectMeta, hasNamespaceField bool) fields.Set { + source["metadata.name"] = objectMeta.Name + if hasNamespaceField { + source["metadata.namespace"] = objectMeta.Namespace + } + return source +} + // MergeFieldsSets merges a fields'set from fragment into the source. func MergeFieldsSets(source fields.Set, fragment fields.Set) fields.Set { for k, value := range fragment { From c6b098068d184fa18d2ea0b51d2b0057daf81bb4 Mon Sep 17 00:00:00 2001 From: Wojciech Tyczynski Date: Mon, 17 Oct 2016 15:07:15 +0200 Subject: [PATCH 2/2] Allocate podFields map with a correct hint for size --- pkg/registry/core/pod/strategy.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index 4350ad8cf6f..0f6376fb2d1 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -192,11 +192,14 @@ func NodeNameTriggerFunc(obj runtime.Object) []storage.MatchValue { // PodToSelectableFields returns a field set that represents the object // TODO: fields are not labels, and the validation rules for them do not apply. func PodToSelectableFields(pod *api.Pod) fields.Set { - podSpecificFieldsSet := fields.Set{ - "spec.nodeName": pod.Spec.NodeName, - "spec.restartPolicy": string(pod.Spec.RestartPolicy), - "status.phase": string(pod.Status.Phase), - } + // The purpose of allocation with a given number of elements is to reduce + // amount of allocations needed to create the fields.Set. If you add any + // field here or the number of object-meta related fields changes, this should + // be adjusted. + podSpecificFieldsSet := make(fields.Set, 5) + podSpecificFieldsSet["spec.nodeName"] = pod.Spec.NodeName + podSpecificFieldsSet["spec.restartPolicy"] = string(pod.Spec.RestartPolicy) + podSpecificFieldsSet["status.phase"] = string(pod.Status.Phase) return generic.AddObjectMetaFieldsSet(podSpecificFieldsSet, &pod.ObjectMeta, true) }