Optimize Index() method to avoid unnecessary copies

This commit is contained in:
wojtekt
2019-01-30 16:39:38 +01:00
parent d654b49c0e
commit 70b7513c7e

View File

@@ -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)
}
} }
} }