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)
This commit is contained in:
parent
d4634983f0
commit
b0ff1ea425
1 changed files with 12 additions and 11 deletions
|
@ -558,18 +558,19 @@ def merge_hash(a, b):
|
||||||
''' recursively merges hash b into a
|
''' recursively merges hash b into a
|
||||||
keys from b take precedence over keys from a '''
|
keys from b take precedence over keys from a '''
|
||||||
|
|
||||||
result = copy.deepcopy(a)
|
result = {}
|
||||||
|
|
||||||
# next, iterate over b keys and values
|
for dicts in a, b:
|
||||||
for k, v in b.iteritems():
|
# next, iterate over b keys and values
|
||||||
# if there's already such key in a
|
for k, v in dicts.iteritems():
|
||||||
# and that key contains dict
|
# if there's already such key in a
|
||||||
if k in result and isinstance(result[k], dict):
|
# and that key contains dict
|
||||||
# merge those dicts recursively
|
if k in result and isinstance(result[k], dict):
|
||||||
result[k] = merge_hash(a[k], v)
|
# merge those dicts recursively
|
||||||
else:
|
result[k] = merge_hash(a[k], v)
|
||||||
# otherwise, just copy a value from b to a
|
else:
|
||||||
result[k] = v
|
# otherwise, just copy a value from b to a
|
||||||
|
result[k] = v
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue