Skip to content

gh-161282: Fix quadratic time in optparse when parsing many positional arguments - #161283

Open
thejesh23 wants to merge 6 commits into
python:mainfrom
thejesh23:optparse-positionals
Open

thejesh23 wants to merge 6 commits into
python:mainfrom
thejesh23:optparse-positionals

Conversation

@thejesh23

@thejesh23 thejesh23 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Move each run of positional arguments from rargs to largs in one step instead of del rargs[0] per argument. Options are still consumed one at a time, so callbacks see the same largs and rargs.

Tested with new tests (runs mixed with options, -, '', -- and callbacks) and a comparison against the old code on 200,000 random argument lists. 200,000 positional arguments: 2.4 s → 9 ms.

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'.
@thejesh23

Copy link
Copy Markdown
Contributor Author

The ASan failure is test_profiling (test_sampling_with_collapsed_export, ':' not found in '<unreadable frame>'), the known flake tracked in gh-160334; it's unrelated to this change. test_optparse and the rest of the suite passed in that job.

@thejesh23

Copy link
Copy Markdown
Contributor Author

command-line tool that still uses optparse being handed a long list of file paths benifit from this fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant