GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-157407: Shrink _PyArg_Parser and don't write builtin parsers at run time by eendebakpt · Pull Request #157531 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Sep 14, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Make the argument clinic generated code more efficient:

  1. Store the number of positional-only parameters statically. Argument Clinic now emits .pos = N in the static _PyArg_Parser with allows _PyArg_UnpackKeywords() to skip parser_init().
  2. Reduce the size of _PyArg_Parser. pos/min/max become uint16_t and is_kwtuple_owned becomes uint8_t. custom_msg is removed: it is only needed when reporting a conversion error, so it is now looked up in the format string (";message") on that path. No in-tree parser uses ";message". _parser_init() raises SystemError for more than 65535 keywords. The struct goes from 73 bytes to 48 bytes.

Binary size change: .data / file size, main vs. this PR (Release builds):

.data .text file
Windows python316.dll (MSVC) −11,528 +48 −11,264
Windows, all *.pyd + dll −22,008 +48 −24,064
Linux python (gcc 15, static libpython) −9,120 +192
Linux, python + all *.so −17,408 +192

Possible follow-ups

With some more refactoring this could go further:

  • Non-format parsers (the common case, ~750 of ~760 in CPython) never use format, min or max. Builtin-module parsers also never use once, is_kwtuple_owned or next. A separate, smaller struct for them would save more memory, at the cost of changing the private API.
  • More static initialization: Argument Clinic could also emit fname, min and max for format-string parsers, so that builtin ones skip parser_init() too. parser_clear() then only needs to reset the kwtuple and once, since the other fields only depend on the static keywords and format.

Generated with help from Claude Code

  • Issue: Reduce some code sizes #157407

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt and others added 2 commits September 14, 2026 22:26
@eendebakpt @claude
pythongh-136681: Store the number of positional-only parameters stati…
5169a7a
…cally

Argument Clinic now sets _PyArg_Parser.pos statically.  Parsers with a
statically allocated keyword tuple (builtin modules) therefore need no
initialization: _PyArg_UnpackKeywords() skips parser_init() for them,
so they are never written to at run time.

A lazily built keyword tuple is now published last, with
_Py_atomic_store_ptr_release().  parser_clear() keeps pos, since it
only depends on the keywords.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@eendebakpt @claude
pythongh-136681: Reduce the size of _PyArg_Parser
9a7c32b
Narrow pos, min, max and is_kwtuple_owned, which are small counts, and
remove custom_msg: it is only needed to report a conversion error, and
can then be looked up in the format string.  Keep format, keywords and
fname first, so that existing positional initializers still work.
_PyArg_Parser shrinks from 72 to 48 bytes on 64-bit platforms.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@eendebakpt
eendebakpt requested review from AA-Turner, ericsnowcurrently and erlend-aasland as code owners September 14, 2026 21:57
@bedevere-app bedevere-app Bot mentioned this pull request Sep 14, 2026
Reduce some code sizes #157407
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 14, 2026
maurycy
maurycy reviewed Sep 15, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-09-14-12-00-00.gh-issue-157407.pQ7sLk.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt and others added 3 commits September 15, 2026 14:11
@eendebakpt @claude
pythongh-157407: Rename NEWS entry to the correct issue number
28fb676
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@eendebakpt
Merge remote-tracking branch 'origin/main' into feature/clinic-parser…
e356a09
…-shrink

# Conflicts:
#	Modules/_ctypes/clinic/_ctypes.c.h
#	Modules/_io/clinic/bufferedio.c.h
#	Modules/_io/clinic/textio.c.h
#	Modules/_zstd/clinic/zstddict.c.h
#	Modules/clinic/_asynciomodule.c.h
#	Modules/clinic/_cursesmodule.c.h
#	Python/clinic/bltinmodule.c.h
@eendebakpt
Merge remote-tracking branch 'origin/main' into feature/clinic-parser…
a0d41bb
…-shrink

# Conflicts:
#	Modules/_io/clinic/bufferedio.c.h
#	Modules/_io/clinic/fileio.c.h
#	Modules/_io/clinic/iobase.c.h
#	Modules/_io/clinic/textio.c.h
#	Modules/_io/clinic/winconsoleio.c.h
@eendebakpt

eendebakpt commented Sep 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Update: the followup ideas have been implemented in main...eendebakpt:feature/clinic-kwparser. That results in

.text .rdata .data file
python316.dll +1,152 −624 −28,744 −31,232
all *.pyd + dll +1,152 −2,112 −44,440 −49,664

I will look into opening a PR for that as an alternative for this PR.

Update: and some more reduction: eendebakpt/cpython@main...feature/clinic-kwparser-split

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
1 more reviewer
@maurycy maurycy maurycy left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@eendebakpt @maurycy
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.