Commit 3909dc13 authored by Jonathan Basseri's avatar Jonathan Basseri

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.
parent 8bd5a460
...@@ -104,7 +104,14 @@ func (cache *schedulerCache) List(selector labels.Selector) ([]*v1.Pod, error) { ...@@ -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) { func (cache *schedulerCache) FilteredList(podFilter PodFilter, selector labels.Selector) ([]*v1.Pod, error) {
cache.mu.Lock() cache.mu.Lock()
defer cache.mu.Unlock() 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 _, info := range cache.nodes {
for _, pod := range info.pods { for _, pod := range info.pods {
if podFilter(pod) && selector.Matches(labels.Set(pod.Labels)) { if podFilter(pod) && selector.Matches(labels.Set(pod.Labels)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment