gh-116159: argparse: performance improvement parsing large number of options#116162
Merged
Conversation
…er of options 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. Example test case that exercises this performance behavior: https://gist.github.com/amyreese/b825bc092210ea7b64287f033dc995d8 The test case parses a list of arguments that contains 30,000 copies of `--flag=something`. Using latest 3.12 or 3.13 from main, this takes roughly 15-16 seconds to parse. With the proposed fix, this only takes 80 milliseconds. Before: ``` $ ~/opt/cpython/bin/python3.13 -VV Python 3.13.0a4+ (heads/main:0656509033, Feb 29 2024, 11:59:43) [Clang 15.0.0 (clang-1500.1.0.2.5)] $ ~/opt/cpython/bin/python3.13 test_many_flags.py parsed args in 15,195,914 µs ``` With fix: ``` $ ~/opt/cpython/bin/python3.13 -VV Python 3.13.0a4+ (heads/argparse-options:968aa13a1d, Feb 29 2024, 13:34:49) [Clang 15.0.0 (clang-1500.1.0.2.5)] $ ~/opt/cpython/bin/python3.13 test_many_flags.py parsed args in 79,787 µs ``` Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
erlend-aasland
approved these changes
Feb 29, 2024
woodruffw
pushed a commit
to woodruffw-forks/cpython
that referenced
this pull request
Mar 4, 2024
…er of options (python#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 <zsol.zsol@gmail.com>
facebook-github-bot
pushed a commit
to facebookincubator/cinder
that referenced
this pull request
Mar 6, 2024
Summary: Upstream PR: python/cpython#116162 This improves performance when parsing a large number of options from multiple seconds to milliseconds. pikachu_dance Reviewed By: fried Differential Revision: D54553600 fbshipit-source-id: da1ca6d110e701bbd491465c00627f29a08af3c5
adorilson
pushed a commit
to adorilson/cpython
that referenced
this pull request
Mar 25, 2024
…er of options (python#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 <zsol.zsol@gmail.com>
diegorusso
pushed a commit
to diegorusso/cpython
that referenced
this pull request
Apr 17, 2024
…er of options (python#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 <zsol.zsol@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Example test case that exercises this performance behavior:
https://gist.github.com/amyreese/b825bc092210ea7b64287f033dc995d8
The test case parses a list of arguments that contains 30,000 copies
of
--flag=something. Using latest 3.12 or 3.13 from main, this takesroughly 15-16 seconds to parse. With the proposed fix, this only takes
80 milliseconds.
Before:
With fix:
Co-authored-by: Zsolt Dollenstein zsol.zsol@gmail.com