New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argparse.ArgumentParser is slow when parsing a very long option list #96859
Comments
…onals The loop consuming optional and positional arguments in ArgumentParser._parse_known_args() rescans option_string_indices on every iteration of the loop in order to compute the next option index: ```py next_option_string_index = min([ index for index in option_string_indices if index >= start_index]) ``` This becomes very slow when parsing a very long list of arguments, e.g. when parsing a response file. This commit refactors the loop to scan the option indices list only once.
For comparison, here is how alexeypa@548376e behaves with the same repro:
There is still non-linear growth caused by Let me go through a submission checklist before creating a PR for this fix. |
Currently the append, append_const, and extend actions make a copy of the destination list every time the action is called. This is noteceable with very long option lists. This commit adds Action.clone_dest() method that is called the first time an action runs for each destination. This allows to copy lists only once, significantky speeding up appends.
I updated the actions code to copy lists only once on alexeypa@856ca35. This improved the situation significantly:
I think it makes sense to lend this fix on a separate PR, since it addresses a different problem then #96904. |
Hey, trying to bring some attention to #96904. The PR is still waiting for a core review. Is anyone available to take a look? |
Bug report
The following parser exhibits O(N^2) behavior when parsing a very long option list (e.g.
--item=a --item=b ...
):Here is a simple repro that measures the time
parse_args()
take depending on the number of options:The output on my machine:
Your environment
The text was updated successfully, but these errors were encountered: