mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Avoid array growth in FilteredList.
The method (*schedulerCache).FilteredList builds an array of *v1.Pod that contains every pod in the cluster except for those filtered out by a predicate. Today, it starts with a nil slice and appends to it. Based on current usage, FilteredList is expected to return every pod in the cluster or omit some pods from a single node. This change reserves array capacity equal to the total number of pods in the cluster.
This commit is contained in:
parent
8bd5a46016
commit
3909dc1341
@ -104,7 +104,14 @@ func (cache *schedulerCache) List(selector labels.Selector) ([]*v1.Pod, error) {
|
||||
func (cache *schedulerCache) FilteredList(podFilter PodFilter, selector labels.Selector) ([]*v1.Pod, error) {
|
||||
cache.mu.Lock()
|
||||
defer cache.mu.Unlock()
|
||||
var pods []*v1.Pod
|
||||
// podFilter is expected to return true for most or all of the pods. We
|
||||
// can avoid expensive array growth without wasting too much memory by
|
||||
// pre-allocating capacity.
|
||||
maxSize := 0
|
||||
for _, info := range cache.nodes {
|
||||
maxSize += len(info.pods)
|
||||
}
|
||||
pods := make([]*v1.Pod, 0, maxSize)
|
||||
for _, info := range cache.nodes {
|
||||
for _, pod := range info.pods {
|
||||
if podFilter(pod) && selector.Matches(labels.Set(pod.Labels)) {
|
||||
|
Loading…
Reference in New Issue
Block a user