From f2212a5d950990d57d6840369f1befe28870ba07 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 20 Dec 2012 12:01:24 +0100
Subject: [PATCH] Improve network query algorithm
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Similar to commit 572a277. Directly iterate over β€œself.wanted” instead
of iterating through all networks and checking if they're wanted.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cmdlib.py | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index b133cc50f..3b866646e 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -16202,7 +16202,6 @@ class _NetworkQuery(_QueryBase):
     do_instances = query.NETQ_INST in self.requested_data
     do_groups = do_instances or (query.NETQ_GROUP in self.requested_data)
 
-    network_to_groups = None
     network_to_instances = None
 
     # For NETQ_GROUP, we need to map network->[groups]
@@ -16222,21 +16221,22 @@ class _NetworkQuery(_QueryBase):
           group_instances = [instance for instance in all_instances.values()
                              if instance.primary_node in group_nodes]
 
-        for net_uuid in group.networks.keys():
-          if net_uuid in network_to_groups:
-            netparams = group.networks[net_uuid]
-
+        for net_uuid in self.wanted:
+          netparams = group.networks.get(net_uuid, None)
+          if netparams:
             info = (group.name, netparams[constants.NIC_MODE],
                     netparams[constants.NIC_LINK])
 
             network_to_groups[net_uuid].append(info)
 
-            if do_instances:
-              for instance in group_instances:
-                for nic in instance.nics:
-                  if nic.network == self._all_networks[net_uuid].name:
-                    network_to_instances[net_uuid].append(instance.name)
-                    break
+          if do_instances:
+            for instance in group_instances:
+              for nic in instance.nics:
+                if nic.network == self._all_networks[net_uuid].name:
+                  network_to_instances[net_uuid].append(instance.name)
+                  break
+    else:
+      network_to_groups = None
 
     if query.NETQ_STATS in self.requested_data:
       stats = \
-- 
GitLab