GeezerVison is showing github.com — gh-153568: Skip the expression precedence chain for single-token atoms by pablogsal · Pull Request #153575 · python/cpython
Conversation
pablogsal
commented
Jul 11, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Parsing a bare name or number descends the whole expression precedence chain (about 14 rule invocations plus their memoization traffic) just to wrap one token in a node, and that shape dominates real code through call arguments, assignments and subscripts. When the next token cannot start or continue an expression the result is provably that single atom, so the chain entry rules now build it directly, leaving the token fill state exactly as before so error locations are unaffected.
Rules declare the hook in the grammar with a new (fastpath=function) flag next to (memo), so the generator only emits the hook call and knows nothing about tokens or expressions; the atom logic lives with the other token helpers in pegen.c, and the mechanism is documented with a worked example in InternalDocs/parser.md. Candidate hooks on more chain rules, assignment targets and subscript slices were measured individually and dropped: none cleared 0.4%, and hooking every chain level measured 6% slower from the per-entry decline checks.
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.69 s ± 0.03 |
|
| this PR |
1.52 s ± 0.02 |
1.11x faster |
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
pablogsal
changed the title
gh-153568: Skip the expression precedence chain for single-token atoms
gh-153568: Add fast paths for single-token atoms in the parser
Jul 11, 2026
pablogsal
changed the title
gh-153568: Add fast paths for single-token atoms in the parser
gh-153568: Skip the expression precedence chain for single-token atoms
Jul 11, 2026
ambv
commented
Oct 20, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
This PR is superseded by #157413, a newer open pull request for the same issue (gh-153568), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.
Sorry, incorrectly flagged as a duplicate.
|
pablogsal reacted with confused emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great idea! Left a few comments inline.
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.
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 PR is superseded by #157413, a newer open pull request for the same issue (gh-153568), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.
Sorry, incorrectly flagged as a duplicate.
@ambv

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
…n atoms
A bare name or number followed by a token that cannot extend an
expression is now built directly, avoiding a dozen rule invocations
per atom. Rules declare the hook with the new (fastpath=function)
flag next to (memo); the generator only emits the hook call and knows
nothing about tokens or expressions.
Expose memo and fastpath attributes for the C generator and simplify the atom fast-path comment.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
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.
|