Skip to content

gh-153568: Cache repeated identifiers while parsing - #153577

Merged
pablogsal merged 3 commits into
python:mainfrom
pablogsal:gh-153568-ident-cache
Jul 18, 2026
Merged

pablogsal merged 3 commits into
python:mainfrom
pablogsal:gh-153568-ident-cache

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026 •

Copy link
Copy Markdown
Member

Every occurrence of a name paid a UTF-8 decode, a hash and an intern lookup, even though identifiers repeat constantly in real code. A small per-parse table now reuses the interned name created the first time a given spelling is seen, and everything it stores lives in the parse arena so no lifetime management is needed.

Benchmark (parsing 8 of the largest stdlib files, 1.3 MB, 20 times per run with _PyParser_ASTFromString — parser only, no AST-to-Python conversion; pyperf, interleaved runs):

build time per run speedup
main 1.70 s ± 0.02
this PR 1.45 s ± 0.01 1.17x faster
Identifiers repeat constantly in real code, so the parser now reuses
the interned name created the first time it sees a given spelling.
@StanFromIreland

Copy link
Copy Markdown
Member

Oh nice, a 17% speed up is quite good!

@pablogsal
pablogsal requested a review from Fidget-Spinner July 18, 2026 11:31
Comment thread Parser/pegen.c Outdated
// so borrowed references are valid for the lifetime of the parse
// (including the second error pass, which reuses parser and arena).
Py_ssize_t len = PyBytes_GET_SIZE(t->bytes);
uint32_t hash = 2166136261u;

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.

Move this to global+const.

Image
@pablogsal
pablogsal enabled auto-merge (squash) July 18, 2026 12:55
@pablogsal
pablogsal merged commit a0c6c4c into python:main Jul 18, 2026
50 checks passed
@pablogsal
pablogsal deleted the gh-153568-ident-cache branch July 18, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants