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: Pause the GC while converting the AST to Python objects by pablogsal · Pull Request #153583 · 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

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%.

  • 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 15:51
@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
maurycy
maurycy reviewed Jul 17, 2026
View reviewed changes
Comment thread Parser/asdl_c.py
// so a garbage collection while building them cannot free anything of
// this tree and only pays to traverse its half-built nodes. Pause the
// collector while the conversion runs.
int gc_was_enabled = PyGC_Disable();

@maurycy maurycy Jul 17, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

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.

My, probably naive, question here is what about free-threading?

There seems to be a single global (per interpret) state:

cpython/Python/gc_free_threading.c

Lines 2527 to 2539 in 9d231cb

int
PyGC_Enable(void)
{
GCState *gcstate = get_gc_state();
return _Py_atomic_exchange_int(&gcstate->enabled, 1);
}
int
PyGC_Disable(void)
{
GCState *gcstate = get_gc_state();
return _Py_atomic_exchange_int(&gcstate->enabled, 0);
}

cpython/Python/gc_free_threading.c

Lines 1656 to 1661 in 9d231cb

static GCState *
get_gc_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->gc;
}

For example: a thread disabled GC (in the window between here) but the parser would enable it again?

Sorry, something went wrong.

Uh oh!

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

All reactions

@pablogsal pablogsal Jul 18, 2026

Copy link
Copy Markdown
Member Author

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.

Yes this will always be racy because is currently not possible to know if this was us or someone else doing it. This is something we need to consider when reviewing this optimization

Sorry, something went wrong.

Uh oh!

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

All reactions

@maurycy maurycy Jul 18, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

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.

Maybe this is a more general problem?

Quick ripgrep reveals:

cpython/Modules/_posixsubprocess.c

Lines 1063 to 1067 in 1b9fe5c

/* We need to call gc.disable() when we'll be calling preexec_fn */
if (preexec_fn != Py_None) {
need_to_reenable_gc = PyGC_Disable();
}

cpython/Modules/_posixsubprocess.c

Lines 1326 to 1329 in 1b9fe5c

if (need_to_reenable_gc) {
PyGC_Enable();
}

In reverse, maybe even more places would gain from disabling changing the default GC behaviour (disabling it?).

Sorry, something went wrong.

Uh oh!

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

All reactions
@pablogsal pablogsal mentioned this pull request Jul 18, 2026
Add a private API to defer automatic garbage collection #153946
Open
@pablogsal
pablogsal force-pushed the gh-153568-ast2obj-gc branch from 7a56747 to 09a5678 Compare August 25, 2026 23:52
pablogsal added 2 commits September 14, 2026 21:36
@pablogsal
pythongh-153568: Pause the GC while converting the AST to Python objects
e6bd93f
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.
@pablogsal
pythongh-153568: Skip GC pause without the GIL
e00fbc2
@pablogsal
pablogsal force-pushed the gh-153568-ast2obj-gc branch from 7b2600f to e00fbc2 Compare September 14, 2026 20:50
lysnikolaou
lysnikolaou approved these changes 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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 22, 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
+1 more reviewer
@maurycy maurycy maurycy left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

awaiting merge

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 @maurycy @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.