Don't deepcopy the config if the old value is not needed
authorKlaus Aehlig <aehlig@google.com>
Mon, 14 Dec 2015 14:08:22 +0000 (15:08 +0100)
committerKlaus Aehlig <aehlig@google.com>
Wed, 16 Dec 2015 17:00:07 +0000 (18:00 +0100)
The _UpgradeConfig function carries out internal upgrades of the
configuration, and additionally, if requested, saves the configuration
in case it changed in this process. To compare the old and the new
version, a deep copy of the old version is kept. As deep copying large
configurations is an expensive operation, only do it, if the value is
used afterwards.

Signed-off-by: Klaus Aehlig <aehlig@google.com>
Reviewed-by: Lisa Velden <velden@google.com>

lib/config/__init__.py

index 0e9c0c8..dfe2625 100644 (file)
@@ -2914,7 +2914,10 @@ class ConfigWriter(object):
     """
     # Keep a copy of the persistent part of _config_data to check for changes
     # Serialization doesn't guarantee order in dictionaries
-    oldconf = copy.deepcopy(self._ConfigData().ToDict())
+    if saveafter:
+      oldconf = copy.deepcopy(self._ConfigData().ToDict())
+    else:
+      oldconf = None
 
     # In-object upgrades
     self._ConfigData().UpgradeConfig()
@@ -2936,7 +2939,10 @@ class ConfigWriter(object):
       # serializing/deserializing the object.
       self._UnlockedAddNodeToGroup(node.uuid, node.group)
 
-    modified = (oldconf != self._ConfigData().ToDict())
+    if saveafter:
+      modified = (oldconf != self._ConfigData().ToDict())
+    else:
+      modified = True # can't prove it didn't change, but doesn't matter
     if modified and saveafter:
       self._WriteConfig()
       self._UnlockedDropECReservations(_UPGRADE_CONFIG_JID)