[Notes] [Git][BuildStream/buildstream][danielsilverstone-ct/further-optimisations] _yaml.py: Reduce use of `isinstance()` in `node_sanitize()`



Title: GitLab

Daniel Silverstone pushed to branch danielsilverstone-ct/further-optimisations at BuildStream / buildstream

Commits:

1 changed file:

Changes:

  • buildstream/_yaml.py
    ... ... @@ -914,9 +914,20 @@ 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 isinstance(node, collections.Mapping):
    
    920 931
             result = SanitizedDict()
    
    921 932
     
    
    922 933
             key_list = [key for key, _ in node_items(node)]
    
    ... ... @@ -924,10 +935,10 @@ def node_sanitize(node):
    924 935
                 result[key] = node_sanitize(node[key])
    
    925 936
     
    
    926 937
             return result
    
    927
    -
    
    928 938
         elif isinstance(node, list):
    
    929 939
             return [node_sanitize(elt) for elt in node]
    
    930 940
     
    
    941
    +    # Everything else (such as commented scalars) just gets returned as-is.
    
    931 942
         return node
    
    932 943
     
    
    933 944
     
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]