Daniel Silverstone pushed to branch danielsilverstone-ct/further-optimisations at BuildStream / buildstream
Commits:
-
73c18504
by Daniel Silverstone at 2018-11-06T13:41:09Z
1 changed file:
Changes:
| ... | ... | @@ -914,9 +914,21 @@ RoundTripRepresenter.add_representer(SanitizedDict, |
| 914 | 914 |
# Only dicts are ordered, list elements are left in order.
|
| 915 | 915 |
#
|
| 916 | 916 |
def node_sanitize(node):
|
| 917 |
+ # Short-circuit None which occurs ca. twice per element
|
|
| 918 |
+ if node is None:
|
|
| 919 |
+ return node
|
|
| 920 |
+ |
|
| 921 |
+ node_type = type(node)
|
|
| 922 |
+ # Next short-circuit integers, floats, strings, booleans, and tuples
|
|
| 923 |
+ if node_type in (int, float, str, bool, tuple):
|
|
| 924 |
+ return node
|
|
| 925 |
+ # Now short-circuit lists
|
|
| 926 |
+ elif node_type is list:
|
|
| 927 |
+ return [node_sanitize(elt) for elt in node]
|
|
| 917 | 928 |
|
| 918 |
- if isinstance(node, collections.Mapping):
|
|
| 919 |
- |
|
| 929 |
+ # Finally ChainMap and dict, and other Mappings need special handling
|
|
| 930 |
+ if (node_type in (dict, ChainMap) or
|
|
| 931 |
+ isinstance(node, collections.Mapping)):
|
|
| 920 | 932 |
result = SanitizedDict()
|
| 921 | 933 |
|
| 922 | 934 |
key_list = [key for key, _ in node_items(node)]
|
| ... | ... | @@ -924,10 +936,10 @@ def node_sanitize(node): |
| 924 | 936 |
result[key] = node_sanitize(node[key])
|
| 925 | 937 |
|
| 926 | 938 |
return result
|
| 927 |
- |
|
| 928 | 939 |
elif isinstance(node, list):
|
| 929 | 940 |
return [node_sanitize(elt) for elt in node]
|
| 930 | 941 |
|
| 942 |
+ # Everything else (such as commented scalars) just gets returned as-is.
|
|
| 931 | 943 |
return node
|
| 932 | 944 |
|
| 933 | 945 |
|
