Skip to content

GH-142267: Cache formatter to avoid repeated _set_color calls#142268

Merged
savannahostrowski merged 7 commits into
python:mainfrom
savannahostrowski:argparse-color-perf
Dec 5, 2025
Merged

GH-142267: Cache formatter to avoid repeated _set_color calls#142268
savannahostrowski merged 7 commits into
python:mainfrom
savannahostrowski:argparse-color-perf

Conversation

@savannahostrowski

@savannahostrowski savannahostrowski commented Dec 4, 2025

Copy link
Copy Markdown
Member

bm_subparsers benchmark (1000 optional arguments, 10 iterations):

Version Time
3.13 0.246s
3.14 0.934s
3.15 (main) 0.883s
3.15 (with fix) 0.158s

Profiled with Tachyon (./python.exe -m profiling.sampling --live):

Before:
🥇 HelpFormatter._set_color (12.6%) │ 🥈 Mapping.get (5.4%) │ 🥉 can_colorize._safe_getenv (4.1%)

After:
🥇 <GC> (8.4%) │ 🥈 bm_subparsers (8.3%) │ 🥉 _ActionsContainer.add_argument (7.3%)

Comment thread Lib/argparse.py
Comment thread Lib/argparse.py
Comment thread Lib/argparse.py Outdated

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@savannahostrowski savannahostrowski enabled auto-merge (squash) December 5, 2025 16:20
@savannahostrowski savannahostrowski merged commit 4085ff7 into python:main Dec 5, 2025
46 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 5, 2025
…pythonGH-142268)

(cherry picked from commit 4085ff7)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
@bedevere-app

bedevere-app Bot commented Dec 5, 2025

Copy link
Copy Markdown

GH-142313 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Dec 5, 2025
savannahostrowski added a commit that referenced this pull request Dec 5, 2025
GH-142268) (#142313)

GH-142267: Cache formatter to avoid repeated `_set_color` calls (GH-142268)
(cherry picked from commit 4085ff7)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants