https://github.com/python/cpython/commit/4a630980328b67f0aba6a04c3950aa9dbd532895 commit: 4a630980328b67f0aba6a04c3950aa9dbd532895 branch: main author: Amethyst Reese <[email protected]> committer: erlend-aasland <[email protected]> date: 2024-03-01T11:52:53+01:00 summary:
gh-116159: argparse: performance improvement parsing large number of options (#116162) When parsing positional vs optional arguments, the use of min with a list comprehension inside of a loop results in quadratic time based on the number of optional arguments given. When combined with use of prefix based argument files and a large number of optional flags, this can result in extremely slow parsing behavior. This replaces the min call with a simple loop with a short circuit to break at the next optional argument. Co-authored-by: Zsolt Dollenstein <[email protected]> files: M Lib/argparse.py diff --git a/Lib/argparse.py b/Lib/argparse.py index 4200dd5e334ad5..0dbdd67a82f391 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2153,10 +2153,11 @@ def consume_positionals(start_index): while start_index <= max_option_string_index: # consume any Positionals preceding the next option - next_option_string_index = min([ - index - for index in option_string_indices - if index >= start_index]) + next_option_string_index = start_index + while next_option_string_index <= max_option_string_index: + if next_option_string_index in option_string_indices: + break + next_option_string_index += 1 if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index) _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: [email protected]
