Unverified Commit 94b5aeba authored by Kubernetes Prow Robot's avatar Kubernetes Prow Robot Committed by GitHub

Merge pull request #73529 from wojtek-t/optimize_index

Optimize Index() method to avoid unnecessary copies
parents f86218f9 70b7513c
...@@ -148,12 +148,19 @@ func (c *threadSafeMap) Index(indexName string, obj interface{}) ([]interface{}, ...@@ -148,12 +148,19 @@ func (c *threadSafeMap) Index(indexName string, obj interface{}) ([]interface{},
} }
index := c.indices[indexName] index := c.indices[indexName]
// need to de-dupe the return list. Since multiple keys are allowed, this can happen. var returnKeySet sets.String
returnKeySet := sets.String{} if len(indexKeys) == 1 {
for _, indexKey := range indexKeys { // In majority of cases, there is exactly one value matching.
set := index[indexKey] // Optimize the most common path - deduping is not needed here.
for _, key := range set.UnsortedList() { returnKeySet = index[indexKeys[0]]
returnKeySet.Insert(key) } else {
// Need to de-dupe the return list.
// Since multiple keys are allowed, this can happen.
returnKeySet = sets.String{}
for _, indexKey := range indexKeys {
for key := range index[indexKey] {
returnKeySet.Insert(key)
}
} }
} }
......
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