mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #84182 from wojtek-t/microoptimize_selector
Microoptimization in SelectorFrom*Set
This commit is contained in:
commit
b5a2abfda7
@ -850,7 +850,7 @@ func SelectorFromSet(ls Set) Selector {
|
|||||||
if ls == nil || len(ls) == 0 {
|
if ls == nil || len(ls) == 0 {
|
||||||
return internalSelector{}
|
return internalSelector{}
|
||||||
}
|
}
|
||||||
var requirements internalSelector
|
requirements := make([]Requirement, 0, len(ls))
|
||||||
for label, value := range ls {
|
for label, value := range ls {
|
||||||
r, err := NewRequirement(label, selection.Equals, []string{value})
|
r, err := NewRequirement(label, selection.Equals, []string{value})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -862,7 +862,7 @@ func SelectorFromSet(ls Set) Selector {
|
|||||||
}
|
}
|
||||||
// sort to have deterministic string representation
|
// sort to have deterministic string representation
|
||||||
sort.Sort(ByKey(requirements))
|
sort.Sort(ByKey(requirements))
|
||||||
return requirements
|
return internalSelector(requirements)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectorFromValidatedSet returns a Selector which will match exactly the given Set.
|
// SelectorFromValidatedSet returns a Selector which will match exactly the given Set.
|
||||||
@ -872,13 +872,13 @@ func SelectorFromValidatedSet(ls Set) Selector {
|
|||||||
if ls == nil || len(ls) == 0 {
|
if ls == nil || len(ls) == 0 {
|
||||||
return internalSelector{}
|
return internalSelector{}
|
||||||
}
|
}
|
||||||
var requirements internalSelector
|
requirements := make([]Requirement, 0, len(ls))
|
||||||
for label, value := range ls {
|
for label, value := range ls {
|
||||||
requirements = append(requirements, Requirement{key: label, operator: selection.Equals, strValues: []string{value}})
|
requirements = append(requirements, Requirement{key: label, operator: selection.Equals, strValues: []string{value}})
|
||||||
}
|
}
|
||||||
// sort to have deterministic string representation
|
// sort to have deterministic string representation
|
||||||
sort.Sort(ByKey(requirements))
|
sort.Sort(ByKey(requirements))
|
||||||
return requirements
|
return internalSelector(requirements)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseToRequirements takes a string representing a selector and returns a list of
|
// ParseToRequirements takes a string representing a selector and returns a list of
|
||||||
|
@ -617,3 +617,16 @@ func TestSafeSort(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkSelectorFromValidatedSet(b *testing.B) {
|
||||||
|
set := map[string]string{
|
||||||
|
"foo": "foo",
|
||||||
|
"bar": "bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
if SelectorFromValidatedSet(set).Empty() {
|
||||||
|
b.Errorf("Unexpected selector")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user