Skip to content

gh-153568: Don't materialize parser token text that is never read#153576

Open
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-token-text
Open

gh-153568: Don't materialize parser token text that is never read#153576
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-token-text

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026

Copy link
Copy Markdown
Member

The parser created a bytes object for every token, but only identifiers, keywords, numbers, strings and type comments ever have their text read. Operators and structural tokens now skip the allocation entirely.

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.72 s ± 0.04
this PR 1.69 s ± 0.03 1.02x faster

Only tokens whose text is actually consumed get a bytes object;
operators and structural tokens no longer allocate one.
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.

1 participant