From b0ff1ea425585ec16585ec6a7d2e676ed3c45430 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Tue, 25 Mar 2014 21:59:13 +0100 Subject: [PATCH] performance optimisation in hash merge logic rewrite deepcopy in util.merge_hash and just iterate on an inventory with 500 groups and 800 hosts this brings back the inventory initialisation from 13s to 3s (with hash_behaviour=merge) --- lib/ansible/utils/__init__.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index cbd33057468..9640ec8ed66 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -558,18 +558,19 @@ def merge_hash(a, b): ''' recursively merges hash b into a keys from b take precedence over keys from a ''' - result = copy.deepcopy(a) - - # next, iterate over b keys and values - for k, v in b.iteritems(): - # if there's already such key in a - # and that key contains dict - if k in result and isinstance(result[k], dict): - # merge those dicts recursively - result[k] = merge_hash(a[k], v) - else: - # otherwise, just copy a value from b to a - result[k] = v + result = {} + + for dicts in a, b: + # next, iterate over b keys and values + for k, v in dicts.iteritems(): + # if there's already such key in a + # and that key contains dict + if k in result and isinstance(result[k], dict): + # merge those dicts recursively + result[k] = merge_hash(a[k], v) + else: + # otherwise, just copy a value from b to a + result[k] = v return result