GeezerVison is showing github.com — gh-153568: Reuse shared prefixes in generated parser rules by pablogsal · Pull Request #157465 · python/cpython
Conversation
pablogsal
commented
Sep 13, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 afe3d53d4d 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Sep 15, 2026
⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️
Hi! The buildbot AMD64 CentOS9 Refleaks 3.x (tier-1) has failed when building commit 89fe985.
What do you need to do:
- Don't panic.
- Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
- Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/853/builds/4253) and take a look at the build logs.
- Check if the failure is related to this commit (89fe985) or if it is a false positive.
- If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.
You can take a look at the buildbot page here:
https://buildbot.python.org/#/builders/853/builds/4253
Summary of the results of the build (if available):
==
Click to see traceback logs
Note: switching to '89fe9857dd0832de9b597d63ecb11fa7a09984f1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 89fe9857dd0 gh-153568: Reuse shared prefixes in generated parser rules (#157465)
Switched to and reset branch 'main'
make: *** [Makefile:2481: buildbottest] Error 3
Expanding reachable commits in commit graph: 99281
Expanding reachable commits in commit graph: 171162, done.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
lysnikolaou
Awaiting requested review from lysnikolaou
lysnikolaou is a code owner
AA-Turner
Awaiting requested review from AA-Turner
AA-Turner is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|