Skip to content

gh-153568: Skip the expression precedence chain for single-token atoms - #153575

Merged
pablogsal merged 4 commits into
python:mainfrom
pablogsal:gh-153568-atom-fastpath
Oct 23, 2026
Merged

pablogsal merged 4 commits into
python:mainfrom
pablogsal:gh-153568-atom-fastpath

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026 •

Copy link
Copy Markdown
Member

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

@pablogsal
pablogsal requested a review from lysnikolaou as a code owner July 11, 2026 14:01
@bedevere-app bedevere-app Bot mentioned this pull request Jul 11, 2026
@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch 4 times, most recently from f3cc7e4 to 24e869c Compare July 11, 2026 14:39
@pablogsal
pablogsal requested a review from AA-Turner as a code owner July 11, 2026 14:39
@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch 2 times, most recently from b3a8d8f to 605c6d7 Compare July 11, 2026 15:29
@pablogsal 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
pablogsal force-pushed the gh-153568-atom-fastpath branch 2 times, most recently from 26282a7 to 61277c2 Compare July 11, 2026 15:46
@pablogsal 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
@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch from 61277c2 to 5616907 Compare September 14, 2026 20:50
@ambv

ambv commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

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 ambv closed this Oct 20, 2026
@ambv ambv reopened this Oct 21, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 21, 2026

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

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.

Comment thread Tools/peg_generator/pegen/c_generator.py Outdated
Comment thread Parser/pegen.c Outdated
Comment thread Parser/pegen.c Outdated
@pablogsal

Copy link
Copy Markdown
Member Author

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
muse_mascot_jolly_by_cassiemunoz_dmvztvk-pre

@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch from 7dac9d1 to 0136cf5 Compare October 23, 2026 19:00
@pablogsal

Copy link
Copy Markdown
Member Author

@lysnikolaou can you take another look?

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one hypernit

Comment thread Parser/pegen.c Outdated
…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.
pablogsal and others added 3 commits October 23, 2026 21:15
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Expose memo and fastpath attributes for the C generator and simplify the atom fast-path comment.
@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch from 8b90c67 to ef620c0 Compare October 23, 2026 20:17
@pablogsal
pablogsal enabled auto-merge (squash) October 23, 2026 20:17
@pablogsal
pablogsal merged commit b77cb89 into python:main Oct 23, 2026
65 checks passed
@pablogsal
pablogsal deleted the gh-153568-atom-fastpath branch October 23, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants