Repository navigation
Conversation
Each positional argument was removed from the front of the remaining argument list with 'del rargs[0]', so parsing N positional arguments took O(N**2) time. Move each run of consecutive positional arguments to 'largs' and delete it from 'rargs' in one step. Options and callbacks still see the same 'parser.largs' and 'parser.rargs'.
Contributor
Author
|
The ASan failure is |
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.
OptionParser._process_args()removed every positional argument from the front ofrargswithdel rargs[0], so parsing N positional arguments took O(N²) time. With 200,000 positional arguments,parse_args()goes from 2.47 s to 8 ms.The fix appends each run of consecutive positional arguments to
largsand deletes the run fromrargsin one step. A run stops at the next argument that is handled as an option (arg[:1] == "-" and len(arg) > 1, which includes--), so option processing,--and callbacks are unchanged, and callbacks still see the sameparser.largsandparser.rargs. Options are still removed one at a time, because callbacks may inspect and modifyparser.rargs.Short command lines are unaffected: about 1.5 µs per
parse_args()call before and after, within ±3%.Testing:
-, empty strings and--, and callbacks that consume or modifylargsandrargsbetween runs.count,appendandnargs=2options, callbacks that read or modifylargsandrargs,--,-, empty strings, unknown options, anddisable_interspersed_args(). The parsed values, leftover arguments, what the callbacks saw, and the errors were all identical.test_optparse,test_argparse,test_getoptand the full test suite pass.AI disclosure: prepared with the help of Claude Code; I reviewed the change and the test results.