Skip to content

gh-157407: Reduce code size of ast and unicodeobject - #157393

Merged
methane merged 6 commits into
python:mainfrom
methane:reduce-codesize
Sep 23, 2026
Merged

methane merged 6 commits into
python:mainfrom
methane:reduce-codesize

Conversation

@methane

@methane methane commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

This pull request refactors and optimizes the initialization and cleanup code for AST annotations and global static strings, focusing on reducing .text segment size.
This change reduces the size of Python's .text segment by approximately 1%, or about 60 KiB.

The main changes involve switching to offset-based iteration for both AST type cleanup and string initialization, and consolidating annotation logic to avoid code duplication.

Disclosure of the use of AI:

  • This change was selected and refined by me from a set of code-size reduction changes generated by Codex.
  • The description above was edited by me based on Copilot's "Generate Summary."
  • This text was translated into English by ChatGPT from the Japanese text I wrote.

@picnixz

picnixz commented Sep 13, 2026

Copy link
Copy Markdown
Member

Can you create an issue for such changes please? just for tracking purposes

@methane methane changed the title reduce .text size gh-157407: Reduce code size of ast and unicodeobject Sep 13, 2026
@methane

methane commented Sep 13, 2026

Copy link
Copy Markdown
Member Author

This pr reduces:

  • add_ast_annotations: 48K bytes
  • _PyAST_Fini : 13K bytes
  • _PyUnicode_InitGlobalObjects : 10K bytes

Comment thread Python/Python-ast.c Outdated
Comment thread Parser/asdl_c.py Outdated
Comment thread Parser/asdl_c.py Outdated
Comment thread Tools/build/generate_global_objects.py Outdated
Comment thread Parser/asdl_c.py
Comment thread Parser/asdl_c.py Outdated
methane and others added 3 commits September 22, 2026 12:34
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
@methane
methane merged commit 16b357b into python:main Sep 23, 2026
52 checks passed
@hugovk

hugovk commented Sep 23, 2026

Copy link
Copy Markdown
Member

Looks like this caused test_peg_generator to fail on a number of AMD64 Windows (tier 1) buildbots:

Python-ast.c(835): error C2099: initializer is not a constant

For example: https://buildbot.python.org/#/builders/1620/builds/4536

methane added a commit that referenced this pull request Sep 24, 2026
`&PyUnicode_Type` cannot be determined at compile time on Windows.
Caused by #157393.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants