Skip to content

gh-153568: Pause the GC while converting the AST to Python objects#153583

Open
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-ast2obj-gc
Open

gh-153568: Pause the GC while converting the AST to Python objects#153583
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-ast2obj-gc

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026

Copy link
Copy Markdown
Member

Building the Python object tree out of the C AST allocates thousands of container objects in a burst, which repeatedly trips the garbage collector into traversing the half-built tree. Those nodes cannot be part of a reference cycle until the conversion exposes them, so a collection during the conversion can never free anything of this tree. Pausing the collector around the conversion (restoring the previous state, so a user-disabled GC stays disabled) removes those wasted traversals.

Benchmark (running ast.parse over 8 of the largest stdlib files, 1.3 MB, 20 times per run; pinned cores):

build time per run speedup
main 133.6 ms
this PR 120.5 ms 1.11x faster

Executed instructions (stable under machine load, perf stat) drop by 5.6%.

Freshly converted AST nodes cannot be part of a reference cycle yet, so
collections during the conversion only pay to traverse the half-built
tree without ever freeing any of it.
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