From f42df48a1b7c7c65c92815ddeac46359c8e6d1ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= <tseeker@nocternity.net>
Date: Sun, 18 Sep 2022 12:12:23 +0200
Subject: [PATCH] Don't reset the cache on pop if no deletion occurs

When the stack is being pop'd, it is not necessary to reset it if no
variable is either reset to its previous value or deleted.
---
 inventory_plugins/reconstructed.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/inventory_plugins/reconstructed.py b/inventory_plugins/reconstructed.py
index 4b5edeb..ee82e47 100644
--- a/inventory_plugins/reconstructed.py
+++ b/inventory_plugins/reconstructed.py
@@ -161,16 +161,18 @@ class VariableStorage(MutableMapping):
         restored. The cache will be reset.
         """
         restore = self._script_stack.pop()
-        if not restore:
-            return
+        unchanged = 0
         for vn, vv in restore.items():
             existed, value = vv
             if existed:
                 self._script_vars[vn] = value
             elif vn in self._script_vars:
                 del self._script_vars[vn]
-        self._cache = self._host_vars.copy()
-        self._cache.update(self._script_vars)
+            else:
+                unchanged += 1
+        if unchanged != len(restore):
+            self._cache = self._host_vars.copy()
+            self._cache.update(self._script_vars)
 
     def _set_host_var(self, name, value):
         """Set a host variable.