Skip to content

gh-153568: Make the parser's per-rule stack check an inline comparison - #153573

Merged
pablogsal merged 3 commits into
python:mainfrom
pablogsal:gh-153568-stack-check
Sep 14, 2026
Merged

pablogsal merged 3 commits into
python:mainfrom
pablogsal:gh-153568-stack-check

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026

Copy link
Copy Markdown
Member

Every rule entry paid a thread-state fetch plus an out-of-line call just to ask whether the C stack is about to overflow. Caching the thread state and soft stack limit in the parser turns that into one inline pointer comparison, and the overflow guard still fires exactly as before.

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.86 s
this PR 1.65 s 1.13x faster

Comment thread Parser/pegen.c Outdated
Comment thread Parser/pegen.c Outdated
Comment thread Parser/pegen.h
pablogsal and others added 3 commits September 14, 2026 16:31
…parison

The parser now caches the thread state and stack limit up front and
only falls back to the full check when the stack is nearly exhausted.
@pablogsal
pablogsal force-pushed the gh-153568-stack-check branch from f684750 to fad63c3 Compare September 14, 2026 15:34
@pablogsal
pablogsal enabled auto-merge (squash) September 14, 2026 15:41
@pablogsal
pablogsal merged commit f88d668 into python:main Sep 14, 2026
63 checks passed
@pablogsal
pablogsal deleted the gh-153568-stack-check branch September 14, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants