Merge pull request #129628 from 249043822/br004

remove duplicate getAttrsFunc calls to reduce temporary memory allocations
This commit is contained in:
Kubernetes Prow Robot 2025-01-15 23:34:33 -08:00 committed by GitHub
commit 63cb5837dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 6 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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