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 — Reduce some code sizes · Issue #157407 · python/cpython

Reduce some code sizes #157407

New issue
Copy link
New issue
Copy link
Open
Open
Reduce some code sizes#157407
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@methane
methane
opened on Sep 13, 2026
Issue body actions

In a release build without PGO or LTO, Python's .text segment is over 5 MB.

$ size ./python
   text    data     bss     dec     hex filename
5726491  829744  468280 7024515  6b2f83 ./python

Much of this size is necessary, but some code can be reduced significantly with only small changes to code generation. Reducing the amount of code may improve the efficiency of CPU resources such as the instruction cache and branch prediction structures.

The following are the results of having Codex investigate the top 25 functions.

Largest functions in CPython

Top 25 functions in the rebuilt ./python on main, before merging the bytes and 1-byte Unicode decimal-output paths.

Sizes are function symbol sizes in bytes, obtained with readelf --symbols --wide ./python and sorted in descending order. They include inlined code but exclude separately stored data tables and out-of-line callees. The comments summarize the main reasons for the code size, based on the source and inline debug information.

Rank Function Bytes Why it is large
1 _PyEval_EvalFrameDefault 61,487 Main bytecode interpreter: many instructions and specialized variants, with inlined stack and reference-count operations.
2 add_ast_annotations 49,036 Generated code repeats type construction, dictionary insertion, cleanup, and error handling for every AST field.
3 obj2ast_stmt 31,029 Converts Python AST objects into internal statement nodes, with separate field validation and conversion for each statement type.
4 long_to_decimal_string_internal 24,989 Decimal digit output is duplicated for bytes and 1-, 2-, and 4-byte Unicode representations, with substantial SIMD expansion.
5 obj2ast_expr 20,585 The expression counterpart: many expression types, each with its own attribute checks and conversion logic.
6 type_ready 20,027 Many type-initialization helpers are inlined, especially the extensive slot-inheritance logic in inherit_slots.
7 sre_ucs2_match 17,008 Complete regex matching engine for 2-byte characters, including repetition, backtracking, and inlined character-set checks.
8 sre_ucs4_match 16,341 A separately compiled copy of the regex matching engine for 4-byte characters.
9 sre_ucs1_match 16,288 A separately compiled copy of the regex matching engine for 1-byte characters.
10 _PyConfig_Read 15,804 Command-line parsing, environment processing, encoding configuration, and other initialization helpers are extensively inlined.
11 ast2obj_stmt.part.0 15,590 Builds Python AST objects from internal statements, with repeated attribute assignments and inlined list conversions.
12 _PyAST_Fini 13,914 Generated Py_CLEAR operations individually expand reference-count checks and cleanup for many AST state fields.
13 errno_exec 13,753 Registers many errno constants individually; the _add_errcode helper is inlined at repeated call sites.
14 _PyUnicode_ToNumeric 13,106 A generated switch maps a large, sparse set of Unicode code points to numeric values.
15 init_types 12,346 Generated initialization for all AST classes and operator singletons, plus inlined identifier initialization.
16 split 12,249 Inlines whitespace and separator splitting for ASCII and 1-, 2-, and 4-byte Unicode representations.
17 replace 12,168 Handles several replacement strategies and character-width conversions, with inlined search and counting routines.
18 r_object 12,135 Main marshal decoder: handles many object types, nested containers, code objects, and shared-reference reconstruction.
19 PyUnicode_Format 11,388 Inlines % format parsing, argument conversion, padding, width, and precision handling.
20 codegen_visit_expr_impl 11,387 Compiles many expression types, with inlined helpers for calls, method-call optimization, subscripts, and string construction.
21 _PyUnicode_InitGlobalObjects 11,154 Contains generated static-string interning calls and inlined initialization of the global table and interpreter intern dictionary.
22 simple_stmt_rule 9,764 Generated parser alternatives inline several statement rules, particularly imports, plus lookahead, backtracking, and syntax-error handling.
23 codegen_visit_stmt 9,732 Dispatches statement compilation and inlines helpers for imports, assignments, returns, and pattern matching.
24 compound_stmt_rule 9,705 Inlines substantial parsing logic for with, match, try, and for, including invalid-syntax rules.
25 _PyLexer_get_normal 9,681 Combines token recognition, indentation handling, and identifier validation with inlined character-reading and buffer-refill logic.

Linked PRs

  • gh-157407: Reduce code size of ast and unicodeobject #157393
  • gh-157407: Argument Clinic: parse positional-only defining_class methods without _PyArg_Parser #157483
  • gh-157407: Shrink _PyArg_Parser and don't write builtin parsers at run time #157531
  • gh-157407: Fix a compilation error on Windows. #158048
  • gh-157407: Use _PyUnicode_AsUTF8NoNUL for AC 's' and 'z' converters #158210

Activity

  1. StanFromIreland
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Sep 13, 2026
  2. picnixz
    added
    performancePerformance or resource usagePerformance or resource usage
    on Sep 13, 2026
  3. eendebakpt commented on Sep 13, 2026

    @eendebakpt
    eendebakpt
    on Sep 13, 2026
    Contributor
    More actions

    @methane There might also be something to gain here with argument clinic (the added .text per method is not large, but argument clinic generates many methods). See main...eendebakpt:cpython:feature/clinic-posonly-defclass-2 (-2.5 kB text, -12 kB .data ) and main...eendebakpt:cpython:feature/clinic-parser-split (to be measured). If you think these are relevant I will clean up the branches a bit and make a PR against this issue.

    Reacted by Inada Naoki
  4. methane commented on Sep 14, 2026

    @methane
    methane
    on Sep 14, 2026
    MemberAuthor
    More actions

    Creating a separate issue for every opportunity to reduce code size would be inefficient. Let's use this issue until Python 3.16 reaches beta.

    I plan to close this issue once the problems we have already identified are resolved, but the issue number can still be referenced after it is closed.

  5. eendebakpt
    added 2 commits that reference this issue on Sep 15, 2026

    pythongh-157407: Rename NEWS entry to the correct issue number

    28fb676

    pythongh-157407: Rename NEWS entry to the correct issue number

    22c7704
  6. methane
    added 2 commits that reference this issue on Sep 23, 2026

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

    16b357b

    gh-157407: Fix a compilation error on Windows. (#158048)

    bd0913a
  7. eendebakpt
    added 11 commits that reference this issue on Sep 26, 2026

    pythongh-157407: Add a const _PyArg_KwParser for parsers without a fo…

    fbcc23e

    pythongh-157407: Regenerate Argument Clinic files for _PyArg_KwParser

    06ad350

    pythongh-157407: Shrink Argument Clinic parsers and make them const i…

    8a6ab57

    pythongh-157407: Regenerate Argument Clinic files

    25dab20

    pythongh-157407: Shrink Argument Clinic parsers and make them const i…

    35424bc

    pythongh-157407: Regenerate Argument Clinic files

    99813c3

    pythongh-157407: Shrink Argument Clinic parsers and make them const i…

    4731422

    pythongh-157407: Regenerate Argument Clinic files

    19e889a

    pythongh-157407: Shrink Argument Clinic parsers and make them const i…

    6581ec1

    pythongh-157407: Regenerate Argument Clinic files

    4775079

    gh-157407: Argument Clinic: parse positional-only `defining_class` me…

    1c4fa4f
  8. eendebakpt
    added a commit that references this issue on Oct 5, 2026

    gh-157407: Use _PyUnicode_AsUTF8NoNUL for AC 's' and 'z' converters (#…

    cfd6cf7
  9. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  10. github-project-automation
    moved this to Todo in Project_VKon Oct 18, 2026
  11. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.