rewriter: optimize the list_to_dict function

This uses an iterator instead of walking a list, which is roughly twice
as fast. This also does away with the pre-check on whether the list is
valid for converting to a dict, and instead handles the case of an
uneven number by catching another exception. This is preferable since
it's a fatal error anyway, so avoiding it in the non-fatal case is
preferable.
pull/5227/merge
Dylan Baker 6 years ago
parent 346ab9f0d8
commit 8e1670cc60
  1. 13
      mesonbuild/rewriter.py

@ -874,11 +874,16 @@ target_operation_map = {
}
def list_to_dict(in_list: List[str]) -> Dict[str, str]:
if len(in_list) % 2 != 0:
raise TypeError('An even ammount of arguments are required')
result = {}
for i in range(0, len(in_list), 2):
result[in_list[i]] = in_list[i + 1]
it = iter(in_list)
try:
for i in it:
# calling next(it) is not a mistake, we're taking the next element from
# the iterator, avoiding te need to preprocess it into a sequence of
# key value pairs.
result[i] = next(it)
except StopIteration:
raise TypeError('in_list parameter of list_to_dict must have an even length.')
return result
def generate_target(options) -> List[dict]:

Loading…
Cancel
Save