-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
argparse performance regression in 3.14+ due to colorization overhead #142267
Copy link
Copy link
Closed
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Fields
Give feedbackNo fields configured for issues without a type.
I spent some time running the argparse
bm_subparsersbenchmark frompyperformance(1000 optional arguments, 10 iterations)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 peradd_argument()- once for metavar validation and once in_check_help()for help string validation (called at the end ofadd_argument). Each_get_formatter()call creates a newHelpFormatter, which calls_set_color(), which callscan_colorize(), which checks 5 environment variables.I think a viable fix is to cache the
HelpFormatteronArgumentParserfor 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
_set_colorcalls #142268_set_colorcalls (GH-142268) #142313