Merge pull request #129628 from 249043822/br004

remove duplicate getAttrsFunc calls to reduce temporary memory allocations

Kubernetes-commit: 63cb5837ddf5a9fdc542f6f26e84a35d19caa0ec
This commit is contained in:
Kubernetes Publisher 2025-01-15 23:34:33 -08:00
commit ae92d91104
4 changed files with 6 additions and 20 deletions

View File

@ -29,7 +29,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
@ -288,10 +287,6 @@ func TestCacheWatcherStoppedOnDestroy(t *testing.T) {
}
func TestResourceVersionAfterInitEvents(t *testing.T) {
getAttrsFunc := func(obj runtime.Object) (labels.Set, fields.Set, error) {
return nil, nil, nil
}
const numObjects = 10
store := cache.NewIndexer(storeElementKey, storeElementIndexers(nil))
@ -300,7 +295,7 @@ func TestResourceVersionAfterInitEvents(t *testing.T) {
store.Add(elem)
}
wci, err := newCacheIntervalFromStore(numObjects, store, getAttrsFunc, "", false)
wci, err := newCacheIntervalFromStore(numObjects, store, "", false)
if err != nil {
t.Fatal(err)
}

View File

@ -752,7 +752,7 @@ func (w *watchCache) getAllEventsSinceLocked(resourceVersion uint64, key string,
// that covers the entire storage state.
// This function assumes to be called under the watchCache lock.
func (w *watchCache) getIntervalFromStoreLocked(key string, matchesSingle bool) (*watchCacheInterval, error) {
ci, err := newCacheIntervalFromStore(w.resourceVersion, w.store, w.getAttrsFunc, key, matchesSingle)
ci, err := newCacheIntervalFromStore(w.resourceVersion, w.store, key, matchesSingle)
if err != nil {
return nil, err
}

View File

@ -21,9 +21,6 @@ import (
"sort"
"sync"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
)
@ -106,7 +103,6 @@ type watchCacheInterval struct {
initialEventsEndBookmark *watchCacheEvent
}
type attrFunc func(runtime.Object) (labels.Set, fields.Set, error)
type indexerFunc func(int) *watchCacheEvent
type indexValidator func(int) bool
@ -140,10 +136,9 @@ func (s sortableWatchCacheEvents) Swap(i, j int) {
// returned by Next() need to be events from a List() done on the underlying store of
// the watch cache.
// The items returned in the interval will be sorted by Key.
func newCacheIntervalFromStore(resourceVersion uint64, store storeIndexer, getAttrsFunc attrFunc, key string, matchesSingle bool) (*watchCacheInterval, error) {
func newCacheIntervalFromStore(resourceVersion uint64, store storeIndexer, key string, matchesSingle bool) (*watchCacheInterval, error) {
buffer := &watchCacheIntervalBuffer{}
var allItems []interface{}
if matchesSingle {
item, exists, err := store.GetByKey(key)
if err != nil {
@ -162,15 +157,11 @@ func newCacheIntervalFromStore(resourceVersion uint64, store storeIndexer, getAt
if !ok {
return nil, fmt.Errorf("not a storeElement: %v", elem)
}
objLabels, objFields, err := getAttrsFunc(elem.Object)
if err != nil {
return nil, err
}
buffer.buffer[i] = &watchCacheEvent{
Type: watch.Added,
Object: elem.Object,
ObjLabels: objLabels,
ObjFields: objFields,
ObjLabels: elem.Labels,
ObjFields: elem.Fields,
Key: elem.Key,
ResourceVersion: resourceVersion,
}

View File

@ -392,7 +392,7 @@ func TestCacheIntervalNextFromStore(t *testing.T) {
store.Add(elem)
}
wci, err := newCacheIntervalFromStore(rv, store, getAttrsFunc, "", false)
wci, err := newCacheIntervalFromStore(rv, store, "", false)
if err != nil {
t.Fatal(err)
}