mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 06:01:50 +00:00
Merge pull request #34948 from wojtek-t/avoid_unnecessary_allocation
Automatic merge from submit-queue Avoid unnecessary allocation This is supposed to avoid unnecessary memory allocations. PodToSelectableFields seems to be the biggest contributor to memory allocations: ``` Showing top 10 nodes out of 247 (cum >= 83166442) flat flat% sum% cum cum% 1796823715 31.09% 31.09% 1796823715 31.09% k8s.io/kubernetes/pkg/registry/core/pod.PodToSelectableFields 530856268 9.19% 40.28% 530856268 9.19% k8s.io/kubernetes/pkg/storage.NamespaceKeyFunc 241505351 4.18% 44.46% 241505351 4.18% reflect.unsafe_New ... ```
This commit is contained in:
@@ -192,13 +192,15 @@ func NodeNameTriggerFunc(obj runtime.Object) []storage.MatchValue {
|
|||||||
// PodToSelectableFields returns a field set that represents the object
|
// PodToSelectableFields returns a field set that represents the object
|
||||||
// TODO: fields are not labels, and the validation rules for them do not apply.
|
// TODO: fields are not labels, and the validation rules for them do not apply.
|
||||||
func PodToSelectableFields(pod *api.Pod) fields.Set {
|
func PodToSelectableFields(pod *api.Pod) fields.Set {
|
||||||
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&pod.ObjectMeta, true)
|
// The purpose of allocation with a given number of elements is to reduce
|
||||||
podSpecificFieldsSet := fields.Set{
|
// amount of allocations needed to create the fields.Set. If you add any
|
||||||
"spec.nodeName": pod.Spec.NodeName,
|
// field here or the number of object-meta related fields changes, this should
|
||||||
"spec.restartPolicy": string(pod.Spec.RestartPolicy),
|
// be adjusted.
|
||||||
"status.phase": string(pod.Status.Phase),
|
podSpecificFieldsSet := make(fields.Set, 5)
|
||||||
}
|
podSpecificFieldsSet["spec.nodeName"] = pod.Spec.NodeName
|
||||||
return generic.MergeFieldsSets(objectMetaFieldsSet, podSpecificFieldsSet)
|
podSpecificFieldsSet["spec.restartPolicy"] = string(pod.Spec.RestartPolicy)
|
||||||
|
podSpecificFieldsSet["status.phase"] = string(pod.Status.Phase)
|
||||||
|
return generic.AddObjectMetaFieldsSet(podSpecificFieldsSet, &pod.ObjectMeta, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResourceGetter is an interface for retrieving resources by ResourceLocation.
|
// ResourceGetter is an interface for retrieving resources by ResourceLocation.
|
||||||
|
@@ -21,7 +21,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/fields"
|
"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 {
|
func ObjectMetaFieldsSet(objectMeta *api.ObjectMeta, hasNamespaceField bool) fields.Set {
|
||||||
if !hasNamespaceField {
|
if !hasNamespaceField {
|
||||||
return fields.Set{
|
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.
|
// MergeFieldsSets merges a fields'set from fragment into the source.
|
||||||
func MergeFieldsSets(source fields.Set, fragment fields.Set) fields.Set {
|
func MergeFieldsSets(source fields.Set, fragment fields.Set) fields.Set {
|
||||||
for k, value := range fragment {
|
for k, value := range fragment {
|
||||||
|
Reference in New Issue
Block a user