Skip to content

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

Open
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-atom-fastpath
Open

gh-153568: Skip the expression precedence chain for single-token atoms#153575
pablogsal wants to merge 1 commit 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 from 605c6d7 to 26282a7 Compare July 11, 2026 15:38
…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 pablogsal force-pushed the gh-153568-atom-fastpath branch 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
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