Improve performance of method haveOverlap

Signed-off-by: mantuliu <240951888@qq.com>
This commit is contained in:
mantuliu 2023-02-21 16:50:10 +08:00
parent 487c443239
commit e1e07e34e3
2 changed files with 24 additions and 5 deletions

View File

@ -144,11 +144,7 @@ func haveOverlap(a1, a2 []string) bool {
if len(a1) > len(a2) {
a1, a2 = a2, a1
}
m := make(sets.String)
for _, val := range a1 {
m.Insert(val)
}
m := sets.New[string](a1...)
for _, val := range a2 {
if _, ok := m[val]; ok {
return true

View File

@ -359,6 +359,29 @@ func TestAccessModeConflicts(t *testing.T) {
}
}
func BenchmarkHaveOverlap(b *testing.B) {
tests := []struct {
a1 []string
a2 []string
isOverlap bool
}{
{
a1: []string{"ab", "ac", "abc", "abcd", "abcde", "abcd", "ab", "abcdef", "abcdefg", "1", "2", "3", "4", "5", "6"},
a2: []string{"bc", "helloworld", "bcd", "bcdef", "bcde", "12", "23", "34", "45", "56", "67", "78", "89"},
isOverlap: false,
},
}
testError := false
for n := 0; n < b.N; n++ {
for _, test := range tests {
if !testError && haveOverlap(test.a1, test.a2) != test.isOverlap {
testError = true
b.Errorf("test haveOverlap wrong, test case is: %v", test)
}
}
}
}
func newPlugin(ctx context.Context, t *testing.T) framework.Plugin {
return newPluginWithListers(ctx, t, nil, nil, nil, true)
}