Skip to content

Commit

Permalink
Improve slice allocation in LabelSelectorAsSelector
Browse files Browse the repository at this point in the history
Kubernetes-commit: 82e4ab5ec629bbae431b36f794b90222f9cb00a8
  • Loading branch information
alculquicondor authored and k8s-publishing-bot committed May 20, 2021
1 parent 0376ffb commit 51eb781
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
10 changes: 6 additions & 4 deletions pkg/apis/meta/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) {
if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 {
return labels.Everything(), nil
}
selector := labels.NewSelector()
requirements := make([]labels.Requirement, 0, len(ps.MatchLabels)+len(ps.MatchExpressions))
for k, v := range ps.MatchLabels {
r, err := labels.NewRequirement(k, selection.Equals, []string{v})
if err != nil {
return nil, err
}
selector = selector.Add(*r)
requirements = append(requirements, *r)
}
for _, expr := range ps.MatchExpressions {
var op selection.Operator
Expand All @@ -64,8 +64,10 @@ func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) {
if err != nil {
return nil, err
}
selector = selector.Add(*r)
requirements = append(requirements, *r)
}
selector := labels.NewSelector()
selector = selector.Add(requirements...)
return selector, nil
}

Expand Down Expand Up @@ -154,7 +156,7 @@ func SetAsLabelSelector(ls labels.Set) *LabelSelector {
}

selector := &LabelSelector{
MatchLabels: make(map[string]string),
MatchLabels: make(map[string]string, len(ls)),
}
for label, value := range ls {
selector.MatchLabels[label] = value
Expand Down
10 changes: 3 additions & 7 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,9 @@ func safeSort(in []string) []string {

// Add adds requirements to the selector. It copies the current selector returning a new one
func (s internalSelector) Add(reqs ...Requirement) Selector {
var ret internalSelector
for ix := range s {
ret = append(ret, s[ix])
}
for _, r := range reqs {
ret = append(ret, r)
}
ret := make(internalSelector, 0, len(s)+len(reqs))
ret = append(ret, s...)
ret = append(ret, reqs...)
sort.Sort(ByKey(ret))
return ret
}
Expand Down

0 comments on commit 51eb781

Please sign in to comment.