Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-153568: Reuse shared prefixes in generated parser rules - #157465

Open
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-parser-prefix-cache
Open

gh-153568: Reuse shared prefixes in generated parser rules#157465
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-parser-prefix-cache

Conversation

@pablogsal

@pablogsal pablogsal commented Sep 13, 2026

Copy link
Copy Markdown
Member

For obj[index], the parser tries the attribute and call alternatives before the subscript one. Each choice starts by asking for the same obj again. The parse cache saves us from parsing it from scratch, but we still pay for another rule call and cache lookup.

This teaches the generator to keep that result and the position where it ended in local variables. When one choice fails, the next one can pick them up directly and try its own ending. It does this for adjacent choices that start with the same memoized rule, including expression, arithmetic and assignment-target rules.

The grammar stays as it is. The generator finds these shared starts on its own.

On my machine, this makes parsing the standard library and twenty repositories about 10.7% faster overall.

Codebase Python files Parsing speed
Standard library 2,054 11.5% faster
Django 2,932 11.7% faster
pandas 1,527 11.6% faster
NumPy 495 11.7% faster
SciPy 999 10.7% faster
scikit-learn 1,036 10.0% faster
SymPy 1,607 11.6% faster
Transformers 4,884 10.7% faster
PyTorch 4,890 10.5% faster
Home Assistant 18,731 11.1% faster
mypy 442 9.7% faster
Requests 37 11.3% faster
Flask 83 11.3% faster
FastAPI 1,138 10.6% faster
SQLAlchemy 673 11.3% faster
Black 345 9.4% faster
pytest 274 10.7% faster
Ansible 1,853 10.6% faster
Scrapy 491 11.9% faster
Celery 428 10.2% faster
Sphinx 774 10.6% faster

The table compares this change against main at fd0970c0ab, using release builds without PGO or LTO. Each result is the median of five paired runs. The timings include tokenization, building the AST and cleanup, without compiling bytecode or running the files.

This overlaps with #157437, which shares some of these starts by rewriting the grammar. Adding this on top of that PR still gives about 5.2% faster parsing across the standard library, mypy, Home Assistant and PyTorch in a separate run. That comparison does not include the rest of the pending parser changes.

Part of #153568.

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