Mercurial > public > mercurial-scm > hg
comparison mercurial/dicthelpers.py @ 18847:40c679748fa9
dicthelpers: inline diff and join code
mpm suggested this change since it improves performance slightly.
Benchmarking hg perfcalculate -r .
Before:
! wall 0.141173 comb 0.140000 user 0.140000 sys 0.000000 (best of 66)
After:
! wall 0.138619 comb 0.140000 user 0.140000 sys 0.000000 (best of 69)
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Fri, 29 Mar 2013 16:07:57 -0700 |
parents | 860d36b763ae |
children | ed46c2b98b0d |
comparison
equal
deleted
inserted
replaced
18846:860d36b763ae | 18847:40c679748fa9 |
---|---|
3 # Copyright 2013 Facebook | 3 # Copyright 2013 Facebook |
4 # | 4 # |
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 def _diffjoin(d1, d2, default, compare): | 8 def diff(d1, d2, default=None): |
9 '''Return all key-value pairs that are different between d1 and d2. | |
10 | |
11 This includes keys that are present in one dict but not the other, and | |
12 keys whose values are different. The return value is a dict with values | |
13 being pairs of values from d1 and d2 respectively, and missing values | |
14 represented as default.''' | |
9 res = {} | 15 res = {} |
10 if d1 is d2 and compare: | 16 if d1 is d2: |
11 # same dict, so diff is empty | 17 # same dict, so diff is empty |
12 return res | 18 return res |
13 | 19 |
14 for k1, v1 in d1.iteritems(): | 20 for k1, v1 in d1.iteritems(): |
15 if k1 in d2: | 21 if k1 in d2: |
16 v2 = d2[k1] | 22 v2 = d2[k1] |
17 if not compare or v1 != v2: | 23 if v1 != v2: |
18 res[k1] = (v1, v2) | 24 res[k1] = (v1, v2) |
25 else: | |
26 res[k1] = (v1, default) | |
27 | |
28 for k2 in d2: | |
29 if k2 not in d1: | |
30 res[k2] = (default, d2[k2]) | |
31 | |
32 return res | |
33 | |
34 def join(d1, d2, default=None): | |
35 '''Return all key-value pairs from both d1 and d2. | |
36 | |
37 This is akin to an outer join in relational algebra. The return value is a | |
38 dict with values being pairs of values from d1 and d2 respectively, and | |
39 missing values represented as default.''' | |
40 res = {} | |
41 | |
42 for k1, v1 in d1.iteritems(): | |
43 if k1 in d2: | |
44 res[k1] = (v1, d2[k1]) | |
19 else: | 45 else: |
20 res[k1] = (v1, default) | 46 res[k1] = (v1, default) |
21 | 47 |
22 if d1 is d2: | 48 if d1 is d2: |
23 return res | 49 return res |
25 for k2 in d2: | 51 for k2 in d2: |
26 if k2 not in d1: | 52 if k2 not in d1: |
27 res[k2] = (default, d2[k2]) | 53 res[k2] = (default, d2[k2]) |
28 | 54 |
29 return res | 55 return res |
30 | |
31 def diff(d1, d2, default=None): | |
32 '''Return all key-value pairs that are different between d1 and d2. | |
33 | |
34 This includes keys that are present in one dict but not the other, and | |
35 keys whose values are different. The return value is a dict with values | |
36 being pairs of values from d1 and d2 respectively, and missing values | |
37 represented as default.''' | |
38 return _diffjoin(d1, d2, default, True) | |
39 | |
40 def join(d1, d2, default=None): | |
41 '''Return all key-value pairs from both d1 and d2. | |
42 | |
43 This is akin to an outer join in relational algebra. The return value is a | |
44 dict with values being pairs of values from d1 and d2 respectively, and | |
45 missing values represented as default.''' | |
46 return _diffjoin(d1, d2, default, False) |