Skip to content

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

Merged
pablogsal merged 3 commits into
python:mainfrom
pablogsal:gh-153568-token-text
Sep 14, 2026
Merged

pablogsal merged 3 commits 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
Comment thread Parser/pegen.c Resolved
@maurycy

maurycy commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

It seems that 1e18605, suggested by me, broke a lot! I think it means that token_type == NAME is mapped to something that's not in the token_needs_text(). I did not understand this. This is fragile! My bad.

@pablogsal
pablogsal force-pushed the gh-153568-token-text branch 2 times, most recently from 382cb01 to f78606c Compare August 27, 2026 11:39
pablogsal and others added 3 commits September 14, 2026 16:32
Only tokens whose text is actually consumed get a bytes object;
operators and structural tokens no longer allocate one.
Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
@pablogsal
pablogsal enabled auto-merge (squash) September 14, 2026 20:27
@pablogsal
pablogsal merged commit e5fbabb into python:main Sep 14, 2026
98 of 100 checks passed
@pablogsal
pablogsal deleted the gh-153568-token-text branch September 14, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants