{{ message }}
gh-157407: Shrink _PyArg_Parser and don't write builtin parsers at run time - #157531
Open
eendebakpt wants to merge 3 commits into
Open
eendebakpt wants to merge 3 commits into
eendebakpt wants to merge 3 commits into
Conversation
…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) <noreply@anthropic.com>
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) <noreply@anthropic.com>
eendebakpt
requested review from
AA-Turner,
ericsnowcurrently and
erlend-aasland
as code owners
September 14, 2026 21:57
maurycy
reviewed
Sep 15, 2026
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Make the argument clinic generated code more efficient:
.pos = Nin the static_PyArg_Parserwith allows_PyArg_UnpackKeywords()to skipparser_init()._PyArg_Parser.pos/min/maxbecomeuint16_tandis_kwtuple_ownedbecomesuint8_t.custom_msgis 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()raisesSystemErrorfor 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):python316.dll(MSVC)*.pyd+ dllpython(gcc 15, static libpython)python+ all*.soPossible follow-ups
With some more refactoring this could go further:
format,minormax. Builtin-module parsers also never useonce,is_kwtuple_ownedornext. A separate, smaller struct for them would save more memory, at the cost of changing the private API.fname,minandmaxfor format-string parsers, so that builtin ones skipparser_init()too.parser_clear()then only needs to reset the kwtuple andonce, since the other fields only depend on the static keywords and format.Generated with help from Claude Code