GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@pablogsal pablogsal commented Jul 11, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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
  • Issue: Speed up the parser #153568

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@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
Speed up the parser #153568
Open
@bedevere-app bedevere-app Bot added the awaiting core review label 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 mentioned this pull request Sep 13, 2026
gh-153568: Parse arithmetic with operator loops #157413
Open
@pablogsal
pablogsal force-pushed the gh-153568-atom-fastpath branch from 61277c2 to 5616907 Compare September 14, 2026 20:50
@waroad waroad mentioned this pull request Oct 17, 2026
importlib: later import hangs forever after a RecursionError in a small-stack thread leaves a module lock held #159652
Closed
@ambv

ambv commented Oct 20, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

pablogsal reacted with confused emoji
All reactions
  • 😕 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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 reviewed Oct 22, 2026
View reviewed changes

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread Tools/peg_generator/pegen/c_generator.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Parser/pegen.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Parser/pegen.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Oct 23, 2026

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

pablogsal commented Oct 23, 2026

Copy link
Copy Markdown
Member Author

@lysnikolaou can you take another look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lysnikolaou
lysnikolaou reviewed Oct 23, 2026
View reviewed changes

@lysnikolaou lysnikolaou left a comment

Copy link
Copy Markdown
Member

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.

just one hypernit

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Parser/pegen.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

lysnikolaou
lysnikolaou approved these changes Oct 23, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 23, 2026
@pablogsal
pythongh-153568: Skip the expression precedence chain for single-toke…
86b00a5
…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
@pablogsal @lysnikolaou
Update Parser/pegen.c
071ed26
Co-authored-by: Lysandros Nikolaou <[email protected]>
@pablogsal
pythongh-153568: Parse rule flags in the Rule constructor
a431447
Expose memo and fastpath attributes for the C generator and simplify the atom fast-path comment.
@lysnikolaou @pablogsal
Update Parser/pegen.c
ef620c0
@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

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal deleted the gh-153568-atom-fastpath branch October 23, 2026 21:23
@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lysnikolaou lysnikolaou lysnikolaou approved these changes
@AA-Turner 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

Labels

type-feature A feature request or enhancement

Projects

None yet

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.

3 participants

@pablogsal @ambv @lysnikolaou
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.