From 5a57139d91c8bf02c420e8528f8544b9c2c30a87 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 18 Feb 2016 15:59:57 +0100 Subject: [PATCH] Improve efficiency of merge_hash This is related to #14559, but only the part for Ansible v2.0 This commit makes merging empty dicts, or equal dicts more efficient. I noticed that while debugging merge_hash a lot of merges related to empty dictionaries and sometimes also identical dictionaries. --- lib/ansible/utils/vars.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py index 4d44a068c2..73ba52b4b3 100644 --- a/lib/ansible/utils/vars.py +++ b/lib/ansible/utils/vars.py @@ -74,6 +74,12 @@ def merge_hash(a, b): """ _validate_mutable_mappings(a, b) + + # if a is empty or equal to b, return b + if a == {} or a == b: + return b.copy() + + # if b is empty the below unfolds quickly result = a.copy() # next, iterate over b keys and values