Optimize kubernetes.stringSliceOverlaps
As the TODO comment indicates, this function is O(n^2).
Uses a map to make it O(n).
@@ -79,15 +79,18 @@ func NewK8sDatastore(thisClusterID string, stopCh <-chan struct{}) *k8s {
return &kubeDatastore
}
-// todo: need to fix this O(n^2) solution...
func stringSliceOverlaps(left []string, right []string) bool {
+ hash := make(map[string]bool)
for _, s := range left {
- for _, t := range right {
- if s == t {
- return true
+ hash[s] = true
}
+
+ for _, s := range right {
+ if hash[s] {
+ return true
}
}
+
return false
}
