Skip to content

argparse performance regression in 3.14+ due to colorization overheadย #142267

@savannahostrowski

Description

@savannahostrowski

I spent some time running the argparse bm_subparsers benchmark from pyperformance (1000 optional arguments, 10 iterations)

Version Time Function Calls
3.10 0.585s 560K
3.11 0.207s 560K
3.12 0.229s 560K
3.13 0.246s 560K
3.14 0.934s 2,380K
3.15 (main) 0.883s 2,367K

For a "realistic" CLI with 10 arguments, parser creation is ~3x slower on main compared to 3.13.

The root cause is that _get_formatter() is called twice per add_argument() - once for metavar validation and once in _check_help() for help string validation (called at the end of add_argument ). Each _get_formatter() call creates a new HelpFormatter, which calls _set_color(), which calls can_colorize(), which checks 5 environment variables.

I think a viable fix is to cache the HelpFormatter on ArgumentParser for validation operations. The validation only performs read-only operations (_format_args, _expand_help) that don't modify formatter state. This preserves the existing _get_formatter() behavior while eliminating redundant _set_color() calls during argument setup.

Linked PRs

Metadata

Metadata

Labels

3.14bugs and security fixes3.15new features, bugs and security fixesperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions